A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS3 object array to class

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    1

    AS3 object array to class

    I am new and having an issue with the use of classes in as3. I am working on a platform engine. I am trying to create a function that will dynamically create a number of badguys. I have created an array of objects in my main timeline

    Actionscript Code:
    function badPlayer()
        {
            var bads:Array = new Array();
            for (var i=0; i<5; i++)
            {
                var mc = new bman();
                mc.name=["mc"+i];
                bads.push(mc);
                _backGround.addChild(mc);
                mc.x = 100; //<---this will be a random number eventually lol
                mc.y = 100;
                trace (bads);
                Baddies(_backGround.mc); //here I am trying to export mc to my class
            }
        }
    badPlayer()

    Here is a snip-it from my class. My trace statement wont even output.

    Actionscript Code:
    public class Baddies extends MovieClip
    {
        private var pistolSound:pistolShot = new pistolShot();
        //private var mc = new mc();
        private var _rotateSpeedMax:Number = 2;
        private var _gravity:Number = .68;
        private var _bulletSpeed:Number = 2;        
        private var _maxDistance:Number = 200;
        private var _reloadSpeed:Number = 500; //milliseconds
        private var _barrelLength:Number = 20;
        private var _bulletSpread:Number = 5;
        private var _isLoaded:Boolean = true;      
        private var _isFiring:Boolean = true;
        private var _endX:Number;
        private var _endY:Number;
        private var _startX:Number;
        private var _startY:Number;
        private var _reloadTimer:Timer;
        private var _bullets:Array = [];
        private var _gun:MovieClip;
        private var _enemy:MovieClip;
        private var _yx:Number;
        private var _yy:Number;
        private var _pcos:Number;
        private var _psin:Number;
        private var _trueRotation:Number;
        public function Baddies()
        {
            trace("working");
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }

    Basically I am trying to create several bad guys (bman) and have the same code (Baddies) apply to each of them. I have also tried to change the linkage name of bman to Baddies with no success.

    Baddies is an AS file that should direct each badguy to aim and shoot at the player

    This is the 1st game I have done where I am actually using external AS files and I am so confused. If I had just coded this whole game on the main timeline I would probably be wrapping up the game by now.

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    Normally, you would have Baddies.as create an instance of bman so that all you have to do is create a series of Baddies instances.

    If you want to create instances of bman yourself and pass them to instances of Baddies, you should require it in Baddies' constructor like this:

    Baddies.as
    Code:
    public class Baddies extends MovieClip
    {
        private var pistolSound:pistolShot = new pistolShot();
        //private var mc = new mc();
        private var _rotateSpeedMax:Number = 2;
        private var _gravity:Number = .68;
        private var _bulletSpeed:Number = 2;        
        private var _maxDistance:Number = 200;
        private var _reloadSpeed:Number = 500; //milliseconds
        private var _barrelLength:Number = 20;
        private var _bulletSpread:Number = 5;
        private var _isLoaded:Boolean = true;       
        private var _isFiring:Boolean = true;
        private var _endX:Number;
        private var _endY:Number;
        private var _startX:Number;
        private var _startY:Number;
        private var _reloadTimer:Timer;
        private var _bullets:Array = [];
        private var _gun:MovieClip;
        private var _enemy:MovieClip;
        private var _yx:Number;
        private var _yy:Number;
        private var _pcos:Number;
        private var _psin:Number;
        private var _trueRotation:Number;
        private var _bmanSymbol:bman;
        public function Baddies(bmanSymbol:bman) 
        {
            _bmanSymbol = bmanSymbol;
            trace("working");
            stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
        }
    Then in the body of Baddies.as, any time you need to manipulate the bman instance, you can just refer to _bmanSymbol.

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It looks like you're having trouble with the basics of what a class is and what it's for. A class is not a procedure. It should not, generally, merely process and spit back out other instances. A class is a template for a kind of object. Baddies is a bad class. Baddy would be better. Baddy would contain all the smarts for an individual baddie. You would then create a bunch of those Baddy instances and put them in an array and on the display.

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