A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] AS3 Classes multiple radio buttons

  1. #1
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971

    resolved [RESOLVED] AS3 Classes multiple radio buttons

    Hello,

    i'm working with classes for first time, i didn't even worked with it in AS2.

    I have a class named radio.as and it has this script:
    package
    {

    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    public class radio extends MovieClip
    {
    public var amount:int;

    public function radio()
    {
    // constructor code

    }

    public function create_Buttons(amount:int):void
    {
    for (var i:int = 1; i<=amount; i++)
    {
    trace("Creating radio button"+i);
    var new_r:radiob = new radiob();
    addChild(new_r);
    new_r.buttonMode = true;
    new_r.x = 100;
    new_r.y = 150+ i* (new_r.width + 6);
    new_r.addEventListener(MouseEvent.CLICK, getValues);
    }
    }

    function getValues(e:MouseEvent):void
    {
    trace(e.target.name);
    e.target.buttonMode = false;
    }

    }


    }
    Then in the first frame of the main timeline:
    var j:radio = new radio();
    j.create_Buttons(5);
    And the radio button movieclip has a class name at the library of "radiob".

    It is kinda working, because the create_Buttons(); function is fired 5 time as it should, but no movieclip is placed on the stage.

    Any help will be appreciated.
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    It's often a good idea to post the error you were getting too, as it narrows it down.

    You were probably receiving this error code
    Code:
    Error: Error #2136: The SWF file file:///**********radio.swf contains invalid data.
    	at radio/frame1()
    Anyway, replace this
    PHP Code:
    var j:radio = new radio();
    j.create_Buttons(5); 
    with
    PHP Code:
    create_Buttons(5); 
    you don't need to create a new class directive as you are currently inside of the one you are linked to, you would only need to call the first code if you were calling the function from another class file.

  3. #3
    Senior Member
    Join Date
    Nov 2001
    Posts
    1,145
    If you haven't tried using standard classes yet...

    Open a new AS3 fla, save it to a folder, click on the "edit class definition" pencil, name the class "Main" (Main is the name of your main class, use uppercase first letter for classes), a new class named Main will be created, save it to the same folder as the fla.

    Main function gets called first. You can create other classes, put them in the same folder as main, and use them in Main. In Flash, create a New Actionscript3 Class, name it uppercase first - MyButton, save it to same folder as Main.as. Then in Main you can do: myButton:MyButton = new MyButton();

    You can put the create_buttons function under the Main function and call it from Main().

    Make everything private until you need to make it public.

  4. #4
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Thanks both for the tips, i know this will be very useful, not only for me, but for every one anyone that reads this thread.

    Basically, i don't have an issue with the class itself. The class is being loaded, and the create_Buttons function is fired 5 times, so i shall suppose the class is working. The problem is, the addChild method inside the create_Buttons function, is not adding the childs on the stage. That's weird because I have another project I helped someone with, and i loaded buttons and got it working perfectly. I will read your comments to see if I missed something.

    Thanks!
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  5. #5
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    And fruitbeard, thanks for reminding me to specify if I get some error, that should be a good practice we all should get used to.

    But in this case, i am not getting any error message. The only issue here is that the create_Buttons function, is actually running, but doesn't adding the childs on the stage. Stage is empty. Weird.
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

  6. #6
    Designer, Programmer, Musician angelhdz's Avatar
    Join Date
    Mar 2010
    Posts
    971
    Solved. You both helped me big time. I forgot to add the class name in the Properties Panel > Class field.

    And i changed the script from
    var j:radio = new radio();
    j.create_Buttons(5);
    to

    create_Buttons(5);
    And now its working. I will keep practicing the Classes because this is a new world for me. Thanks
    Already mastering AS3. It was so fun. Now into Javascript and PHP and all its libraries

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