Ononmin Flash prototype – step 3

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers