Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Wish Upon a Star game, Actionscript 3, Flash and Game development.

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.

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