Develop a Flash game like Wish Upon a Star

Read all posts about "" game

Yesterday I got a couple of badges playing at Wish Upon a Star, and I found the game simple and fun, as well as perfect for a brief tutorial for beginners.

Let’s see what I developed at the moment in this little prototype:

* player moves left and right with slippy controls (like in the original game until you buy grippy control)
* stars fall down at every second
* stars disappear when they leave the bottom of the state
* player can collect stars by touching their center
* stars explode with a particle effect (powered by Flint Particles) when collected

This is the main class:

package {
	import flash.display.Sprite;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	public class Main extends Sprite {
		// falling stars per second
		private var starsPerSecond:Number=5;
		// vector which will contain all stars
		private var starVector:Vector.=new Vector.();
		// player DisplayObject and properties
		private var player:Player=new Player();
		private var playerSpeed:Number=0;
		private var playerThrust:Number=0.5;
		private var playerMaxSpeed:Number=3;
		private var playerFriction:Number=0.98;
		// game timer
		private var timer:Timer=new Timer(1000);
		public function Main() {
			// add background
			var bg:Background=new Background();
			addChild(bg);
			// add the player
			addChild(player);
			// start the timer
			timer.start();
			// listeners
			timer.addEventListener(TimerEvent.TIMER, tick);
			addEventListener(Event.ENTER_FRAME, update);
			stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed);
		}
		private function tick(e:TimerEvent):void {
			// every second, add some stars and put them on stage and in starVector vector
			for (var i:Number=0; iplayerMaxSpeed) {
				playerSpeed=playerMaxSpeed;
			}
			if (playerSpeed<-playerMaxSpeed) {
				playerSpeed=- playerMaxSpeed;
			}
		}
		private function update(e:Event):void {
			for (var i:Number=0; i490) {
					removeChild(starVector[i]);
					starVector.splice(i,1);
					if (i>0) {
						i--;
					}
				}
				// remove stars when their CENTER hit the player
				if (player.hitTestPoint(starVector[i].x,starVector[i].y,true)) {
					var starExplosion:StarExplosion=new StarExplosion(starVector[i]);
					addChild(starExplosion);
					removeChild(starVector[i]);
					starVector.splice(i,1);
					if (i>0) {
						i--;
					}
				}
			}
			// adjust player posiiton
			player.x+=playerSpeed;
			playerSpeed*=playerFriction;
		}
	}
}

This is Star class, only used to randomly place stars and assign them a random speed:

package {
	import flash.display.Sprite;
	public class Star extends Sprite {
		public var ySpeed:Number;
		public function Star() {
			x=Math.random()*600+20;
			y=-10+Math.random()*-240;
			ySpeed=0.9+Math.random()*0.2;
		}
	}
}

This is Player class,only used to place the player:

package {
	import flash.display.Sprite;
	public class Player extends Sprite {
		public function Player() {
			x=320;
			y=465;
		}
	}
}

And this is StarExplosion class, used for the particle effect:

package {
	import flash.display.Sprite;
	import flash.geom.Point;
	import org.flintparticles.common.counters.*;
	import org.flintparticles.common.actions.*;
	import org.flintparticles.common.displayObjects.RadialDot;
	import org.flintparticles.common.initializers.*;
	import org.flintparticles.twoD.actions.*;
	import org.flintparticles.twoD.emitters.Emitter2D;
	import org.flintparticles.twoD.initializers.*;
	import org.flintparticles.twoD.renderers.*;
	import org.flintparticles.twoD.zones.*;
	public class StarExplosion extends Sprite {
		public function StarExplosion(s:Star) {
			x=s.x;
			y=s.y;
			var emitter:Emitter2D = new Emitter2D();
			emitter.counter=new Blast(50);
			emitter.addInitializer(new ImageClass(Star));
			emitter.addInitializer(new Velocity(new DiscZone(new Point(0,0),70,40)));
			emitter.addInitializer(new ScaleImageInit(0.2,0.4));
			emitter.addInitializer(new Lifetime(1,1.5));
			emitter.addInitializer(new AlphaInit(0.2,0.7));
			emitter.addAction(new Move());
			emitter.addAction(new Age());
			emitter.addAction(new Accelerate(0,100));
			emitter.addAction(new RandomDrift(20,2));
			var renderer:DisplayObjectRenderer = new DisplayObjectRenderer();
			renderer.addEmitter(emitter);
			addChild(renderer);
			emitter.start();
		}
	}
}

This is the result:

Move the player with LEFT and RIGHT arrow keys.

Download the source code. Next time, player jump and animation, and something more.

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