Do you like my tutorials?

Then consider supporting me on Ko-fi

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

In this 3rd part we’ll manage stage edges and balls collisions. You’re invited to read steps 1 and 2 if you didn’t already.

The main function does not change.

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 now decides the color of the balls in the stage (like in the original game, they must change color at every level) and places all balls in a container called balls_cont. This will make our life easier when we’ll remove balls from stage.

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;
		// mad balls container
		public var balls_cont:Sprite = new Sprite();
		public function field_mc(balls_on_stage:int):void {
			// adding the mushroom
			addChild(mushroom);
			addChild(balls_cont);
			// adding the big circles
			place_balls(balls_on_stage);
			// 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 or hits a mad ball
		public function remove_bullet():void {
			firing=false;
			removeChild(bullet);
			bullet=null;
		}
		// function to pick a random color and add n balls
		public function place_balls(n):void {
			var random_color=Math.round(Math.random()*0xFFFFFF);
			for (var i:int=1; i<=n; i++) {
				madball=new madball_mc(random_color);
				balls_cont.addChild(madball);
			}
		}
	}
}

mushroom_mc class does not change

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;
		}
	}
}

bullet_mc now must manage three types of collision: the collision with left and right sides (the bullet rebounds just changing its x speed), the collision with the upper side (the bullet rebounds changing its y speed) and the collision with the bottom side (the bullet is removed and the player can shoot again)

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=10;
		// bullet directional speed
		public var xspeed:int;
		public var yspeed:int;
		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);
			xspeed=speed*Math.sin(ball_direction);
			yspeed=speed*Math.cos(ball_direction)*-1;
		}
		public function on_enter_frame(e:Event):void {
			// moving the bullet using trigonometry
			x+=xspeed;
			y+=yspeed;
			// removing the bullet if it reaches the bottom of the stage
			if (y>400) {
				remove();
			}
			// side bouncing
			if (x>500||x<0) {
				xspeed*=-1;
			}
			// top bouncing
			if (y<0) {
				yspeed*=-1;
			}
		}
		// garbage collection and bullet removal
		public function remove():void {
			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();
		}
	}
}

madball_mc now has a frame listener... if the mushroom is firing, then checks the distance between the bullet and the mad ball itself. If they collide, then turn the ball black as in the original game and remove the bullet, so the player can shoot again.

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	public class madball_mc extends Sprite {
		public function madball_mc(col:int):void {
			// coloring the ball with a random color
			var ball_color:ColorTransform=this.transform.colorTransform;
			ball_color.color=col;
			this.transform.colorTransform=ball_color;
			// simply randomly placing and scaling the ball
			x=Math.random()*400+50;
			y=Math.random()*200+50;
			width=Math.random()*100+20;
			height=width;
			addEventListener(Event.ENTER_FRAME, on_enter_frame);
		}
		public function on_enter_frame(e:Event):void {
			// checking if the mushroom is firing
			var par:field_mc=this.parent.parent as field_mc;
			if (par.firing) {
				// if firing, check collision with circle
				var dist_x:int=par.bullet.x-x;
				var dist_y:int=par.bullet.y-y;
				var distance:Number=dist_x*dist_x+dist_y*dist_y;
				// if the bullet collides with a ball, remove the bullet and change ball color
				if (distance

And this is the result:

You know how to play :)

Download the source code.

During next step, we'll finish the game.

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