-
Referencing a var within a dynamically created button via an attached function
I'm having a issue using a var within a button.
I create the button and add the var to the to button:
addChild(answerButton);
answerButton.thisX = ii; // temp value that changes with each addchild
The Question is how do I get the buttons to trace (to start with) the value of the var contained within them?
So easy in as 2
Here is my code below:
PHP Code:
//matchingSetUp.as
package {
import flash.display.Sprite;
import flash.text.*;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class matchingSetUp extends Sprite {
private var choiceButton:choice;
private var allAnswers:Array;
private var answerButton:answer_box;
private var tempString;//my movieclip name
private var myThis:MovieClip;//my movieclip
private var sortMethods:Array = [1,2,8];// 3 diverse types of sorts
var sortInt:int=0;
private var _matchingObj:Object;
public function matchingSetUp(_matchingObj) {
init(_matchingObj);
}
private function init(_matchingObj) {
// setting up drop zones
for (var i:int=0; i < _matchingObj.problemCodes.length; i++) {
choiceButton=new choice ;
addChild(choiceButton);
choiceButton.x=_matchingObj.targetsX;
choiceButton.y=_matchingObj.targetsY[i];
choiceButton.name="mc" + _matchingObj.problemCodes[i];
}
//setting up Correct answer buttons
//// putting all answer into 1 array
allAnswers = _matchingObj.choiceBank;
for (var k:int = 0; k< _matchingObj.extraChoices.length; k++) {
allAnswers.push(_matchingObj.extraChoices[k]);
}
//// pseudo-randomizing the answers boxes based on sorts
sortInt = (Math.random() * 3);
allAnswers.sort(sortMethods[sortInt]);
//// adding the boxes to the stage
for (var ii:int=0; ii < allAnswers.length; ii++) {
answerButton=new answer_box ;
addChild(answerButton);
answerButton.x=757;// this needs to be in the fla struct
if (ii == 0) {
answerButton.y=108;// this needs to be in the fla struct
} else {
tempString =allAnswers[ii-1];
myThis=answerButton.parent.getChildByName(tempString) as MovieClip;
answerButton.y=myThis.y + 29;
}
answerButton.name=allAnswers[ii];
answerButton.thisX = ii;
trace(answerButton.thisX); myThis=answerButton.parent.getChildByName(answerButton.name) as MovieClip;
myThis.my_name.text = allAnswers[ii];
answerButton.addEventListener(MouseEvent.CLICK,myMouseDown2);
}
}
private function myMouseDown2(event:MouseEvent):void {
trace(thisX); // how do I access the var here ??????????????????????????
}
}
}
-
PHP Code:
private function myMouseDown2(e:MouseEvent):void {
trace(e.target.name);
}
I don't know where your headed, but if you name or object like answerButton.name = "BOB"; if you use the method above, it will trace the name.
-
Your eventHandler function is receiving an Event object ("event"), which in turn has a property called "target" that will be a reference to the object that dispatched the event. So in this case event.target is the same object as answerButton. To trace out the value try:
trace(event.target.thisX);
-
One snag - I just realized you're using a custom class...if thisX is a publicly defined variable (instead of a dynamically added property), you'll need to hard-cast the event.target so the compiler knows that it can expect to find the thisX param...that would look like this:
trace( answer_box(event.target).thisX );
-
Thanks for the fast responce!
One thing about this method:
trace( answer_box(event.target).thisX );
Is giving me this error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.text::TextField@10857299 to answer_box.
at matchingSetUp/myMouseDown2()
My guess would be because there is a text field within the MC. I tried to move the field behind the shape in the MC but still get the error.
-
This code produces the same error:
trace(event.target.thisX);
Both work when you click an area on the Mc that the text box is not on but if you click the text box within the Mc . . . error
-
Well, I cheated for now:
trace(event.target.parent.thisX);
Is there another way to avoid this event nested text field error?
-
You could try event.currentTarget, or you could set the parent object to mouseChildren=false
-
Cool, I like this one better: event.currentTarget
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|