Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – Moving the character


In order to do so, we are going to await the ENTER_FRAME event that is triggered every time a new frame is drawn on the screen (frame drawing is automatically handled in Flash and you do not have to handle it through a loop like in other languages).

We will then move the character on each frame depending on its speed. Let's add the following to the Game class:

import flash.events.Event;

class Game
{
   //previous code
   public static function main(): Void
   {
      //previous code
      //Add an event handler for every frame drawing    flash.Lib.current.stage.addEventListener(flash.events.Event.ENTER_FRAME, enterFrame);
   }
   
   //previous code   
   private static function enterFrame(args : Dynamic)
   {
      player.x += horizontalSpeed;
   }
}

What just happened?

With this code, the character will move on each frame depending on its speed.