flash on 2011-12-10

by 3f5
♥0 | Line 65 | Modified 2011-12-10 10:11:32 | MIT License
play

ActionScript3 source code

/**
 * Copyright 3f5 ( http://wonderfl.net/user/3f5 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/xB0X
 */

package {
    import flash.events.Event;
    import flash.display.Sprite;
    
    [SWF(width = "465", height = "465", frameRate = "30")]
    public class FlashTest extends Sprite {
        public function FlashTest() {
            addEventListener(Event.ENTER_FRAME, function():void {
                if (stage.stageWidth > 0)
                {
                    removeEventListener(Event.ENTER_FRAME, arguments.callee);
                    init();
                }
            });
        }
        
        private function init():void {
            var display:Sprite = new Sprite();
           
            display.width = 465;
            display.height = 465;
            
            addChild(display);

            var unko:UnkoSprite = new UnkoSprite();

            unko.graphics.beginFill(0xFF0000);
            unko.graphics.drawCircle(0, 0, 10);
            unko.graphics.endFill();

            display.addChild(unko);

            Wonderfl.log(stage.stageWidth);
        }
    }
}
import flash.events.Event;
import flash.display.Sprite;

class UnkoSprite extends Sprite {
    public var gravity:Number = 0.98;
    public var drag:Number = 0.8;
    public var aX:Number = 5;
    public var aY:Number = 4;

    public function UnkoSprite() {
        super();
        
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    private function onEnterFrame(event:Event):void {
        if (!parent) {
            return;
        }

        if (x + width >= parent.width) {

            x = parent.width - width;
            aX *= -1 * 0.8;
        }
        if (x <= 0) {
            x = 0
            aX *= -1 * 0.8;
        }
        if (y + height > parent.height) {
            y = parent.height - height;
            aY *= -1 * 0.8;
        }
        if (y <= 0) {
            y = 0;
            aY *= -1 * 0.8;
        }

        aX *= drag;
        aY = aY * drag + gravity;

        x += aX;
        y += aY;
    }

}