Get the complete source code of “Hurry it’s Christmas” Flash 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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
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.<Stuff>; private var targetPositionVector:Vector.<Point>; private var targetVector:Vector.<Stuff>; 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.<Combo>; 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.<Combo>(); 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.<Point>(); 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.<Stuff>(); for (var i:Number=0; i<targetPositionVector.length; i++) { var targetStuff:Stuff=new Stuff(); targetStuff.x=targetPositionVector[i].x; targetStuff.y=targetPositionVector[i].y; targetStuff.width=48; targetStuff.height=48; targetStuff.gotoAndStop(i+1); targetVector.push(targetStuff); christmasTree.addChild(targetStuff); } lightTarget(); movingStuff=null; fallingSpeed=2; fallingVector=new Vector.<Stuff>(); 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; i<targetVector.length; i++) { targetVector[i].gotoAndStop(i+17); } var lights:Number=0; while (lights<8) { var randomLight:Number=Math.ceil(Math.random()*16); if (targetVector[randomLight-1].currentFrame>16) { 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; i<targetVector.length; i++) { if (targetVector[i].hitTestPoint(mouseX,mouseY,true)&&movingStuff.currentFrame==targetVector[i].currentFrame) { placed++; movingStuff.removeEventListener(MouseEvent.MOUSE_DOWN,stuffClicked); movingStuff.placed=true; movingStuff.kill=true; fallingSpeed+=0.025; if (movingStuff.currentFrame==lastMatched) { multiplier++; if (multiplier>5) { 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.
They can be easily customized to meet the unique requirements of your project.












This post has 4 comments
xiaobin
Thank you !!
Rahyar
Hi Emmanuel :)
Merry Christmas!
It’s a wonderful gift.
Thank you
Get the complete source code of “Hurry it’s Christmas” Flash game – Emanuele Feronato « eaflash
[...] on http://www.emanueleferonato.com Share this:TwitterFacebookLike this:LikeBe the first to like [...]
Y?ld?ray
Thanks a lot Emanuele. Nice work!