Saturday, June 30, 2012

[Action Script] Efficient ActionScript 3.0 June,2012

I use FlashDevelop 4.01 to write small ActionScript projects and have messed around with Mobile Adobe Air ActionScript 3.0 Apps. My question is for anyone who would like to answer, for them to share experience, or to help point me in a better direction. Here is a small code example of a Pong type ball bouncing around a 500 x 500 px screen.

Main.as -
Code: package
{
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.display.StageScaleMode;
        import flash.display.StageAlign;
       
        [SWF(width="500", 
                height="500",
                frameRate="30", 
                backgroundColor="#000000", 
                pageTitle="AdvEngine - Flash Version 1.0")]
        public class Main extends Sprite
        {
                public static const WIDTH:uint = 500;
                public static const HEIGHT:uint = 500;
               
                private var ball:Ball = new Ball();
               
                public function Main():void
                {
                        addEventListener(Event.ADDED_TO_STAGE, init);
                        addEventListener(Event.ENTER_FRAME, update);
                        addChild( ball );
                }
               
                private function init(e:Event = null):void
                {
                        removeEventListener(Event.ADDED_TO_STAGE, init);
                        stage.scaleMode = StageScaleMode.NO_SCALE;
                        stage.align = StageAlign.TOP_LEFT;
                }
               
                private function update(event:Event):void {
                        ball.update();
                }
               
        }
       
}
Ball.as -
Code: package
{
        import flash.geom.Point;
        import flash.display.Bitmap;
        import flash.display.BitmapData;
       
        public class Ball extends Bitmap
        {
               
                private var fp:Point        = new Point(0, 0);
                private var dx:int                = 5;
                private var dy:int                = 4;
               
                public function Ball()
                {
                        super( new BitmapData( 10, 10, false, 0xffffff ) );
                        cacheAsBitmap = true;
                }
               
                public function update():void {
                       
                        fp.x = x + dx;
                        fp.y = y + dy;
                       
                        // test horz boundaries
                        if ( fp.x < 0 ) {
                                fp.x = 0;
                                dx *= -1;
                        }else if ( Main.WIDTH < (fp.x + width) ) {
                                fp.x = Main.WIDTH - width;
                                dx *= -1;
                        }
                       
                        //test vert boundaries
                        if ( fp.y < 0 ) {
                                fp.y = 0;
                                dy *= -1;
                        }else if ( Main.HEIGHT < (fp.y + height) ) {
                                fp.y = Main.HEIGHT - height;
                                dy *= -1;
                        }
                       
                        x = fp.x;
                        y = fp.y;
                       
                }
        }

}When the movie starts up it is choppy, for maybe the first 3 seconds, then it evens out, but continues to look choppy every now and then. Changing the frame rate to 60 instead of 30 makes it move without any choppy-ness on the computer, but for some reason my phone won't allow more than 35 - 40 fps. I thought using Bitmaps and cacheAsBitmap and cacheAsBitmapMatrix would be the best options I had for hitting that frame rate each time, but it still seems choppy. I thought 30 would be more than enough for solid fps.

- Is there a way to give more initial memory to the application? Would this help?

- Is there a way to push rendering to the GPU effectively instead of CPU ( I know the application.xml file in the mobile development has a tag <renderMode> which can be changed to gpu, but this has always slowed my application to 19 - 22 fps...

- Any ideas would be awesome. Or let me know if the above code runs perfectly smooth for you.

Thanks ahead of time
Efficient ActionScript 3.0

Related Post



0 comments: