{"id":1171,"date":"2009-01-24T08:05:31","date_gmt":"2009-01-24T12:05:31","guid":{"rendered":"http:\/\/www.mrsunstudios.com\/?p=1171"},"modified":"2022-05-29T08:23:30","modified_gmt":"2022-05-29T12:23:30","slug":"tutorial-make-a-rhythm-game-in-as3-part-5","status":"publish","type":"post","link":"http:\/\/www.mrsunstudios.com\/blog\/flash\/tutorial-make-a-rhythm-game-in-as3-part-5\/","title":{"rendered":"Tutorial: Make a Rhythm Game in AS3 &#8211; Part 5"},"content":{"rendered":"<div class=\"toc\">\n<ol>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3\/\">The Brainstorming<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3-part-2\/\">Create the Required Symbols\/Art<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3-part-3\/\">Programming the Arrows<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3-part-4\/\">Programming the Arrows Part 2<\/a><\/li>\n<li class=\"c_chap\"><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3-part-5\/\">Make a Level<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3-part-6\/\">Scoring<\/a><\/li>\n<li><a href=\"http:\/\/mrsunstudios.com\/2009\/01\/tutorial-make-a-rhythm-game-in-as3-part-7\/\">Finishing Touches<\/a><\/li>\n<\/ol>\n<\/div>\n<h3>Step 5: Make a Level <span style=\"font-weight:normal;font-size:15px;\">(or five)<\/span><\/h3>\n<p>Now that we&#8217;ve programmed the basic gameplay for our game, we can make a menu system with levels. It&#8217;s going to be very simple, only one page because we aren&#8217;t going to make many levels. If you want to paginate it yourself, go ahead. But, this isn&#8217;t the focus of this tutorial, so that will be up to you to figure out.<\/p>\n<p>Let&#8217;s first create the 5 levels that we&#8217;re going to need. Place this code in your first frame.<\/p>\n<pre lang=\"actionscript\">\r\n\/\/The array for the first level of the game\r\nvar lvlArray0:Array = new Array(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4);\r\nvar lvlArray1:Array = new Array(1,3,2,4,1,3,2,4,1,3,2,4,1,3,2,4);\r\nvar lvlArray2:Array = new Array(4,3,2,1,4,3,2,1,4,3,2,1,4,3,2,1);\r\nvar lvlArray3:Array = new Array(4,2,3,1,4,2,3,1,4,2,3,1,4,2,3,1);\r\nvar lvlArray4:Array = new Array(2,1,4,3,2,1,4,3,2,1,4,3,2,1,4,3);\r\n\/\/The names of all of the levels\r\nvar lvlNames:Array = new Array('level1','level2','level3','level4','level5');\r\n\/\/The array holding all of the lvl arrays\r\nvar lvlArrayAll:Array = new Array(lvlArray0,lvlArray1,lvlArray2,lvlArray3,lvlArray4);\r\n<\/pre>\n<p>The levels aren&#8217;t too complicated, just patterns of the four arrows repeated a few times. Now, we have to make the menu system. First of all, we have to create another layer where we will place all of the labels. Now, we can separate the frames a bit so we can actually see the labels when we make them. I&#8217;m going to label the first one &#8220;begin&#8221; and the game frame &#8220;game&#8221;.<\/p>\n<p>Next, create a &#8220;menu&#8221; frame in between the first frame and the game frame. So far, the frames should look like this:<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/rhythm-as3\/pt5\/frames.gif\" alt=\"How the frame layout should be\" \/><\/p>\n<p>Ok, now for the menu. First, we&#8217;re going to have to put a <tt>stop();<\/tt> in the &#8220;menu&#8221; actions so we can actually use it. Then, on the top of the stage, we&#8217;re going to type in the following words: &#8220;Select a Level:&#8221;. Then, we have to create a MovieClip which is the level select button. Just draw a rectangle (mine&#8217;s 250x50px) and convert it to a MovieClip. We&#8217;re using a MovieClip so we can add actions to it. We&#8217;re going to name it <tt>btnSongSelect<\/tt>. We also have to export it for ActionScript. You can do this in the &#8220;Convert to Symbol&#8221; window by click on &#8220;Advanced&#8221;. Check off &#8220;Export for ActionScript&#8221; and leave everything else as it is.<\/p>\n<p>Next, inside the symbol, add a dynamic text field that&#8217;s about the same height and width of the rectangle itself. In the area that says &#8220;&lt;Instance Name&gt;&#8221;, type in <tt>txtName<\/tt>. This is what your symbol should look like so far:<br \/>\n<img src=\"http:\/\/mrsunstudios.com\/obj\/tuts\/rhythm-as3\/pt5\/the-button.gif\" alt=\"What your button should look like\" \/><\/p>\n<p>Now, make a new layer above &#8220;Layer 1&#8221; and add the following code onto the first frame:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/lvlID will be passed into this MovieClip\r\n\/\/when we dynamically place it onto the stage\r\n\/\/It is the index number of the level and name arrays\r\nvar lvlID:int;\r\n\/\/lvlName is the variable that holds the name of the\r\n\/\/level that this button navigates to\r\nvar lvlName:String = MovieClip(root).lvlNames[lvlID];\r\n\/\/makes it so this movieclip doesn't keep on playing\r\nvar destroy:Boolean = false;\/\/whether or not to destroy this\r\nstop();\r\n\r\n\/\/setting the text\r\ntxtName.text = 'Level '+lvlID;\r\n\r\nthis.addEventListener(MouseEvent.CLICK,clickThis);\r\n\r\nfunction clickThis(e:MouseEvent):void{\r\n\t\/\/if the user clicks on this button\r\n\t\/\/make the current level the one that this represents\r\n\tMovieClip(root).lvlCurrent = lvlID;\r\n\t\/\/go to the game frame\r\n\tMovieClip(root).gotoAndStop('game');\r\n\t\/\/and remove the buttons from the stage\r\n\t\/\/(we'll define this function in a bit)\r\n\tMovieClip(root).removeButtons();\r\n}\r\n\r\nthis.addEventListener(Event.ENTER_FRAME, eFrame);\r\n\r\nfunction eFrame(e:Event):void{\r\n\tif(destroy){\/\/if destroy is set to true\r\n\t\tthis.parent.removeChild(this);\/\/then remove this\r\n\t\t\/\/and remove all listeners as well\r\n\t\tremoveEventListener(MouseEvent.CLICK,clickThis);\r\n\t\tremoveEventListener(Event.ENTER_FRAME, eFrame);\r\n\t}\r\n}\r\n<\/pre>\n<p>As always, I&#8217;ve commented this code pretty extensively. Now, we have to add actions to the main timeline that will actually place the buttons onto the stage and make a function to remove them when one is clicked:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/stop the frame\r\nstop();\r\n\r\n\/\/this loop will create the buttons\r\nfor(var i:int=0;i<lvlArrayAll.length;i++){\r\n\t\/\/set a movieclip variable to be the button\r\n\tvar newButton:MovieClip = new btnSongSelect();\r\n\taddChild(newButton);\r\n\t\/\/the _x value will be 130, centered\r\n\tnewButton.x = 130;\r\n\t\/\/the y value will be at a minimum of 70 and then continue\r\n\t\/\/to be added by 60 every subsequent button\r\n\tnewButton.y = 70+60*i;\r\n\t\/\/define the id of this button, or which level it\r\n\t\/\/represents\r\n\tnewButton.lvlID = i;\r\n}\r\n\r\n\/\/this function will remove all of the buttons from the stage\r\nfunction removeButtons():void{\r\n\t\/\/we're going to use the same loop\r\n\tfor(var i:int=0;i<numChildren;i++){\r\n\t\tvar remove = getChildAt(i);\r\n\t\t\/\/set the target's destroy variable to true\r\n\t\tremove.destroy = true;\r\n\t}\r\n}\r\n<\/pre>\n<p>Now, when you run this code, it should work fine, until the game finishes and we are returned back to the menu. We have to make some changes to the code. Do you remember this at around line 80?<\/p>\n<pre lang=\"actionscript\">\r\n\/\/get the next arrow if it the song isn't finished\r\nif(sArrow < lvlArrayAll[lvlCurrent].length){\r\n\tsArrow ++;\r\n} else {\r\n\t\/\/if the song is finished, then reset the game\r\n\t\/\/of course, we don't have the code for that yet\r\n\t\/\/so we're just going to go back to the first frame\r\n\tgotoAndPlay(1);\r\n\tgameIsOver = true;\r\n}\r\n<\/pre>\n<p>Well, we can change a few bits of it to make this:<\/p>\n<pre lang=\"actionscript\">\r\n\/\/get the next arrow if it the song isn't finished\r\nif(sArrow < lvlArrayAll[lvlCurrent].length){\r\n\tsArrow ++;\r\n} else {\r\n\t\/\/if the song is finished, then reset the game\r\n\tgotoAndStop('menu');\r\n\tgameIsOver = true;\r\n\t\/\/then remove this enter_frame listener\r\n\tremoveEventListener(Event.ENTER_FRAME, makeLvl);\r\n}\r\n<\/pre>\n<p>Well, that's all we're going to do for this lesson. Next time, we'll add some scoring methods and then change that part of the code even more (but just the <tt>gotoAndStop()<\/tt> part.<\/p>\n<h4>Final Product<\/h4>\n<p><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:\/\/www.mrsunstudios.com\/obj\/tuts\/rhythm-as3\/pt5\/source.swf\" \/><embed type=\"application\/x-shockwave-flash\" width=\"550\" height=\"400\" src=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/rhythm-as3\/pt5\/source.swf\"><\/embed><\/object><\/p>\n<p><a href=\"http:\/\/www.mrsunstudios.com\/obj\/tuts\/rhythm-as3\/pt5\/source.zip\">Source Files (zipped)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Brainstorming Create the Required Symbols\/Art Programming the Arrows Programming the Arrows Part 2 Make a Level Scoring Finishing Touches Step 5: Make a Level (or five) Now that we&#8217;ve programmed the basic gameplay for our game, we can make a menu system with levels. It&#8217;s going to be very simple, only one page because [&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":[25,8,19,18,68,70,69,11],"_links":{"self":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1171"}],"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=1171"}],"version-history":[{"count":13,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1171\/revisions"}],"predecessor-version":[{"id":1223,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/posts\/1171\/revisions\/1223"}],"wp:attachment":[{"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/media?parent=1171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/categories?post=1171"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.mrsunstudios.com\/blog\/wp-json\/wp\/v2\/tags?post=1171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}