


October 07, 2014 – Migrating from AS2 to AS3, EnterFrame Event
So, I decided to finally write a special serie of Migrating from ActionScript 2 to ActionScript 3, because, there are a lot of developers using ActionScript 2 yet, afraid of changing to ActionScript 3.
If you are using ActionScript 2 and you do not want to change to ActionScript 3, it's not a problem, but I recommend learning ActionScript 3 because it's OO (Object-Oriented) and you can make a more clear and optimized code for your games.
In this tutorial, I will show how to create the event EnterFrame used mainly to Game Loop. So, if you are familiarized with ActionScript 2, you probably use the following statement inside a Movie Clip in the stage, to declare the Game Loop:
onClipEvent(enterFrame) {
// codes of the game loop here
}
To make this in ActionScript 3, you need to add an event listener, just use the code below in the action area, of some frame:
function GameLoop(e:Event):void {
// codes of the game loop here
}
addEventListener(Event.ENTER_FRAME,GameLoop);
The name of the function doesn't need to be GameLoop, you can use the name you want, at the moment it's easy, but don't forget if you change the frame of the main timeline, you will need to remove all event listeners declared, just change the add to remove, example:
function GameLoop(e:Event):void {
gotoAndPlay(5);
removeEventListener(Event.ENTER_FRAME,GameLoop);
}
addEventListener(Event.ENTER_FRAME,GameLoop);
If you don't remove the listener, the code can glitch in the future, because it will keep running, even if you are in another frame.
Well, I hope this part of tutorial helps you, in the next posts I will write about: buttons and another things that you will need to know when changing from ActionScript 2 to ActionScript 3.
If you are using ActionScript 2 and you do not want to change to ActionScript 3, it's not a problem, but I recommend learning ActionScript 3 because it's OO (Object-Oriented) and you can make a more clear and optimized code for your games.
In this tutorial, I will show how to create the event EnterFrame used mainly to Game Loop. So, if you are familiarized with ActionScript 2, you probably use the following statement inside a Movie Clip in the stage, to declare the Game Loop:
onClipEvent(enterFrame) {
// codes of the game loop here
}
To make this in ActionScript 3, you need to add an event listener, just use the code below in the action area, of some frame:
function GameLoop(e:Event):void {
// codes of the game loop here
}
addEventListener(Event.ENTER_FRAME,GameLoop);
The name of the function doesn't need to be GameLoop, you can use the name you want, at the moment it's easy, but don't forget if you change the frame of the main timeline, you will need to remove all event listeners declared, just change the add to remove, example:
function GameLoop(e:Event):void {
gotoAndPlay(5);
removeEventListener(Event.ENTER_FRAME,GameLoop);
}
addEventListener(Event.ENTER_FRAME,GameLoop);
If you don't remove the listener, the code can glitch in the future, because it will keep running, even if you are in another frame.
Well, I hope this part of tutorial helps you, in the next posts I will write about: buttons and another things that you will need to know when changing from ActionScript 2 to ActionScript 3.
© PlayGB.com - Free online games - Blog
