{"id":1236,"date":"2009-01-01T08:02:12","date_gmt":"2009-01-01T12:02:12","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1236"},"modified":"2022-05-29T08:23:33","modified_gmt":"2022-05-29T12:23:33","slug":"tutorial-create-a-game-like-winter-bells-in-as2-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-game-like-winter-bells-in-as2-part-2\/","title":{"rendered":"Tutorial: Create a Game Like Winter Bells in AS2 &#8211; Part 2"},"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 class=\"c_chap\"><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><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 2: Programming the &#8220;Bells&#8221;<\/h3>\n<p>Let&#8217;s get right to it. In this part of the tutorial, we&#8217;re going to add the &#8220;bells&#8221; to the stage. They&#8217;ll be 100% randomized so we can continue playing the game forever (You&#8217;d like that, wouldn&#8217;t you?). Anyway, we first have to create a new MovieClip to symbolize the bell. Because I ain&#8217;t the greatest artist, mine is going to be a 25&#215;25 square. When you convert it into a MovieClip called <tt>mcBell<\/tt>, also Export it for ActionScript, so we can add it to the stage through ActionScript.<\/p>\n<p><center><img src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/winter-bells-as2\/pt2\/export.gif\" \/><\/center><\/p>\n<p>Do the same thing we did with <tt>mcMain<\/tt> and set it to be the precise center of the MovieClip.<\/p>\n<p>Now, we can add some intense actions. Are you ready? Let&#8217;s first define some variables at the top of the stage:<\/p>\n<pre lang=\"actionscript\">\nvar bellTime:Number = 0;\/\/how many frames have elapsed since the last bell was made\nvar bellLimit:Number = 20;\/\/how many frames it takes to create a new bell\nvar bellTop:Number = -50;\/\/the y coordinate that it'll have when we add it to stage\nvar bellTotal:Number = 0;\/\/how many bells have already been added to stage\n<\/pre>\n<p>I&#8217;ve explained in detail which variable does what. Now, add this function to the bottom of the code:<\/p>\n<pre lang=\"actionscript\">\n_root.createEmptyMovieClip('bellHolder', _root.getNextHighestDepth());\/\/create a MovieClip that will hold all of the bells\nfunction makeLvl():Void{\n\tbellTime ++;\/\/increment the time\n\tif(bellTime >= bellLimit){\/\/if the time for bell creation has been reached\n\t\tbellTotal ++;\/\/increase the amount of bells created\n\t\tbellHolder.attachMovie('mcBell', 'bell'+bellTotal,bellHolder.getNextHighestDepth());\/\/add a bell to the bellHolder\n\t\tif(bellTotal == 1){\/\/if it's the first bell created\n\t\t\tbellHolder['bell'+bellTotal]._x = Math.random()*525;\/\/place it in a random spot on the stage\n\t\t} else {\/\/otherwise, \n\t\t\t\/\/In order to keep the next bell from being too far away from the previous bell, place it up to 250px away\n\t\t\tbellHolder['bell'+bellTotal]._x = bellHolder['bell'+(bellTotal-1)]._x + (Math.random()*500)-250;\n\t\t\tif(bellHolder['bell'+bellTotal]._x > 525){\/\/if it is off the stage to the right\n\t\t\t\tbellHolder['bell'+bellTotal]._x -= 250;\/\/set it inside the stage\n\t\t\t} else if (bellHolder['bell'+bellTotal]._x < 0){\/\/same with too far left\n\t\t\t\tbellHolder['bell'+bellTotal]._x += 250;\n\t\t\t}\n\t\t}\n\t\tbellHolder['bell'+bellTotal]._y = bellTop;\/\/set the y's value off the stage\n\t\tbellHolder['bell'+bellTotal].onEnterFrame = function(){\/\/set some functions for this bugger\n\t\t\tthis._y += 3;\/\/move the bell slowly downwards\n\t\t\tif(this.hitTest(mcMain)){\/\/if this touches the main character\n\t\t\t\t_root.mainJumping = true;\/\/make him jump\n\t\t\t\tjumpSpeed = jumpSpeedLimit*-1;\/\/reset the jumpSpeed\n\t\t\t\t\n\t\t\t\tthis.removeMovieClip();\/\/and finally remove him from the stage\n\t\t\t}\n\t\t}\n\t\tbellTime = 0;\/\/reset the time for another creation\n\t}\n}\n<\/pre>\n<p>That's some pretty intense code, eh? In order to make it function right, we have to run it in the main <tt>onEnterFrame()<\/tt> function. Now, it should be functioning (no pun intended) pretty well. Now, if you've ever played <a href=\"http:\/\/www.ferryhalim.com\/orisinal\/g3\/bells.htm\">Winter Bells<\/a>, you should know that the greatest point booster of the game is the bird that doubles the score. We're going to create one of those in our game as well.<\/p>\n<p>First of all, let's create a \"bird\" MovieClip, eh? Mine is simply going to be a sideways triangle pointing right that is 25px wide and 12.5px high. I guess those are pretty good dimensions. Next, convert it into a MovieClip and Export it for ActionScript as <tt>mcBird<\/tt>.<\/p>\n<p>Now, let's add some actions to this birdy. Add this code to the bottom of the <tt>makeLvl()<\/tt> function:<\/p>\n<pre lang=\"actionscript\">\n\t\/\/every 20 bells, we'll create a bird, not including the first one\n\tif(int(bellTotal\/20) == bellTotal\/20 && bellTime == 1 && bellTotal != 0){\n\t\tbellHolder.attachMovie('mcBird','bird'+bellTotal, bellHolder.getNextHighestDepth());\/\/attach a bird to the stage\n\t\tbellHolder['bird'+bellTotal]._y = bellTop;\/\/set the bird's y value\n\t\tbellHolder['bird'+bellTotal]._x = -50;\/\/set the birds x value at first off the stage\n\t\tbellHolder['bird'+bellTotal].speed = 5;\/\/set the speed\n\t\tbellHolder['bird'+bellTotal].onEnterFrame = function(){\/\/set some actions for the bell\n\t\t\tthis._x += this.speed;\/\/move the bird based on the speed\n\t\t\tif(this._x >= 525){\/\/if the x value is too far right\n\t\t\t\tthis.speed = -5;\/\/change the speed to negative so it moves left\n\t\t\t\tthis._xscale = -100;\/\/turn the MovieClip around\n\t\t\t} \n\t\t\tif (this._x <= 0){\/\/save with too far left\n\t\t\t\tthis.speed = 5;\n\t\t\t\tthis._xscale = 100;\n\t\t\t}\n\t\t\tthis._y += 3;\/\/move the bird slowly down\n\t\t\tif(this.hitTest(mcMain)){\/\/if it touches mcMain\n\t\t\t\t\/\/basically do everything that happened to the bell\n\t\t\t\t_root.mainJumping = true;\n\t\t\t\tjumpSpeed = jumpSpeedLimit*-1;\n\t\t\t\t\n\t\t\t\tthis.removeMovieClip();\n\t\t\t}\n\t\t}\n\t}\n<\/pre>\n<p>It's basically the same thing we did for the bell, except now it moves from side to side.<\/p>\n<p>Well, here concludes the 2nd part of the tutorial. Next time, we'll make this level playable.<\/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\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as2\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as2\/pt2\/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 2: Programming the &#8220;Bells&#8221; Let&#8217;s get right to it. In this part of the tutorial, we&#8217;re going to add the &#8220;bells&#8221; to the stage. They&#8217;ll be 100% randomized so we can continue playing the game forever (You&#8217;d like that, wouldn&#8217;t you?). Anyway, we [&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,6,68],"tags":[7,233,235,18,68,234,236,11,232],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1236"}],"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=1236"}],"version-history":[{"count":11,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1236\/revisions"}],"predecessor-version":[{"id":1288,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1236\/revisions\/1288"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1236"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}