AS3 dynamic btn scripts
- Started
- Last post
- 8 Responses
- CyBrain
Before I was hobbled by AS3, I use to put button scripts in a for loop so I could write one script for many buttons at one time. Can I still do this in AS3?
Here's my example.
function mainRollOver1 (event:MouseEvent):void {
if (currentMain != "main1") {
Tweener.addTween(main1_txt, {_color:0xFFF200, time:theDuration, transition:"easeOutQuart"});
}
}Could I set up a total of 6 buttons in a for loop like this? (I don't know how to express the name of the function here)
for (var i:Number; i <= 6; i++) { this["main" + i + "_txt"].buttonMode = true; this["main" + i + "_txt"].addEventListener(MouseEvent.ROLL_OVER, mainRollOver1); function <dynamic function name> (event:MouseEvent):void {
if (currentMain != "main" + 1) {
Tweener.addTween(this["main" + i + "_txt"], {_color:0xFFF200, time:theDuration, transition:"easeOutQuart"});
}
}Thanks in advance.
- Pupsipu0
ugh, yea, you can even do it properly with classes.
- CyBrain0
I might be better off writing a rollover, rollout, and click function for 6 buttons than trying to remember to import all the crap I would need to get a separate .as file to work, but I'll give it a shot.
- acrossthesea0
I'm working on an example for you. This is less complicated than you probably think.
- acrossthesea0
http://pastebin.com/f39422ea8
Here's a simple way to assign functionality to 6 movieclips. You can take it a step further and have the loop create and position the actual movieclips too.- Inside each buttonX movieclip is a symbol with the instance name of buttonBase and it's getting tweened on rollover/outacrossthesea
- CyBrain0
That looks exactly like what I was trying to do. Megathanks!
- acrossthesea0
No problem. If you have any other questions feel free to ask. I'm no programmer, but I know enough AS3 to get by.
- As far as I'm concerned, anyone who is decent in AS3 is a programmer.CyBrain
- cuke4260
it's a little more work upfront, ultimately it's way better/faster.