It's all in the name - Instance Name Function


/ Published in: ActionScript 3
Save to your folder(s)

This uses the button's instance name to assign functions. Instead of using a bunch of event listeners, you can use one function to delegate actions according to the button's instance name.


Copy this code and paste it in your HTML
  1. // Assumes that the following buttons with instance names are on the stage
  2. // shape1, shape2, shape3, shape4, shape5
  3. // bg1, bg2, bg3, bg4, bg5
  4. // font1, font2, font3, font4, font5
  5. // color1, color2, color3, color4, color5
  6. //
  7. // This goes thought all the children and cleans out the generic "instance" named children and assigns the rest a click listener
  8. for (var i:int = 0; i < numChildren; i++) {
  9. var tempName:String = getChildAt(i).name.replace(/\d/g,'');
  10. if (tempName != "instance") {
  11. getChildAt(i).addEventListener(MouseEvent.CLICK, myButton);
  12. }
  13. }
  14. // Once clicked, this function uses the variables in the name to assign actions
  15. function myButton(evt:MouseEvent):void {
  16.  
  17. var goToFrame:int = evt.currentTarget.name.replace(/\D/g,'');
  18. var tempName:String = evt.currentTarget.name.replace(/\d/g,'');
  19.  
  20. switch (tempName) {
  21. case "shape" :
  22. trace(1,tempName,goToFrame);
  23. //shapeMC.gotoAndStop(goToFrame)
  24. break;
  25. case "bg" :
  26. trace(2,tempName,goToFrame);
  27. //bgMC.gotoAndStop(goToFrame)
  28. break;
  29. case "font" :
  30. trace(3,tempName,goToFrame);
  31. //fontMC.gotoAndStop(goToFrame)
  32. break;
  33. case "color" :
  34. trace(4,tempName,goToFrame);
  35. //colorMC.gotoAndStop(goToFrame)
  36. break;
  37. }
  38. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.