{"id":1245,"date":"2009-01-01T08:04:39","date_gmt":"2009-01-01T12:04:39","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1245"},"modified":"2022-05-29T08:23:33","modified_gmt":"2022-05-29T12:23:33","slug":"tutorial-create-a-game-like-winter-bells-in-as2-part-4","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-game-like-winter-bells-in-as2-part-4\/","title":{"rendered":"Tutorial: Create a Game Like Winter Bells in AS2 &#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-as2\/\">Basic Character Programming<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as2-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-as2-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-as2-part-4\/\">Scoring<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-create-a-game-like-winter-bells-in-as2-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:Number = 0;\/\/you know what this is\r\nvar scoreInc:Number = 0;\/\/how many points the bell is going to be worth\r\n<\/pre>\n<p>Now, find the code 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 += 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, and some code. Also, when you convert it into a MovieClip, Export it for ActionScript as <tt>mcScoreAdd<\/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:Number = 12;\/\/how many frames are allowed to pass before it disappears\r\n\r\nthis.onEnterFrame = function(){\r\n\tthis.timeLeft --;\/\/decrease the amount of time\r\n\tif(this.timeLeft == 0){\/\/if time is up\r\n\t\tthis.removeMovieClip();\/\/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>hitTest<\/tt> for <tt>mcMain<\/tt> in the bell&#8217;s functions:<\/p>\n<pre lang=\"actionscript\">\r\nbellHolder.attachMovie('mcScoreAdd', 'txt'+this._name, bellHolder.getNextHighestDepth());\/\/add some text to the stage\r\nbellHolder['txt'+this._name]._x = this._x;\/\/set the coordinates for the text\r\nbellHolder['txt'+this._name]._y = this._y;\r\nbellHolder['txt'+this._name].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\nbellHolder.attachMovie('mcScoreAdd', 'txt'+this._name, bellHolder.getNextHighestDepth());\/\/add some text to the stage\r\nbellHolder['txt'+this._name]._x = this._x;\/\/set the coordinates for the text\r\nbellHolder['txt'+this._name]._y = this._y;\r\nbellHolder['txt'+this._name].txtScore.text = 'Double Score';\/\/Show the user that the score was doubled\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 main <tt>onEnterFrame()<\/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:Number = 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>onEnterFrame()<\/tt> functions of both the bell and the bird:<\/p>\n<pre lang=\"actionscript\">\r\nif(_root.gameOver){\r\n\tthis.removeMovieClip();\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 ++;\/\/increase the amount of time main has fallen\r\n\t\r\n\tif(fallingTime >= 12){\/\/if he has fallen long enough\r\n\t\tgameOver = true;\/\/then the game is over\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>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\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 <tt>showFinalStats<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\r\nfunction showFinalStats():Void{\r\n\t\/\/attach the final screen to the stage:\r\n\tattachMovie('mcFinalStats', 'finalstats', _root.getNextHighestDepth());\r\n\t\/\/setting the coordinates\r\n\tfinalstats._x = 115;\r\n\tfinalstats._y = 75;\r\n\tfinalstats.txtFinalScore.text = score;\/\/showing the score\r\n\tfinalstats.btnPlayAgain.onRelease = function(){\/\/when user clicks on the button\r\n\t\t\/\/reset everything\r\n\t\tfinalstats.removeMovieClip();\r\n\t\tgameOver = false;\r\n\t\tstartedJumping = false;\r\n\t\tscore = 0;\r\n\t\tscoreInc = 0;\r\n\t}\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-as2\/pt4\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as2\/pt4\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as2\/pt4\/source.fla\">Source .fla File<\/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,7,4,68],"tags":[7,233,235,18,68,234,236,11,232],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1245"}],"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=1245"}],"version-history":[{"count":10,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions"}],"predecessor-version":[{"id":1290,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1245\/revisions\/1290"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1245"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}