This is a blog about Flash by betaruce :-)

Calendar

October 2005
M T W T F S S
« Sep   Nov »
  1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31  

October 25, 2005

AS 3 Experiment: Bouncing balls

Filed under: My Flash Programs — betaruce @ 8:46 pm

just a simple application….several balls of random size and color are moving around and bounce off when they hit the stage border. You need Flash Player 8.5 to view it.

http://www.box-hosting.net/~betaruce/as3/testingAS3.swf

If you cannot view it, pls kindly leave a comment here (I’m testing another new server :) )

here’s the code:

package {
        import flash.util.trace;
        import flash.display.Sprite;
        import flash.display.StageScaleMode;
        import flash.display.Stage;
        import flash.events.*;
        import flash.geom.*;
       
       
        public var sx:int;
        public var sy:int;     
        public var numMC:int=10;
               
        public class MC extends Sprite{
               
                public var vx:Number=8+7*(Math.random()*2-1);
                public var vy:Number=8+7*(Math.random()*2-1);
               
                public function MC(){
                        graphics.beginFill(uint(255*Math.random())< <16|uint(255*Math.random())<<8|uint(255*Math.random()));
                        graphics.lineStyle(1,0);
                        graphics.drawCircle(0,0,10+40*Math.random());
                        x=Math.random()*sx;
                        y=Math.random()*sy;     
                        addEventListener(EventType.ENTER_FRAME,onEnterFrame);
                }
               
                private function onEnterFrame(e:Event){ 
                        if(x<=width/2){x=width/2; vx*=-1;}
                        if(x>=sx-width/2) {x=sx-width/2; vx*=-1;}
                        if(y< =width/2) {y=width/2; vy*=-1;}
                        if(y>=sy-width/2) {y=sy-width/2; vy*=-1;}
                        x+=vx;
                        y+=vy;
                }
        }
       

        public class testingAS3 extends Sprite {
                       
                public function testingAS3() {
                        stage.scaleMode = StageScaleMode.SHOW_ALL;
                        stage.align=‘TOP_LEFT’;
                        sx=stage.stageWidth;
                        sy=stage.stageHeight;
                        this.graphics.lineStyle(0,0);
                        this.graphics.beginFill(0x444444,100);
                        this.graphics.drawRect(0,0,sx,sy);
                        this.graphics.endFill();
                        for(var i:int=0;i){
                                var temp:Sprite=new MC();
                                stage.addChild(temp);
                        }       
                }

        }
}

One interesting finding is that for the following part:

graphics.drawCircle(0,0,10+40*Math.random());
x=Math.random()*sx;
y=Math.random()*sy;

the x and y parameters of drawCircle must be set to zero in order for the code to work properly as it is now, or else it seems cannot detect the correct stage border. Therefore I need to random the the ball position in the next 2 lines.

not sure why is that so, hope I will know more later.

• • •
Powered by: WordPress • Template by: Priss