Search code examples
actionscript-3flasheventsdata-objects

get property of a dataobject in refernce to other property of same object in as3


how to get the speed property of 'smokeobject' inside a enter_frame listener 'animatesmoke' in reference to the movieclip 'whitesmoke' which is also a property of 'smokeobject

here is my code

public function createRocketSmoke() 
    {
        var smokeObject:Object = new Object();
         smokeObject.whiteSmoke = new Bitmap(new WhiteSmoke(0,0));
         smokeObject.whiteSmoke.x=targetX + Math.random()*4-8;
         smokeObject.whiteSmoke.y=targetY + Math.random()*4-8;

        smokeContainer=new MovieClip();
        smokeContainer.addChild(smokeObject.whiteSmoke);
        addChild(smokeContainer);

        var randomScale = Math.random();
        if(randomScale<.5)
        randomScale = randomScale+.5;
        smokeObject.whiteSmoke.scaleX= randomScale;
        smokeObject.whiteSmoke.scaleY= randomScale;
        smokeObject.speed = Math.random();
        smokeObject.whiteSmoke.rotation = Math.random()*360;
        smokeObject.whiteSmoke.addEventListener(Event.ENTER_FRAME,animateSmoke);    

    }
    public function animateSmoke(event:Event):void
    {
        //here i want the speed property of the 'smokeObject'
    }

Solution

  • What you're trying to do is going to get extremely messy very quickly.

    I suggest creating a class for your Smoke and giving it an update() function:

    public class Smoke
    {
    
        // Properties.
        private var _whiteSmoke:Bitmap;
        private var _speed:Number = 0;
    
    
        // Constructor.
        public function Smoke(targetX:Number = 0, targetY:Number = 0)
        {
            // Prepare graphics.
            _whiteSmoke = new Bitmap(new WhiteSmoke());
            _whiteSmoke.x = targetX + Math.random()*4-8;
            _whiteSmoke. y = targetY + Math.random()*4-8;
            _whiteSmoke.rotation = Math.random()*360;
            _whiteSmoke.scaleX = whiteSmoke.scaleY = 0.5 Math.random() * 0.5;
    
            // Apply random speed.
            _speed = Math.random();
        }
    
    
        // Update this smoke.
        public function update():void
        {
            //
            // Update this smoke here.
            //
        }
    
    
        // The graphics for this smoke.
        public function get whiteSmoke():Bitmap
        {
            return _whiteSmoke;
        }
    
    
        // The speed for this smoke.
        public function get speed():Number
        {
            return _speed;
        }
    
    }
    

    This will allow you to more easily update your smoke objects with an array and a loop to update each of them:

    // This array will hold all your Smoke objects.
    var allSmoke:Array = [];
    
    // Create some Smoke.
    var smoke1:Smoke = new Smoke(10, 10);
    var smoke2:Smoke = new Smoke(10, 10);
    
    // Add it to the array.
    allSmoke.push(smoke1);
    allSmoke.push(smoke2);
    
    
    // Prepare game loop.
    addEventListener(Event.ENTER_FRAME, update);
    function update(e:Event):void
    {
        // Loop through smoke objects and update them.
        for each(var i:Smoke in allSmoke)
        {
            i.update();
        }
    }