{"id":1280,"date":"2009-01-07T08:04:50","date_gmt":"2009-01-07T12:04:50","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1280"},"modified":"2022-05-29T08:23:32","modified_gmt":"2022-05-29T12:23:32","slug":"tutorial-create-a-game-like-winter-bells-in-as3-part-4","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-game-like-winter-bells-in-as3-part-4\/","title":{"rendered":"Tutorial: Create a Game Like Winter Bells in AS3 &#8211; Part 4"},"content":{"rendered":"<div class=\"toc\">\n<ol>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as3\/\">Basic Character Programming<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as3-part-2\/\">Programming the &#8220;Bells&#8221;<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as3-part-3\/\">Level Creation<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as3-part-4\/\">Scoring<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as3-part-5\/\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3>Step 4: Scoring<\/h3>\n<p>Now, we get to score this game. In addition to adding a score, we&#8217;re also going to add a few other components, including displaying the points each bell is worth, as well as the game over stuff. Shall we begin?<\/p>\n<p>Let&#8217;s first do the score, since that&#8217;s the easiest. Define these variables at the top of your code:<\/p>\n<pre lang=\"actionscript\">\r\nvar score:int = 0;\/\/you know what this is\r\nvar scoreInc:int = 0;\/\/how many points the bell is going to be worth\r\n<\/pre>\n<p>Now, find the code in &#8220;Bell.as&#8221; where the bell is hit by <tt>mcMain<\/tt>. Add this code to that:<\/p>\n<pre lang=\"actionscript\">\r\n_root.scoreInc += 10;\/\/increase the amount that the score will increase\r\n_root.score += _root.scoreInc;\/\/add this to the score\r\n<\/pre>\n<p>Next, find the code where the bird is hit by <tt>mcMain<\/tt>.  Double the score in that part of the code with <tt>_root.score *= 2<\/tt><\/p>\n<p>Congratulations, we&#8217;ve just scored our game! That was the easy part. Now, we have to actually display what each bell is worth after hitting it. In order to do this, we&#8217;re going to have to create a new MovieClip. The only thing that&#8217;s going to be inside it is a dynamic textfield. Also, when you convert it into a MovieClip, Export it for ActionScript as the class, <tt>ScoreAdd<\/tt>, would you now?<\/p>\n<p>Give the dynamic textfield an instance name of <tt>txtScore<\/tt>, and set it to 18 point font Arial. Also, you should embed the font, just in case.<\/p>\n<p>Now, create a new layer within that MovieClip and add the following code to it:<\/p>\n<pre lang=\"actionscript\">\r\nvar timeLeft:int = 12;\/\/how many frames are allowed to pass before it disappears\r\n\r\nthis.addEventListener(Event.ENTER_FRAME, timeCheck);\r\n\r\nfunction timeCheck(e:Event):void{\r\n\tthis.timeLeft --;\/\/decrease the amount of time\r\n\tif(this.timeLeft == 0){\/\/if time is up\r\n\t\tthis.parent.removeChild(this);\/\/then remove this\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, let&#8217;s add this to the stage whenever <tt>mcMain<\/tt> hits a bell. Place this code in the <tt>hitTestObject<\/tt> for <tt>mcMain<\/tt> in &#8220;Bell.as&#8221;:<\/p>\n<pre lang=\"actionscript\">\r\nvar scoreText:ScoreAdd = new ScoreAdd();\r\n_root.bellHolder.addChild(scoreText);\/\/add some text to the stage\r\nscoreText.x = this.x;\/\/set the coordinates for the text\r\nscoreText.y = this.y;\r\nscoreText.txtScore.text = _root.scoreInc;\/\/set the text to the amount the score increased by\r\n<\/pre>\n<p>Do something similar for the bird:<\/p>\n<pre lang=\"actionscript\">\r\nvar scoreText:ScoreAdd = new ScoreAdd();\r\n_root.bellHolder.addChild(scoreText);\/\/add some text to the stage\r\nscoreText.x = this.x;\/\/set the coordinates for the text\r\nscoreText.y = this.y;\r\nscoreText.txtScore.text = 'Double Score';\/\/show that the score was doubled\r\nthis.removeEventListener(Event.ENTER_FRAME, eFrame);\/\/remove the listeners\r\n_root.bellHolder.removeChild(this);\/\/and finally remove him from the stage\r\n<\/pre>\n<p>Now, we must display the score to the user. Just add a dynamic text field to the top of the stage with an instance name of <tt>txtScore<\/tt>. Now, in the <tt>eFrame()<\/tt> function, just set the text to <tt>\"Score:  \"+score<\/tt>.<\/p>\n<p>The final task that we must complete is what happens when the user finally falls. To do this, we&#8217;re going to have to create some more variables:<\/p>\n<pre lang=\"actionscript\">\r\nvar gameOver:Boolean = false;\/\/if game has been finished\r\nvar fallingTime:int = 0;\/\/how long the main guy has been falling down\r\n<\/pre>\n<p>Before we go any further, add the following code to the <tt>eFrame()<\/tt> functions of both the bell and the bird:<\/p>\n<pre lang=\"actionscript\">\r\nif(_root.gameOver){\/\/if the game is over\r\n\tthis.removeEventListener(Event.ENTER_FRAME, eFrame);\/\/remove the listeners\r\n\t_root.bellHolder.removeChild(this);\/\/and remove from stage\r\n}\r\n<\/pre>\n<p>This will help us later. Next, find this code in the <tt>moveScreen()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\nif(mcMain.y >= 275 && totalHeight >= 275){ \/\/if mcMain is above a certain point and the screen has been moved up\r\n\tbellHolder.y -= jumpSpeed;\/\/make bellHolder go back down\r\n\tbellTop += jumpSpeed;\/\/as well as the creation point\r\n\tmcMain.y = 275;\/\/keep mcMain's y stationary until it is done falling\r\n}\r\n<\/pre>\n<p>Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\nif(mcMain.y >= 275 && totalHeight >= 275){ \/\/if mcMain is above a certain point and the screen has been moved up\r\n\tbellHolder.y -= jumpSpeed;\/\/make bellHolder go back down\r\n\tbellTop += jumpSpeed;\/\/as well as the creation point\r\n\tmcMain.y = 275;\/\/keep mcMain's y stationary until it is done falling\r\n\t\r\n\tfallingTime ++;\r\n\t\r\n\tif(fallingTime >= 12){\r\n\t\tgameOver = true;\r\n\t}\r\n}\r\n<\/pre>\n<p>We have to do this so that if the player gets up really high and falls, there won&#8217;t be any more bells to catch him\/her and delay the end of the game.<\/p>\n<p>Now, add <tt>_root.startedJumping = true;<\/tt> to where the bell and bird hit test for <tt>mcMain<\/tt>.<\/p>\n<p>Next, add this code to the part of the <tt>mainJump()<\/tt> function where we check if he&#8217;s touching the ground. Change it to this:<\/p>\n<pre lang=\"actionscript\">\r\nif(startedJumping){\/\/if the main has begun jumping\r\n\tgameOver = true;\/\/then finish the game\r\n\tshowFinalStats();\/\/and show the poor kid their stats\r\n}\r\nmainJumping = false;\r\n<\/pre>\n<p>We have to define the <tt>showFinalStats()<\/tt> function at the end of the code, but let&#8217;s first create a MovieClip that will pop up with the stats. Create a gray 300&#215;250 sized square as a background for this MovieClip. Then, add some text like &#8220;Here is your final score:&#8221;. Then add a dynamic textfield with an instance name of <tt>txtFinalScore<\/tt> below and a &#8220;Play Again?&#8221; button below that with an instance name of <tt>btnPlayAgain<\/tt>. This is what mine looks like:<\/p>\n<p><center><img src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/winter-bells-as2\/pt4\/finalstats.gif\" \/><\/center><\/p>\n<p>Now, add the following code to the bottom of everything:<\/p>\n<pre lang=\"actionscript\">\r\nvar finalScreen:mcFinalStats = new mcFinalStats();\r\n\r\nfunction showFinalStats():void{\r\n\t\/\/attach the final screen to the stage:\r\n\taddChild(finalScreen);\r\n\t\/\/setting the coordinates\r\n\tfinalScreen.x = 115;\r\n\tfinalScreen.y = 75;\r\n\tfinalScreen.txtFinalScore.text = String(score);\/\/showing the score\r\n\tfinalScreen.btnPlayAgain.addEventListener(MouseEvent.CLICK, clickPlayAgain);\/\/when user clicks on the button\r\n}\r\n\r\nfunction clickPlayAgain(e:MouseEvent):void{\r\n\t\/\/reset everything\r\n\tremoveChild(finalScreen);\r\n\tgameOver = false;\r\n\tstartedJumping = false;\r\n\tscore = 0;\r\n\tscoreInc = 0;\r\n}\r\n<\/pre>\n<p>Well, now we can be jubilant, for we are soon finished with this game. Next time, we&#8217;ll just fix some issues and add some extras to the game. Join us!<\/p>\n<h4>The Final Product<\/h4>\n<p><center><object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" width=\"550\" height=\"400\" codebase=\"http:\/\/download.macromedia.com\/pub\/shockwave\/cabs\/flash\/swflash.cab#version=6,0,40,0\"><param name=\"src\" value=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as3\/pt4\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as3\/pt4\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as3\/pt4\/source.zip\">Source Files (zipped)<\/a><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Basic Character Programming Programming the &#8220;Bells&#8221; Level Creation Scoring Finishing Touches Step 4: Scoring Now, we get to score this game. In addition to adding a score, we&#8217;re also going to add a few other components, including displaying the points each bell is worth, as well as the game over stuff. Shall we begin? Let&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,8,4,6,68],"tags":[8,233,235,18,68,234,236,11,232],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1280"}],"collection":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/comments?post=1280"}],"version-history":[{"count":3,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1280\/revisions"}],"predecessor-version":[{"id":1339,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1280\/revisions\/1339"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1280"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}