PDA

View Full Version : eventlistener on a combobox (class)


_philip_
03-10-2007, 09:13 PM
hi, trying to put an eventlistener to an combobox, but cant manage to get it work. Anyone who sees what i'm missing?



import mx.controls.*;
import mx.utils.Delegate;
import mx.events.EventDispatcher;

class no.pd.FileBrowser
{


// Event Listner
private var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;


// View
private var holder_mc:MovieClip;
private var container_mc:MovieClip;
private var fileview:MovieClip;
private var viewcombo:ComboBox;
private var viewlist:MovieClip;
private var item_mc:MovieClip;
private var item_arr:Array;



/**
* FileBrowser : Constructor
*/

public function FileBrowser(target:MovieClip)
{
EventDispatcher.initialize(this);
container_mc = target;
initView();
}


/**
* initView : Set the view
*/

private function initView()
{
holder_mc = container_mc.attachMovie("filebrowser_mc", "filebrowser_mc", 1);
fileview = holder_mc.fileview;
viewcombo = holder_mc.viewcombo;
viewlist = holder_mc.viewlist.content1;
item_arr = new Array();

// Event Listenters
viewcombo.addEventListener("change", Delegate.create(this, onViewComboChange));
viewcombo.addEventListener("close", Delegate.create(this, onViewComboClose));
}

private function onViewComboChange(evtObj:Object)
{
trace ("onViewComboChange");
}

private function onViewComboClose(evtObj:Object)
{
trace ("onViewComboChange");
}

}


tanx

pan69
03-11-2007, 12:19 AM
Unless your class is a event dispatcher (of which I see no evidence in your code example) you do not need to initialize it as an EventDispatcher.

The thing with Macromedia v2 components is that they are only ready on the next frame. You should subclass the Macromedia UIObject class (or any of its subclasses) and override the doLater method, which is called on the second frame. Or you need to implement this functionality in your own class.

I've given an example of how to implement your own next frame initializer. To be able to do this you need to extend from MovieClip at some point or use a dummy MovieClip. I choose to make MovieClip the base class in this example:


import mx.controls.*;
import mx.utils.Delegate;

class no.pd.FileBrowser extends MovieClip
{
// View
private var holder_mc:MovieClip;
private var container_mc:MovieClip;
private var fileview:MovieClip;
private var viewcombo:ComboBox;
private var viewlist:MovieClip;
private var item_mc:MovieClip;
private var item_arr:Array;

/**
* FileBrowser : Constructor
*/
public function FileBrowser(target:MovieClip)
{
container_mc = target;
onEnterFrame = initView;
}

/**
* initView : Set the view
*/
private function initView()
{
onEnterFrame = null;

holder_mc = container_mc.attachMovie("filebrowser_mc", "filebrowser_mc", 1);
fileview = holder_mc.fileview;
viewcombo = holder_mc.viewcombo;
viewlist = holder_mc.viewlist.content1;
item_arr = new Array();

// Event Listenters, assuming that viewcombo points to anything valid.
viewcombo.addEventListener("change", Delegate.create(this, onViewComboChange));
viewcombo.addEventListener("close", Delegate.create(this, onViewComboClose));
}

private function onViewComboChange(evtObj:Object)
{
trace ("onViewComboChange");
}

private function onViewComboClose(evtObj:Object)
{
trace ("onViewComboChange");
}
}

_philip_
03-11-2007, 01:18 AM
hoho!! finally i made it :) tanx

insted for useing the enterframe event, i've used setInterval, an that did the stuff :)


like this;


private var initID:Number;

/**
* FileBrowser : Constructor
*/

public function FileBrowser(target:MovieClip)
{
this.container_mc = target;
this.holder_mc = container_mc.attachMovie("filebrowser_mc", "filebrowser_mc", 1);
this.fileview = holder_mc.fileview;
this.viewcombo = holder_mc.viewcombo;
this.viewlist = holder_mc.viewlist;
this.item_arr = new Array();

initID = setInterval(Delegate.create(this, init), 200);
EventDispatcher.initialize(this);
}


/**
* init: Set the view
*/
public function init()
{
clearInterval(initID);

// and her goes the EventListeners
}

pan69
03-11-2007, 04:00 AM
I glad it works for you now. However, I urge you to use the onEnterFrame variant. Your timer solution maight work for you now in this instance it doesn't mean it will work in the next. Your timeout currently happens after 0.2 seconds. What if there is a hickup in the player and it takes longer to render the first frame? The components aren't initialized yet. The onEnterFrame solution always guarentuees that the initialization happens on the second frame. Other then that, timer can be quite resource intensive. Dont use 'm unless you really need to.