A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: [RESOLVED] [ AS3 ] Player class (OOP)

  1. #1
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86

    resolved [RESOLVED] [ AS3 ] Player class (OOP)

    Hi, I'm a rookie at Actionsript 3. I'm trying to make a player class with a parameter to create an object related to the class name that was passed by this parameter in the constructor method.

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;

        public class 
    Player extends MovieClip
        
    {
            private var 
    _player:MovieClip;

            public function 
    Player(n)
            {
                
    _player = new n();
                
                
    createPlayer();
            }

            private function 
    createPlayer():void
            
    {
                
    stage.addChild(_player);
            }
        }

    PHP Code:
    package
    {
        
    import flash.display.MovieClip;
        
    import Player;

        public class 
    Main extends MovieClip
        
    {
            private var 
    _player:MovieClip;

            public function 
    Main()
            {
                
    _player = new Player(LiuKang);
            }
        }


  2. #2
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    I'm fairly certain you have to pass a class name as a parameter. It's been a while for me to do much with as3. This should probably be it for you: https://stackoverflow.com/questions/...ate-from-class
    .

  3. #3
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    I'm trying to make a player class with a parameter to create an object related to the class name that was passed by this parameter in the constructor method
    I see. You did not ask any questions, and the code should work. Maybe you'd see some warnings
    PHP Code:
    package
    {
        
    import flash.display.MovieClip;

        public class 
    Player extends MovieClip
        
    {
            private var 
    _player:MovieClip;

            public function 
    Player(n:Class) // here
            
    {
                
    _player = (new n()) as MovieClip// and here
                
                
    createPlayer(); // this might fail because stage is null - dont call it
                // you might do this.addEventListener(Event.ADDED_TO_STAGE, createPlayer);
            
    }

            private function 
    createPlayer(e:Event null):void // then it has to have arguments
            
    {
                
    stage.addChild(_player);
            }
        }

    Last edited by realMakc; 08-07-2017 at 05:36 PM.
    who is this? a word of friendly advice: FFS stop using AS2

  4. #4
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    You did not ask any questions
    Forgive me, I just forgot.

    Maybe you'd see some warnings
    I was getting the error 1009. I'll test your code, thanks!

  5. #5
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Thanks, your code work.

  6. #6
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Darn, realMakc the link I provided basically supplied the same info. Have fun Mr. Crazy!
    .

  7. #7
    I'm not Crazy :D
    Join Date
    Nov 2015
    Location
    Brazil [<o>]
    Posts
    86
    Quote Originally Posted by swak View Post
    Darn, realMakc the link I provided basically supplied the same info.
    I don't know if I tested it wrong, but the link code you gave me doesn't work.

    Have fun Mr. Crazy!
    Haha OK, thanks

  8. #8
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,834
    Quote Originally Posted by CrazY. View Post
    I don't know if I tested it wrong, but the link code you gave me doesn't work.
    It takes a bit of sorting but the first post was someone giving their code that had problems with answers below. The one with the green checkmark was the one you should look at for the answer. I could have provided the answer but I figured that an example would help in other ways better.
    .

  9. #9
    Member
    Join Date
    Oct 2016
    Posts
    54
    is resolved. i sure they know how use so web site.

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