PDA

View Full Version : onPress Event Dispatching


MJWarren@Syr
07-11-2005, 02:38 PM
I just can't seem to get this event dispatching to work. I need to have my custom button component broadcast an event to all movie clips on the stage. Then I need to have my custom textbox component listen to the event and complete an action. this is what I have so far.

//button class
for(var name in this._parent) {
if(this._parent[name] instanceof ButtonClass3 ) {
this._parent[name].onPress= function() {
dispatchEvent({type: 'myevent', target: this});
};
}
}

//textbox class
buttonInst = new ButtonClass3();
function obj(evtobj) {
trace("ok");
setText("hi");
}
buttonInst.addEventListener("myevent", obj);

any help would be cool

-ashy larry

sleekdigital
07-11-2005, 03:06 PM
I'm sorry, but this code is not making any sense to me. Maybe it would help if you showed more of the context of the code.

for(var name in this._parent) {
if(this._parent[name] instanceof ButtonClass3 ) {
this._parent[name].onPress= function() {
dispatchEvent({type: 'myevent', target: this});
};
}
}


Why would you do this in the button class? Why don't you just make an onPress method in the class .. then all instances of the button will have that onPress handler.


Also, instead of this ...

//textbox class
buttonInst = new ButtonClass3();
function obj(evtobj) {
trace("ok");
setText("hi");
}
buttonInst.addEventListener("myevent", obj);


Try this ...

//textbox class
buttonInst = new ButtonClass3();
function myevent(evtobj) {
trace("ok");
setText("hi");
}
buttonInst.addEventListener("myevent", this);

MJWarren@Syr
07-11-2005, 03:42 PM
here is the rest of the button component class

[Inspectable (defaultValue="unchecked", type=String)]
public function set But(ch:String):Void {
if(ch=="checked") {
this.ischecked=true;
}
}

[Inspectable (defaultValue="Enter Value", type=String)]
public function set Value(val:String):Void {
_text=val;
}


public function ButtonClass3() {
EventDispatcher.initialize(this);
ok = new MikeClass();
init();
}

private function init() {
//stops movie clip at frame 1
this.stop();

//Nested Loops to deal with onPress
for(var name in this._parent) {

if(this._parent[name] instanceof ButtonClass3 ) {

this._parent[name].onPress= function() {


for(var name in this._parent) {

if(!this._parent[name].ischecked) {
this._parent[name].gotoAndStop(2);
//art of checked box is on frame 2
this._parent[name].ischecked=true;
tempName=this.tempNum;


}
else {
this._parent[name].gotoAndStop(1);
this._parent[name].ischecked=false;
}

}

for(var name in this._parent) {

if(this._parent[name].tempNum!=tempName) {
this._parent[name].gotoAndStop(1);
this._parent[name].ischecked=false;
}

}

if(this.ischecked) {
//ok.declare();
//this.pressed=true;
trace(this._name);
dispatchEvent({type: 'myevent', target: this, message:'hey man'});
}


};

}

}

}
}

then depending on which button is pressed different text needs to appeaer in the textbox, this part shouldnt be that difficult if only i could get this dispatch to work. thank you for taking a look at it

sleekdigital
07-11-2005, 03:47 PM
Ok, my above response still holds true then. Try those suggestions, and let us know how it goes.

MJWarren@Syr
07-11-2005, 04:53 PM
im not sure if i completely understand your suggestion about the onPress method. I implemented your second suggestion and still have no success. After trying a much simplier example and tracing the hell out of that I think part of my problem might be in the "target" property of the dispatch object. so im doing more resaerch on that now. thanks again.

-tyrone biggums

sleekdigital
07-11-2005, 04:55 PM
In your code, for some reason, you are going to the parent of the button instance, and looking for all other button instances, then assigning the onPress handler to them. This seems very silly to me, maybe I'm missing something??? I would just put an onPress method in the class, and all instances will then have that onPress handler.

MJWarren@Syr
07-11-2005, 06:26 PM
i have to do this because only one button can be pressed at one time. if i drag three instances of this one component to the stage the first one will be checked by default, if i then click the third button the first becomes unchecked and the third becomes checked. so in order to do this the component has to detected the other buttons on the stage and be able to tell if any other button is checked. maybe there is a more efficient way of doing it?

sleekdigital
07-11-2005, 06:38 PM
ohhhh, sounds an awful lot like a radio button to me? Macromedia already provides this sort of functionality with the radio button component... you use the "groupName" parameter. If you really want to make your own, you might at least get some ideas from their code.

If I were to do it, I would probably have a seperate class that manages the group of buttons rather than building that functionality into the button class itself.

MJWarren@Syr
07-11-2005, 06:53 PM
yeah its pretty much exactly like a radio button. but im making my own so new graphics can easily be put in and out of it without having to mess with the skins. i also have to build the functionality into itself because the buttons are going to get reused in many different projects with different number of buttons and such. i agree that it could be done much simplier but unfortunately its not up to me. do you know where i could look at macromedias code for there radio buttons?

-tron

sleekdigital
07-11-2005, 07:12 PM
Just because you make a seperate class does not mean it won't be reusable. On the contrary, that architecture would probably make things more flexible and reusable. It appears that this is in fact how Macromedia approaches it. If you look in your Flash install folder and drill down to ... \en\First Run\Classes\mx\controls You will see a RadioButton AND a RadioButtonGroup class. Good Luck

MJWarren@Syr
07-11-2005, 08:20 PM
thank you for your help and advise, much appreciated

-charlie murphy