PDA

View Full Version : AS3: updating dynamic textfield with different variables on MouseOver


apprentice
06-28-2010, 03:52 PM
Could someone be so kind as to lend a hand to me with AS3? I have 5 buttons on a simple game menu where I'm trying to change the content in a dynamic textfield when mousing over any of the buttons. I've managed to populate the textfield with a single variable, but if I want to add the other buttons, do I have to create new function names for each one that target the same textfield? I guess it feels cumbersome with so many lines of code, whereas in AS2 it could be done with a few lines of code without having to specify a function name for each mouse event:

btn2.on(rollOver) {
myText = "Activity 2";
}
-----------------------------------------------------------------
AS3 code:
-----------------------------------------------------------------
var myText:TextField = new TextField();
myText.text = ""; //start out empty
myText.type = TextFieldType.DYNAMIC;
addChild(myText);

btn1.addEventListener(MouseEvent.MOUSE_OVER, btnMouseOver);

function btnMouseOver(event:MouseEvent):void {
myText.text = "Activity 1";
}

Noct
06-28-2010, 05:27 PM
You can use one function for everything, you just need to pass identifiers to it. I'd probably create a class for these buttons and do all of this data parsing in there, but it you wanted to do it all on the timeline, you could do something like this:

var myText:TextField = new TextField();
myText.text = "";//start out empty
myText.type = TextFieldType.DYNAMIC;
addChild(myText);
//
var btnArr:Array = new Array(btn1,btn2,btn3,btn4,btn5);
var textArr:Array = new Array("btn1 rolledOver","btn2 rolledOver","btn3 rolledOver","btn4 rolledOver","btn5 rolledOver");
//
for (var i:int = 0; i<btnArr.length; i++) {
var cB = btnArr[i];
cB.idNum = i;
cB.addEventListener(MouseEvent.MOUSE_OVER, btnMouseOver);
}
//
function btnMouseOver(event:MouseEvent):void {
var msgTxtStr:String = textArr[event.target.idNum];
myText.text = msgTxtStr;
}

apprentice
06-28-2010, 06:26 PM
Noct, thanks for the quick reply and for the help! In terms of creating a class, would all of the custom variable strings be stored within that class, or would it need to be stored elsewhere? Hope I'm even making sense here. In any case, thanks for the big help!

app