Ononmin Flash prototype – step 2
- December 26, 2009 by Emanuele Feronato
- Filed under Actionscript 3, Flash, Game design | 1 Comment
I added some features to the Ononmin Flash prototype I showed you some days ago.
Now the mushroom moves along the x axis in a field with a customizable number of bad circles and can fire a bullet. At the moment the bullet keeps on flying until it reaches stage’s edges, then it’s removed and the mushroom can shoot another bullet.
In order to keep classes and variables well organized, I changed some class names. These are the new classes/objects:
ball_mc: the rotating cannon
bullet_mc: the bullet fired by the cannon
field_mc: the battlefield
madball_mc: the spheres your bullet can’t touch
mushroom_mc: the moving mushroom
onon_mc: the game itself
The main class is obviously onon.as that simply creates a battlefield:
1 2 3 4 5 6 7 8 9 10 11 | package { import flash.display.Sprite; public class onon extends Sprite { // field_mc has a parameter determining the number of big circles // on the stage public var field:field_mc = new field_mc(15); public function onon():void { addChild(field); } } } |
field_mc.as manages the circles you cannot touch, the mushroom and the firing routine
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.events.Event; public class field_mc extends Sprite { // the big circle on stage public var madball:madball_mc; // the mushroom public var mushroom:mushroom_mc = new mushroom_mc(); // the bullet fired by the mushroom public var bullet:bullet_mc; // boolean to determine is the mushroom is firing public var firing:Boolean=false; public function field_mc(balls_on_stage:int):void { // adding the mushroom addChild(mushroom); // adding the big circles for (var i:int=1; i<=balls_on_stage; i++) { madball = new madball_mc(); addChild(madball); } // event to be fired once the field is added to stage addEventListener(Event.ADDED_TO_STAGE, init); } public function init(e:Event):void { // simply checking for the mouse-pressed on the stage stage.addEventListener(MouseEvent.MOUSE_DOWN, fire); } public function fire(e:MouseEvent):void { // if the mushroom is not firing... if (! firing) { // shoot a bullet bullet=new bullet_mc(); addChild(bullet); firing=true; } } // function to remove a bullet, to be called when the bullet flies off the stage public function remove_bullet():void { firing=false; removeChild(bullet); bullet=null; } } } |
So madball_mc.as just places and scales the circle you can’t touch in a random way
1 2 3 4 5 6 7 8 9 10 11 12 | package { import flash.display.Sprite; public class madball_mc extends Sprite { public function madball_mc():void { // simply randomly placing and scaling the ball x=Math.random()*400+50; y=Math.random()*200+50; width = Math.random()*100+20 height = width } } } |
Then mushroom_mc.as manages mushroom movement and adds the cannon
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package { import flash.display.Sprite; import flash.events.Event; public class mushroom_mc extends Sprite { public var ball:ball_mc=new ball_mc(); public function mushroom_mc():void { y=400; addChild(ball) addEventListener(Event.ENTER_FRAME, on_enter_frame); } public function on_enter_frame(e:Event) { // following mouse along X axis x=stage.mouseX; } } } |
ball_mc.as manages the rotating cannon
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 | package { import flash.display.Sprite; import flash.events.Event; public class ball_mc extends Sprite { // starting degrees value public var degrees:int=0; // value to transform degrees to radians public var deg_to_rad=0.0174532925; // rotation speed public var speed:int=2; public function ball_mc():void { addEventListener(Event.ENTER_FRAME, on_enter_frame); } public function on_enter_frame(e:Event) { // updating the degrees degrees+=speed; // inverting the speed if degrees are > 80 or < -80 if (Math.abs(degrees)>80) { speed*=-1; } // placing and rotating the ball along the mushroom circumference // using a bit of trigonometry y=-20-30*Math.cos(degrees*deg_to_rad); x=30*Math.sin(degrees*deg_to_rad); rotation=degrees; } } } |
Finally bullet_mc.as manages the fired bullet
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 32 33 34 35 36 37 | package { import flash.display.Sprite; import flash.events.Event; public class bullet_mc extends Sprite { // variable to convert degrees to radians public var deg_to_rad=0.0174532925; // variable to save ball direction public var ball_direction:Number; // bullet speed public var speed:int=4; public function bullet_mc():void { addEventListener(Event.ADDED_TO_STAGE, init); addEventListener(Event.ENTER_FRAME, on_enter_frame); } // initializing the bullet placing it in the right spot // according to mushroom position and cannon rotation public function init(e:Event):void { var par:field_mc=this.parent as field_mc; ball_direction=par.mushroom.ball.rotation*0.0174532925; x=par.mushroom.x+50*Math.sin(ball_direction); y=380-50*Math.cos(ball_direction); } public function on_enter_frame(e:Event):void { // moving the bullet using trigonometry x+=speed*Math.sin(ball_direction); y-=speed*Math.cos(ball_direction); if (y<0||x>500||x<0) { // if the bullet flies off the stage, do some garbage collection // and calling the function to remove the bullet removeEventListener(Event.ADDED_TO_STAGE, init); removeEventListener(Event.ENTER_FRAME, on_enter_frame); var par:field_mc=this.parent as field_mc; par.remove_bullet(); } } } } |
Nothing difficult but it’s good to see every objects working in its own class
This is the result:
Press the mouse to fire the bullet, you cannot fire again until the bullet flies off the stage
Next time, collision detection.
They can be easily customized to meet the unique requirements of your project.
One Response
Leave a Reply
- Una guida completa al gioco del poker online e una selezione dei migliori casino online.
- casino online
- migliori casino online
- BlackJack online
- casinò online
- Giochi casino


(5 votes, average: 3.80 out of 5)



i can see the basis of a game coming about now :)