AS 3 Experiment: Bouncing balls
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(0×444444,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.
[…] from JPEG (source code and example) Msn Messenger in Flex 2.0…Almost!?!?!? Security??? AS 3 Experiment: Bouncing balls Betaruce (source and example) AS 3 Experiment: Animated Plas […]
Pingback by Franto.com Flash blog » Collected links to ActionScript 3.0 examples — October 26, 2005 @ 4:09 pmHere’s the one to beat: 250.000 REAL bouncing balls
http://www.bravia-advert.com/gallery/gallery02.html
J
Comment by Jensa — October 26, 2005 @ 6:47 pmpushing Flash to the limit
Comment by betaruce — October 26, 2005 @ 7:24 pmCant see your flash movies dude.
Comment by Bob — November 4, 2005 @ 10:26 pmdo you have Flash Player 8.5? or is it my server problem?
Comment by betaruce — November 4, 2005 @ 10:46 pmI can’t view it! I just got the flash player 15 mins. ago!
Comment by you — January 2, 2006 @ 7:47 amI’m not sure I got the .5 version though. Leave a Link!
http://labs.macromedia.com/technologies/flashplayer8_5/
pls leave a comment if you still cannot see it
Comment by betaruce — January 2, 2006 @ 8:47 amRealmente tengo gusto de este Web site. Usted todo ha hecho un buen trabajo.
Comment by Danald — June 13, 2006 @ 7:35 amThat is the nicest ,coolest
Comment by lenny — August 25, 2006 @ 6:46 pmthing I’ve heard for a long time. I really appreciate your work. I’ll share it
with my friends.
http://www.box-hosting.net/~betaruce/as3/testingAS3.swf
Comment by Mak 000001 — January 29, 2007 @ 4:24 pmnot going anywhere.
That is the nicest ,coolest thing. Thanks.
Comment by ball — March 2, 2007 @ 11:52 amyes it is very coolest thing for flesh player but their
Comment by jack — March 27, 2007 @ 3:08 pmis ans other player which was introduce in 2006 was
(youtube) which is getting success day by day and i
think it is great part of internet.