Friday, May 11, 2012

Design a bouncing ball flash



 To design a bouncing ball  in flash using ActionScript 3.0,

1) Start Flash CS5, click new-> actionscript 3.0, create a rectangle at Center using Rectangle Tool, convert it  to MovieClip with name mb  and the instance name: mybox
2) Create a ball and convert it to a MovieClip with name mc, in linkage select check box: Export to ActionScriot and Export to frame 1, name class as mc.
3) Press F9, input following ActionScript (require mybox  at center)
var mymc:mc=new mc();
mybox.addChild(mymc);
var ball_radius=20;
var maxx:int=mybox.width/2-ball_radius;
var maxy:int=mybox.height/2-ball_radius;
var xflay:Boolean,yflay:Boolean;
var p:uint=1;
setInterval(peng,10);

function peng(){
   
//trace(mymc.y);
    if(mymc.x>maxx-10 || mymc.x<-maxx) xflay=!xflay;
    if(mymc.y>maxy || mymc.y<-maxy) yflay=!yflay;
    if(xflay) mymc.x-=p;
    else mymc.x+=p;
    if(yflay) mymc.y-=p;
    else mymc.y+=p;
}
4) Export flash, below is the result:

No comments:

Post a Comment