Get the complete source code of “Hurry it’s Christmas” Flash game

Read all posts about "" game

When I released Hurry it’s Christmas Flash game I told you I would release the source code for free as soon as Christmas ends.

Well, Christmas was three days ago and it’s time to release the source code as promised. The code is uncommented because it’s almost the one I used when I published the game, but the game is quite easy, variable and function names have their meaning, and I tried to write the entire game in one single class, to make it easier for beginners to read the whole code.

The game itself comes from the post inverting Toony Flash game prototype turning it into a Christmas game, so maybe having a look at such post too may help you to understand the concept behind it.

In this code you will also find Playtomic API integration as well as MochiMedia leaderboards and inter-level ads. Also, Flint particle system has been used to render the falling snow

Here is the main class:

package {
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import Playtomic.*;
	import mochi.as3.*;
	public class Main extends Sprite {
		private var fallingTimer:Timer;
		private var fallingInterval:Number;
		private var fallingVector:Vector.;
		private var targetPositionVector:Vector.;
		private var targetVector:Vector.;
		private var fallingSpeed:Number;
		private var movingStuff:Stuff;
		private var christmasTree:ChristmasTree;
		private var score:Number;
		private var scorePanel:ScorePanel;
		private var timeLeft:Number;
		private var timeCanvas:Sprite;
		private var gameTitle:GameTitle;
		private var splashPlayButton:SplashPlayButton;
		private var placed:Number;
		private var gameOver:GameOver;
		private var playAgain:PlayAgain;
		private var level:Number;
		private var lastMatched:Number;
		private var multiplier:Number;
		private var comboVector:Vector.;
		private var logo:Logo;
		private var howToPlay:HowToPlay;
		private var bgMusic:Sound = new BgMusic();
		private var channel:SoundChannel=new SoundChannel();
		private var soundVolume:Number;
		private var soundOnOff:SoundOnOff;
		private var playSound:Boolean;
		private var _mochiads_game_id:String="3174cc9ed4648f22";
		private var adMovieClip:MovieClip;
		public function Main() {
			MochiServices.connect(_mochiads_game_id, root);
			Log.View(951604, "96c86e78c6894d44", "b5da7c420c2844af836a0d898b1dee", root.loaderInfo);
			MochiBot.track(this, "ca48eadc");
			playSound=true;
			var gameBg:GameBg=new GameBg();
			addChild(gameBg);
			var snow:Snow=new Snow();
			addChild(snow);
			var bgMask:BgMask=new BgMask();
			addChild(bgMask);
			splash();
		}
		private function splash():void {
			gameTitle=new GameTitle();
			addChild(gameTitle);
			gameTitle.x=88;
			gameTitle.y=20;
			splashPlayButton= new SplashPlayButton();
			splashPlayButton.x=259;
			splashPlayButton.y=200;
			splashPlayButton.buttonMode=true;
			addChild(splashPlayButton);
			splashPlayButton.addEventListener(MouseEvent.CLICK,showTutorial);
			logo=new Logo();
			addChild(logo);
			logo.x=5;
			logo.y=355;
			logo.buttonMode=true;
			logo.addEventListener(MouseEvent.CLICK,logoClick);
		}
		private function logoClick(e:MouseEvent):void {
			navigateToURL(new URLRequest("http://www.emanueleferonato.com/"));
		}
		private function showTutorial(e:MouseEvent):void {
			Log.Play();
			splashPlayButton.removeEventListener(MouseEvent.CLICK,showTutorial);
			removeChild(splashPlayButton);
			removeChild(gameTitle);
			logo.removeEventListener(MouseEvent.CLICK,logoClick);
			removeChild(logo);
			howToPlay=new HowToPlay();
			addChild(howToPlay);
			howToPlay.buttonMode=true;
			howToPlay.addEventListener(MouseEvent.CLICK,gameSetup);
		}
		private function gameSetup(e:MouseEvent):void {
			howToPlay.removeEventListener(MouseEvent.CLICK,gameSetup);
			removeChild(howToPlay);
			comboVector=new Vector.();
			level=0;
			lastMatched=0;
			scorePanel=new ScorePanel();
			addChild(scorePanel);
			timeCanvas=new Sprite();
			scorePanel.addChild(timeCanvas);
			scorePanel.scoreText.text="0";
			score=0;
			MochiEvents.startPlay();
			placed=0;
			multiplier=0;
			soundVolume=1;
			timeLeft=100;
			christmasTree=new ChristmasTree();
			christmasTree.y=50;
			christmasTree.x=2;
			addChild(christmasTree);
			targetPositionVector=new Vector.();
			targetPositionVector.push(new Point(118,306));
			targetPositionVector.push(new Point(202,306));
			targetPositionVector.push(new Point(53,276));
			targetPositionVector.push(new Point(267,276));
			targetPositionVector.push(new Point(110,380));
			targetPositionVector.push(new Point(280,380));
			targetPositionVector.push(new Point(40,380));
			targetPositionVector.push(new Point(210,380));
			targetPositionVector.push(new Point(160,115));
			targetPositionVector.push(new Point(110,100));
			targetPositionVector.push(new Point(210,100));
			targetPositionVector.push(new Point(160,215));
			targetPositionVector.push(new Point(90,200));
			targetPositionVector.push(new Point(230,200));
			targetPositionVector.push(new Point(160,280));
			targetPositionVector.push(new Point(158,0));
			targetVector=new Vector.();
			for (var i:Number=0; i();
			fallingInterval=500;
			fallingTimer=new Timer(fallingInterval);
			fallingTimer.addEventListener(TimerEvent.TIMER, newStuff);
			fallingTimer.start();
			addEventListener(Event.ENTER_FRAME,update);
			channel=bgMusic.play(0,10000);
			if (playSound) {
				channel.soundTransform=new SoundTransform(1,0);
			}
			else {
				channel.soundTransform=new SoundTransform(0,0);
			}
			soundOnOff=new SoundOnOff();
			soundOnOff.x=00;
			soundOnOff.y=30;
			soundOnOff.buttonMode=true;
			if (playSound) {
				soundOnOff.gotoAndStop(2);
			}
			else {
				soundOnOff.gotoAndStop(1);
			}
			addChild(soundOnOff);
			soundOnOff.addEventListener(MouseEvent.CLICK,switchSound);
		}
		private function switchSound(e:MouseEvent):void {
			playSound=! playSound;
			soundOnOff.gotoAndStop(3-soundOnOff.currentFrame);
			if (playSound) {
				channel.soundTransform=new SoundTransform(1,0);
			}
			else {
				channel.soundTransform=new SoundTransform(0,0);
			}
		}
		private function lightTarget():void {
			level++;
			for (var i:Number=0; i16) {
					lights++;
				}
				targetVector[randomLight-1].gotoAndStop(randomLight);

			}
		}
		private function displayTime():void {
			timeCanvas.graphics.clear();
			timeCanvas.graphics.lineStyle(10,0x00223c);
			timeCanvas.graphics.moveTo(10,470);
			timeCanvas.graphics.lineTo(310,470);
			timeCanvas.graphics.lineStyle(5,0xf8cb0a);
			timeCanvas.graphics.moveTo(10,470);
			timeCanvas.graphics.lineTo(10+300*timeLeft/100,470);
		}
		private function newStuff(e:TimerEvent):void {
			var stuff:Stuff=new Stuff();
			stuff.x=320+24+Math.random()*272;
			stuff.y=-32;
			stuff.rotation=Math.random()*360;
			stuff.rotationDir=(Math.floor(Math.random()*2)==0)?1:-1;
			stuff.fallingSpeed=fallingSpeed-fallingSpeed*0.2+Math.random()*fallingSpeed*0.4;
			stuff.placed=false;
			stuff.kill=false;
			stuff.gotoAndStop(Math.ceil(Math.random()*16));
			stuff.buttonMode=true;
			stuff.addEventListener(MouseEvent.MOUSE_DOWN,stuffClicked);
			fallingVector.push(stuff);
			addChild(stuff);
			timeLeft--;
			displayTime();
			if (timeLeft<=0) {
				scorePanel.removeChild(timeCanvas);
				movingStuff=null;
				fallingTimer.removeEventListener(TimerEvent.TIMER, newStuff);
				fallingTimer.stop();
				for (var i:Number=fallingVector.length-1; i>=0; i--) {
					fallingVector[i].removeEventListener(MouseEvent.MOUSE_DOWN,stuffClicked);
				}
			}
		}
		private function stuffClicked(e:MouseEvent):void {
			if (movingStuff==null) {
				movingStuff=e.target as Stuff;
				stage.addEventListener(MouseEvent.MOUSE_UP,stuffReleased);
			}
		}
		private function stuffReleased(e:MouseEvent):void {
			if (movingStuff!=null) {
				stage.removeEventListener(MouseEvent.MOUSE_UP,stuffReleased);
				for (var i:Number=0; i5) {
								multiplier=5;
							}
							if (multiplier>1) {
								var combo:Combo=new Combo();
								combo.x=targetVector[i].x;
								combo.y=targetVector[i].y;
								combo.gotoAndStop(multiplier-1);
								addChild(combo);
								comboVector.push(combo);
							}
						}
						else {
							multiplier=1;
						}
						lastMatched=movingStuff.currentFrame;
						timeLeft+=multiplier;
						displayTime();
						score+=50*level*multiplier;
						scorePanel.scoreText.text=score.toString();
						if (placed%5==0) {
							lightTarget();
						}
						break;
					}
				}
				movingStuff=null;
			}
		}
		private function newPlay(e:MouseEvent):void {
			MochiScores.closeLeaderboard();
			removeChild(gameOver);
			playAgain.removeEventListener(MouseEvent.CLICK,newPlay);
			removeChild(playAgain);
			adMovieClip=new MovieClip();
			addChild(adMovieClip);
			MochiAd.showInterLevelAd({clip:adMovieClip, id:_mochiads_game_id, res:"640x480", no_bg:true,ad_finished:function ():void {removeChild(adMovieClip);splash()},ad_failed:function ():void {removeChild(adMovieClip);splash()},ad_skipped:function ():void {removeChild(adMovieClip);splash()}});
		}
		private function update(e:Event):void {
			if (timeLeft<=0) {
				if (playSound) {
					soundVolume-=0.05;
					channel.soundTransform=new SoundTransform(soundVolume,0);
					if (soundVolume<=0) {
						channel.stop();
					}
				}
				else {
					channel.stop();
				}
				christmasTree.alpha-=0.015;
				if (christmasTree.alpha<=0) {
					removeChild(scorePanel);
					soundOnOff.removeEventListener(MouseEvent.CLICK,switchSound);
					removeChild(soundOnOff);
					removeEventListener(Event.ENTER_FRAME,update);
					gameOver=new GameOver();
					gameOver.x=165;
					gameOver.y=10;
					addChild(gameOver);
					MochiEvents.endPlay();
					playAgain=new PlayAgain();
					playAgain.x=227;
					playAgain.y=410;
					playAgain.buttonMode=true;
					playAgain.addEventListener(MouseEvent.CLICK,newPlay);
					addChild(playAgain);
					var o:Object={n:[0,10,14,14,14,2,5,11,12,9,2,4,11,8,9,9],f:function (i:Number,s:String):String { if (s.length == 16) return s; return this.f(i+1,s + this.n[i].toString(16));}};
					var boardID:String=o.f(0,"");
					MochiScores.showLeaderboard({boardID: boardID, score: score});
				}
			}
			for (var i:Number=comboVector.length-1; i>=0; i--) {
				comboVector[i].y-=2;
				comboVector[i].alpha-=0.05;
				if (comboVector[i].alpha<=0) {
					removeChild(comboVector[i]);
					comboVector.splice(i,1);
				}
			}
			for (i=fallingVector.length-1; i>=0; i--) {
				if (fallingVector[i].placed) {
					if (fallingVector[i].x!=targetVector[fallingVector[i].currentFrame-1].x) {
						fallingVector[i].x-=(fallingVector[i].x-targetVector[fallingVector[i].currentFrame-1].x)/10;
					}
					if (fallingVector[i].y!=targetVector[fallingVector[i].currentFrame-1].y+50) {
						fallingVector[i].y-=(fallingVector[i].y-targetVector[fallingVector[i].currentFrame-1].y-50)/10;
					}
					if (fallingVector[i].rotation!=0) {
						fallingVector[i].rotation-=fallingVector[i].rotation/10;
					}
					if (fallingVector[i].width!=targetVector[fallingVector[i].currentFrame-1].width) {
						fallingVector[i].width-=(fallingVector[i].width-targetVector[fallingVector[i].currentFrame-1].width)/10;
						fallingVector[i].height=fallingVector[i].width;
					}
				}
				else {
					if (timeLeft<=0) {
						fallingVector[i].fallingSpeed+=1;
					}
					fallingVector[i].y+=fallingVector[i].fallingSpeed;
					fallingVector[i].rotation+=(fallingVector[i].fallingSpeed/10)*(-1*fallingVector[i].rotationDir);
				}
				if (fallingVector[i].kill) {
					fallingVector[i].alpha-=0.025;
				}
				if (fallingVector[i].y>550||fallingVector[i].alpha<=0) {
					removeChild(fallingVector[i]);
					fallingVector.splice(i,1);
				}
			}
			if (movingStuff!=null) {
				movingStuff.rotation-=(movingStuff.fallingSpeed/10)*(-1*movingStuff.rotationDir);
				movingStuff.x=mouseX;
				movingStuff.y=mouseY;
			}
		}
	}
}

And this is the complete source code. You are free to do what you want with it, just remember the graphics is not yours but it's mainly property of deleket.

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