Create a Flash Game like Nano War – step 2

Read all posts about "" game

In the 1st step we created the units and the engine to select them.

Now it’s time to write the code to let selected unit(s) send troops to other unit.

I created a new frame for the sphere object, a green circle, representing the target unit.

Let’s make a recap:

Frame 1: black circle, unselected unit

Frame 2: red circle, selected unit

Frame 3: green circle, target unit

Obviously, an unit cannot be target of itself

So, once we have one or more selected units, the script will have to:

If the player does not press the mouse

Draw a line from selected unit(s) to current mouse position

If the mouse is not over an unselected unit, leave all unselected units to frame 1

If the mouse is over an unselected unit, play frame 3 for that unit and leave all unselected units to frame 1

If the player presses the mouse

If the mouse is clicked outside an unselected unit, deselect all selected units

If the mose is clicked inside a unselected unit, deselect all selected units and select the new unit (during next part we’ll see how to move troops to this unit)

Here it is the script:

// variable stating if I clicked the mouse
clicked = false;
// variable stating if I am dragging the mouse
dragging = false;
// flag determining if an unit was selected
unit_selected = false;
// 10 times loop
for (x=1; x<=10; x++) {
	// can_place = true: I can place the unit / false: I can't
	// initializing it to false to force entering the while loop
	can_place = false;
	// while loop to look for an empty space where to place the unit
	while (!can_place) {
		// setting can_place to true
		// you can always place an unit until you can't...
		can_place = true;
		// generating random x and y positions, and random width
		px = Math.floor(Math.random()*400)+50;
		py = Math.floor(Math.random()*300)+50;
		w = Math.floor(Math.random()*20)+30;
		// scanning all already placed units
		for (y=x-1; y>=1; y--) {
			// determining the distance between the y-th unit and the one I am trying to place
			dist_x = px-_root["sphere_"+y]._x;
			dist_y = py-_root["sphere_"+y]._y;
			distance = Math.ceil(Math.sqrt(dist_x*dist_x+dist_y*dist_y));
			// this is the minimum distance I want between two units:
			// the sum of both radiuses plus 20 pixels
			min_distance = (w+_root["sphere_"+y]._width)/2+20;
			// if the new unit would be too close to an existing one...
			if (distanceMath.min(click_x, posx)) and (this._xMath.min(click_y, posy)) and (this._y

And this is the result

Download the source code and think about enemy's AI... this will be the hardest part

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