• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
 
Gamemaking for beginners: The Gameloop
By flashbynight.comSource file: www.flashbynight.com/tutes/gameloop/gameloop.fla
When I started off making games in Flash, I didn’t yet realize the significance of having a gameloop.Now, it’s the first thing I implement when making a game.What is a gameloop? It is a continuous loop that runs in the background of a game and basically runs thewhole show. In this tutorial, we will implement a gameloop that controls the motion of ‘enemies’ acrossthe screen.Step 1Create a new flash file, click modify>document and select a stage size of 600 x 800 and set the framerate to 35 fps. This means that the display will be updated at a rate of 35 times per second, which will
 
make the motion within our game look smooth to the human eye. Lower than 35 can cause gamemotion to be jerky and a higher rate will only cause the game to run slower.Step 2Let’s create an enemy. Draw anything you like, and save it as a movieclip with the name ‘enemy’. Makesure that you set the linkage settings as below, and make sure you do NOT have an instance of theenemy on the screen, only in the library.Rather than have the enemy on the screen when we begin the game, we will create the enemies fromthe library as we go along. To do this, the linkage settings must be as above.
 
Step 3Now let’s get some coding done. We want a gameloop that will update the main features of the game atthe frame rate (frames displayed per second, which we have set to 35). Here is how we do it:addEventListener(Event.ENTER_FRAME,gameloop);function gameloop(event:Event) {}//gameloopFirst we add a listener, which ‘listens’ out for the frame being updated (or ‘entered’), and then we tie ina function to the listener. Anything within the curly brackets will occur 35 times per second. Let’s add atrace and try it out:addEventListener(Event.ENTER_FRAME,gameloop);function gameloop(event:Event) {trace(“gameloop”);}//gameloopRun the flash movie by pressing CTRL+ENTER and you should see “gameloop” being output at a rate of 35 times per second. We want to use the gameloop to control the motion of our game enemies, but firstwe need to display our enemies on the screen.Step 4We have a movieclip class ‘enemy’ in the library and we wish to add it to the screen. Here is how we doit. Add this line of code OUTSIDE the gameloop:var enemy1 =this.addChild(new enemy());We have cloned a ‘child’ of the movieclip in the library and placed it on the screen. You should see yourenemy in the top left corner of the screen. Since we haven’t specified where we want it to go, Flashautomatically sets the x and y coordinates to 0.
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...