{"id":1268,"date":"2009-01-07T08:02:32","date_gmt":"2009-01-07T12:02:32","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1268"},"modified":"2022-05-29T08:23:32","modified_gmt":"2022-05-29T12:23:32","slug":"tutorial-create-a-game-like-winter-bells-in-as3-part-2","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-create-a-game-like-winter-bells-in-as3-part-2\/","title":{"rendered":"Tutorial: Create a Game Like Winter Bells in AS3 &#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-as3\/\">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-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><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 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 with a class of <tt>Bell<\/tt>, so we can add it to the stage through ActionScript.<\/p>\n<p><center><img src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/winter-bells-as3\/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\">\r\nvar bellTime:int = 0;\/\/how many frames have elapsed since the last bell was made\r\nvar bellLimit:int = 20;\/\/how many frames it takes to create a new bell\r\nvar bellTop:Number = -50;\/\/the y coordinate that it'll have when we add it to stage\r\nvar bellTotal:int = 0;\/\/how many bells have already been added to stage\r\nvar bellLastCoord:Number = 0;\/\/the x value of the previous bell\r\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\">\r\nvar bellHolder:MovieClip = new MovieClip();\/\/creating a MovieClip that will hold all the bells\r\naddChild(bellHolder);\/\/add this MovieClip to the stage\r\nfunction makeLvl():void{\/\/this function will add bells to the stage\r\n\tbellTime ++;\/\/increment the time\r\n\tif(bellTime >= bellLimit){\/\/if the time for bell creation has been reached\r\n\t\tbellTotal ++;\/\/increase the amount of bells created\r\n\t\tvar newBell:Bell = new Bell();\/\/create a new bell instance\r\n\t\tbellHolder.addChild(newBell);\/\/and add it to bellHolder\r\n\t\tbellTime = 0;\/\/reset the time for another creation\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, run the <tt>makeLvl()<\/tt> function in the <tt>eFrame()<\/tt> function. Next, we have to create an external ActionScript file for the <tt>Bell<\/tt> class. Go to <strong>File -> New&#8230;<\/strong> and select &#8220;ActionScript File&#8221;. Save it in the same folder as your .fla file as &#8220;Bell.as&#8221;. Now, place this code into it:<\/p>\n<pre lang=\"actionscript\">\r\npackage{\r\n\timport flash.display.MovieClip;\r\n\timport flash.events.*;\r\n\tpublic class Bell extends MovieClip {\r\n\t\t\r\n\t\tvar _root:Object;\/\/this will symbolize the main timeline\r\n\t\t\r\n\t\tpublic function Bell() {\r\n\t\t\taddEventListener(Event.ADDED, beginClass);\r\n\t\t\taddEventListener(Event.ENTER_FRAME, eFrame);\r\n\t\t}\r\n\t\t\r\n\t\tprivate function beginClass(e:Event):void{\r\n\t\t\t_root = MovieClip(root);\r\n\t\t\t\r\n\t\t\tif(_root.bellTotal == 1){\/\/if it's the first bell created\r\n\t\t\t\tthis.x = Math.random()*525;\/\/place it in a random spot on the stage\r\n\t\t\t\t_root.bellLastCoord = this.x;\r\n\t\t\t} else {\/\/otherwise, \r\n\t\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\r\n\t\t\t\tthis.x = _root.bellLastCoord + (Math.random()*500)-250;\r\n\t\t\t\tif(this.x > 537.5){\/\/if it is off the stage to the right\r\n\t\t\t\t\tthis.x -= 250;\/\/set it inside the stage\r\n\t\t\t\t} else if (this.x < 12.5){\/\/same with too far left\r\n\t\t\t\t\tthis.x += 250;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tthis.y = _root.bellTop;\/\/set the y's value off the stage\r\n\t\t}\r\n\t\tprivate function eFrame(e:Event):void{\r\n\t\t\tthis.y += 3;\/\/move the bell slowly downwards\r\n\t\t\tif(this.hitTestObject(_root.mcMain)){\/\/if this touches the main character\r\n\t\t\t\t_root.mainJumping = true;\/\/make him jump\r\n\t\t\t\t_root.jumpSpeed = _root.jumpSpeedLimit*-1;\/\/reset the jumpSpeed\r\n\t\t\t\t\r\n\t\t\t\tthis.removeEventListener(Event.ENTER_FRAME, eFrame);\/\/remove the listeners\r\n\t\t\t\t_root.bellHolder.removeChild(this);\/\/and finally remove him from the stage\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>That's some pretty intense code, eh? 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. It'll be very similar to the <tt>Bell<\/tt>, so don't worry.<\/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>Bird<\/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\">\r\n\/\/every 20 bells, we'll create a bird, not including the first one\r\nif(int(bellTotal\/20) == bellTotal\/20 && bellTime == 1 && bellTotal != 0){\r\n\tvar newBird:Bird = new Bird();\r\n\tbellHolder.addChild(newBird);\r\n}\r\n<\/pre>\n<p>Next, we'll create a <tt>Bird<\/tt> class, same as we did for the <tt>Bell<\/tt>. Create a new ActionScript File and name it \"Bird.as\". Now, place this code into it:<\/p>\n<pre lang=\"actionscript\">\r\npackage{\r\n\timport flash.display.MovieClip;\r\n\timport flash.events.*;\r\n\tpublic class Bird extends MovieClip {\r\n\t\t\r\n\t\tvar _root:Object;\/\/this will symbolize the main timeline\r\n\t\tvar speed:int = 5;\/\/how fast this bird will move side to side\r\n\t\t\r\n\t\tpublic function Bird() {\r\n\t\t\taddEventListener(Event.ADDED, beginClass);\r\n\t\t\taddEventListener(Event.ENTER_FRAME, eFrame);\r\n\t\t}\r\n\t\t\r\n\t\tprivate function beginClass(e:Event):void{\r\n\t\t\t_root = MovieClip(root);\r\n\t\t\t\r\n\t\t\tthis.x = -50;\/\/setting x value off the stage\r\n\t\t\tthis.y = _root.bellTop;\/\/set the y value off the stage\r\n\t\t}\r\n\t\tprivate function eFrame(e:Event):void{\r\n\t\t\tthis.y += 3;\/\/move the bell slowly downwards\r\n\t\t\t\r\n\t\t\tthis.x += speed;\/\/moving this guy based on it's speed\r\n\t\t\tif(this.x >= 537.5){\/\/if it goes too far right\r\n\t\t\t\tspeed = -5;\/\/make it go backwards\r\n\t\t\t\tscaleX = -1;\/\/and turn it around\r\n\t\t\t}\r\n\t\t\tif(this.x <= 12.5){\/\/same with too far left\r\n\t\t\t\tspeed = 5;\r\n\t\t\t\tscaleX = 1;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tif(this.hitTestObject(_root.mcMain)){\/\/if this touches the main character\r\n\t\t\t\t_root.mainJumping = true;\/\/make him jump\r\n\t\t\t\t_root.jumpSpeed = _root.jumpSpeedLimit*-1;\/\/reset the jumpSpeed\r\n\t\t\t\t\r\n\t\t\t\tthis.removeEventListener(Event.ENTER_FRAME, eFrame);\/\/remove the listeners\r\n\t\t\t\t_root.bellHolder.removeChild(this);\/\/and finally remove him from the stage\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\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-as3\/pt2\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as3\/pt2\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/mrsunstudios.com\/obj\/tuts\/winter-bells-as3\/pt2\/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 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,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\/1268"}],"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=1268"}],"version-history":[{"count":4,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1268\/revisions"}],"predecessor-version":[{"id":1341,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1268\/revisions\/1341"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1268"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}