Ononmin Flash prototype – step 2

Read all posts about "" game

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.

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

214 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
// 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