Go Back   kirupaForum > Flash > Flash IDE

Reply
 
Thread Tools Display Modes
Old 12-30-2008, 02:36 PM   #1
ThaJock
Registered User
Location McKinney, Texas

Posts 109
addEventListener to var when value change

Is there any way to add an eventListener to a variable that listenes for if it change value?
ThaJock is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?

Old 12-30-2008, 05:42 PM   #2
Eric Schleeper
nerd
You need to this when you build a Flash app based on MVC. Actually, this is an example of the Observer Pattern without creating the interface classes. You can make a package with a class that extends the EventDispatcher, then you can make another class(es) that listen for it to change:

package
{
import flash.events.*;
public class VariableHolder extends EventDispatcher
{
private var yourVariable:String;
public function getYourVariable():String
{
return yourVariable;
}
public function setYourVariable(newOne:String):void
{
yourVariable = newOne;
this.update();
}
protected function update():void
{
dispatchEvent(new Event(Event.CHANGE));
}
}
}

Then the class that listens for it
package
{
import flash.events.*;
public class ListensForEvent
{
private var variableHolder:VariableHolder;
private var theOneInTheHolder:String;
//this will take the VariableHolder as a parameter when instantiated
public function ListensForEvent(vHolder:VariableHolder)
{
this.variableHolder = vHolder;
}
public function update(event:Event = null):void
{
theOneInTheHolder = variableHolder.getYourVariable();
trace(theOneInTheHolder);
}
}
}

to make it all work put this on the timeline:

import VariableHolder;
import ListensForEvent;

var theHolder:VariableHolder = new VariableHolder();
var listener1:ListensForEvent = new ListensForEvent(theHolder);
theHolder.addEventListener(Event.CHANGE, listener1.update);

var listener2:ListensForEvent = new ListensForEvent(theHolder);
theHolder.addEventListener(Event.CHANGE, listener2.update);

theHolder.setYourVariable('new Variable');

//this should have:
//new Variable
//new Variable
//in the output


modEdit: please edit your original post instead of double or triple posting

__________________
www.ericschleeper.com
Eric Schleeper is offline   Reply With Quote
Old 04-04-2009, 04:05 PM   #3
justin_mcnally
Registered User
private _variable:String;

public function get variable():String {
return this._variable;
}
public function set variable(value:String):void {
//run triggers here
this._variable = value;
}

now anything u wanna run when the value of vairable is changed just goes in the setter where it says run tiggers here if u really want to dispatch an event you can that from there too like.

this.dispatchEvent(new Event("Variable_Changed"));
justin_mcnally is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 10:20 AM.

SHARE:

SUPPORTERS:

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com