Skip to main content
Participant
September 15, 2013
Question

Converting AS2 to AS3 Please!

  • September 15, 2013
  • 1 reply
  • 1139 views

Hello,

I am really new to ActionScript, infact I have just started learning it today.

I found a tutorial on AS2 on YouTube but there are barely any on AS3.

Could someone please convert all of this code to AS3 so I can start the project on which I am learning from.

Thanks

The main instances are the character's instance name which is 'char' and the ground's instance name which is 'ground' if that is needed.

This is for a small platformer game that is on youtube at this link: https://www.youtube.com/watch?v=US2P89m8GV0

Here is the code:

(Character)


onClipEvent (load) {

var ground:MovieClip = _root.ground;

var grav:Number = 0;

var gravity:Number = 2;

var speed:Number = 7;

var maxJump:Number = -12;

var touchingGround:Boolean = false;

}

onClipEvent (enterFrame) {

_y += grav;

grav += gravity;

while (ground.hitTest(_x, _y, true)) {

_y -= gravity;

grav = 0;

}

if (ground.hitTest(_x, _y+5, true)) {

touchingGround = true;

} else {

touchingGround = false;

}

if (Key.isDown(Key.RIGHT)) {

_x += speed;

}

if (Key.isDown(Key.LEFT)) {

_x -= speed;

}

if (Key.isDown(Key.UP) && touchingGround) {

grav = maxJump;

}

if (ground.hitTest(_x+(_width/2), _y-(_height/2), true)) {

_x -= speed;

}

if (ground.hitTest(_x-(_width/2), _y-(_height/2), true)) {

_x += speed;

}

if (ground.hitTest(_x, _y-(height), true)) {

grav = 3;

}

}

(VCAM Code)


onClipEvent (enterFrame) {

_y += (_root.char._y-_y)/4;

_x += (_root.char._x-_x)/4;

}


(Reset Symbol)


onClipEvent (enterFrame) {

if (_root.char.hitTest(this)) {

_root.char._x = charX

_root.char._y = charY

}

}

This topic is closed to new replies. Start a new post to keep the conversation going.

1 reply

kglad
Community Expert
Community Expert
September 15, 2013

most of the work needs to be done in the ide and that can be explained on a forum, but cannot be done for you.  in particular, you must remove all code from objects. ie, all the onClipEvent() code must be moved from the objects to frames.  to do that each of those objects should be given instance names and those names must be used correctly in the code.

to change the code you showed from as2 to as3, change all _x,_y,_width,_height to x,y,width,height.   change hitTest() to hitTestPoint() and use an eventlistener to trigger the enterframe loop and you'll need to use a keyboardevent listener to detect the keypresses.

// if you use instance name Character


var ground:MovieClip = MovieClip(root).ground;

var grav:Number = 0;

var gravity:Number = 2;

var speed:Number = 7;

var maxJump:Number = -12;

var touchingGround:Boolean = false;

}

this.addEventListener(Event.ENTER_FRAME,enterframeF);


function enterframeF(e:Event):void{

etc