A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: [CS3] Migrating AS from 2 to 3

  1. #1
    Senior Member foochuck's Avatar
    Join Date
    Aug 2000
    Location
    Estero, FL
    Posts
    522

    [CS3] Migrating AS from 2 to 3

    I used to be able to use this code in Flash 8 - AS2:

    Code:
    for (i in this) {
        if (this[i] instanceof Button) {
        trace(this[i]._name);
        }
    }
    However in Flash CS3 using AS3 it does not work.

    I need to use AS3 because I want control over the components I'm using.

    Can anyone help me migrate this script from AS2 to AS3?
    Foochuck
    http://www.myspace.com/foochuck

  2. #2
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    Moved to the AS 3.0 forum

    gparis

  3. #3
    Senior Member foochuck's Avatar
    Join Date
    Aug 2000
    Location
    Estero, FL
    Posts
    522
    Sorry about that gparis - I should've double checked.
    Foochuck
    http://www.myspace.com/foochuck

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I believe that the for in loop will only loop over the dynamic properties of an object.

    You probably don't care about all the properties of an object anyway (x, width, etc) and just want to iterate over its children. You can do that with getChildAt.

    Code:
    var kid:DisplayObject;
    for (var i:int = 0; i < numChildren; i++){
      kid = getChildAt(i);
      if (kid is Button){
        trace(kid.name);
      }
    }

  5. #5
    Senior Member foochuck's Avatar
    Join Date
    Aug 2000
    Location
    Estero, FL
    Posts
    522
    I got this error when I tried your script 5Tons...

    1120: Access of undefined property Button.
    Foochuck
    http://www.myspace.com/foochuck

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I assumed you had Button imported since you used it in your original code. To be honest, I don't deal with Buttons usually.

    Regardless, you may need to have a line like this at the top of your script:
    Code:
    import fl.controls.Button;
    I believe SimpleButton may be more common in AS3. In which case you will need something more like

    Code:
    import flash.display.SimpleButton;
    
    //... stuff
    
    
    var kid:DisplayObject;
    for (var i:int = 0; i < numChildren; i++){
      kid = getChildAt(i);
      if (kid is SimpleButton){
        trace(kid.name);
      }
    }

  7. #7
    Senior Member foochuck's Avatar
    Join Date
    Aug 2000
    Location
    Estero, FL
    Posts
    522
    I need to explain further what I'm tryiing to accomplish with this script.

    Basically I have 4 buttons in my movie. They are named b1, b2, b3, & b4.

    When you click and release (onRelease) any buton I want it to take you to a url with the button's name (b1) followed by .php

    I'm trying to do this dynamically so I don't need to create an array with button names in it - I want to have flash to automatically find each button on the stage. Here's a sample of what I was doing with AS2:

    Code:
    for (i in this) {
        if (this[i] instanceof Button) {
    		this[i].myName = this[i]._name;
    		this[i].onRelease = function() {
    			getURL("celeb_" + this.myName + ".php");
    		}
    		this[i].onRollOver = function() {
    		 _parent.stop();
    		 }
    		this[i].onRollOut = function() {
    		 _parent.play();
    		 }
        }
    }
    This code worked great, however now it doesn't work with AS3. I've tried adopting the code you gave me

    Code:
    import flash.display.SimpleButton;
    
    var kid:DisplayObject;
    for (var i:int = 0; i < numChildren; i++){
      kid = getChildAt(i);
      if (kid is SimpleButton){
        trace(kid.name);
      }
    }
    But I haven't been able to get it to work, I get errors everytime...
    Foochuck
    http://www.myspace.com/foochuck

  8. #8
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    EDIT: never mind...
    Last edited by fx.barrett; 04-29-2008 at 07:37 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I must not fully understand your situation. Could you tell us the particular error messages you are getting?

  10. #10
    Senior Member foochuck's Avatar
    Join Date
    Aug 2000
    Location
    Estero, FL
    Posts
    522
    Create a Flash CS3 - with AS3 document.

    On your main timeline add two buttons call them b1 & b2.

    Put this code on the first frame of your movie:

    Code:
    for (i in this) {
        if (this[i] instanceof Button) {
    		this[i].myName = this[i]._name;
    		this[i].onRelease = function() {
    			getURL("celeb_" + this.myName + ".php");
    		}
    		this[i].onRollOver = function() {
    		 _parent.stop();
    		 }
    		this[i].onRollOut = function() {
    		 _parent.play();
    		 }
        }
    }
    You'll see the errors it returns.

    Now, if you switch to a publish setting using AS2, it works fine. However I need to use AS3 because I need to use the CS3 components.

    Does that help at all?
    Foochuck
    http://www.myspace.com/foochuck

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, yeah you'll get errors with that code. That's AS2 not AS3. Do you mean to say you've got that code AND the code I posted? What errors do you get with just the code I posted?

    I don't actually have flash (flashdevelop + code only projects), so I can't recreate your setup exactly.

  12. #12
    Senior Member foochuck's Avatar
    Join Date
    Aug 2000
    Location
    Estero, FL
    Posts
    522
    Basically I'm trying to setup some code that will generate onRelease events for buttons on my stage without having to update arrays and without retyping each onRelease command for each button..I'm trying to do it with loops and by having flash find each button on the stage, grab it's name and loop through those button names to dynamically create the button commands.
    Foochuck
    http://www.myspace.com/foochuck

  13. #13
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    import flash.display.SimpleButton;
    import flash.net.*;
    
    //...
    
    var kid:DisplayObject;
    for (var i:int = 0; i < numChildren; i++){
      kid = getChildAt(i);
      if (kid is SimpleButton){
       kid.addEventListener(MouseEvent.MOUSE_UP, buttonUp);
       kid.addEventListener(MouseEvent.ROLL_OVER, buttonOver);
       kid.addEventListener(MouseEvent.ROLL_OUT, buttonOut);
      }
    }
    
    //...
    
    function buttonUp(event:MouseEvent):void{
      var request:URLRequest = new URLRequest("celeb_"+event.currentTarget.name+".php");
      navigateToUrl(request);
    }
    
    function buttonOver(event:MouseEvent):void{
      stop();
    }
    
    function buttonOut(event:MouseEvent):void{
      play();
    }
    This entirely replaces your as2 code. This code will not find all buttons on stage, only those that are children of the root. But then, your original code did the same thing.
    Depending on where you put the parts of this code, you may have to rearrange some stuff.
    If you get errors, please copy and paste them. It's really hard to just guess what's wrong.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center