Frequently when playing around with code I need an object to play around with. So I created a Ball.as class. This testing class for Actionscript 3 is useful because it will just create a ball, with a default size and colour, which I can override if I wish.
I have also given it a bounceEffect() and bounce() public methods.
These will give a bounce effect when it hits a boundary and the latter will make it move and bounce from the boundaries. The boundary at the moment is just stage.stageWidth and stage.stageHeight, but I plan on allowing an object to be sent so that the instance can use its hight and width as the boundaries for bouncing.
There are also some getters and setters dealing with colour and radius, etc.
To stop the movement I created a haltObject() public function.
I just threw this together in a few minutes but I find it very useful so I thought I would share it with everyone.
Here is an example of its use…
var myball:Ball = new Ball();
addChild(myball);
myball.bounceEffect();
myball.bounce();
If anyone finds any issues, please let me know and I will update it, and if you find it useful let me know as well. I am sure that it is not perfect
Any thoughts on improvements and additions I will also be happy to receive.
Also note that i have zipped up the file as a Flash Builder project, so that you can just import the whole project if you use Flash Builder. Otherwise just go to the src folder and dig out Ball.as