Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Ononmin game, Actionscript 3, Flash and Game development.

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:

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

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

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

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

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

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

Download the source code.

Next time, collision detection.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.