-
[CS3] How do I convert this from AS2 to AS3?
I want to do this in AS3, but I don't know how, can someone help me?
Code:
onClipEvent (load) {
var speed:Number = 10;
onEnterFrame = function () {
_root.character._x += Key.isDown(Key.RIGHT)*speed;
_root.character._x -= Key.isDown(Key.LEFT)*speed;
_root.character._y += Key.isDown(Key.DOWN)*speed;
_root.character._y -= Key.isDown(Key.UP)*speed;
};
}
-
Señor Member
A few notes:
1) AS3 does not accept actionscript on movieclip.
2) AS3 does not handle events (in your case, enterframe) in the same way.
3) underscores (_) have been taken out of properties.
4) there is no Key.isDown method anymore. However there are classes that emulate this functionality.
1) place your code on the main timeline, or in a document class. It is a good idea to have all of your code either in classes or in the same place.
2) instead of calling the onEnterFrame function and have it just magically call the correct function you have to set up an even listener:
addEventListener(Event.ENTER_FRAME, loop)
and then declare the listening function:
function loop(e:Event):void
{
//code
}
3)the new properties are .root (not usually needed, especially if the code is all in one place), .x and .y
4) http://www.senocular.com/flash/actio...s/KeyObject.as
This is Senocular's class that I personally use (with a few modifications). There are pages he has written on how to use it. Either that or look up another class on google.
Good luck
Haikus are easy
But sometimes they don't make sense
Refrigerator
-
Señor Member
A few notes:
1) AS3 does not accept actionscript on movieclip.
2) AS3 does not handle events (in your case, enterframe) in the same way.
3) underscores (_) have been taken out of properties.
4) there is no Key.isDown method anymore. However there are classes that emulate this functionality.
1) place your code on the main timeline, or in a document class. It is a good idea to have all of your code either in classes or in the same place.
2) instead of calling the onEnterFrame function and have it just magically call the correct function you have to set up an even listener:
addEventListener(Event.ENTER_FRAME, loop)
and then declare the listening function:
function loop(e:Event):void
{
//code
}
3)the new properties are .root (not usually needed, especially if the code is all in one place), .x and .y
4) http://www.senocular.com/flash/actio...s/KeyObject.as
This is Senocular's class that I personally use (with a few modifications). There are pages he has written on how to use it. Either that or look up another class on google.
Good luck
Haikus are easy
But sometimes they don't make sense
Refrigerator
-
Señor Member
sorry for the double post
Haikus are easy
But sometimes they don't make sense
Refrigerator
-
Bald By Choice
You've got a pretty primitive way of moving your character (better to use timers for movement than frame progression), but here's what the translation will be. Note what Mavrisa said, you can't place AS on display objects any more, so this'll go in an Actions layer/file.
PHP Code:
var speed:Number=10;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyPressed,false,0,true);
function keyPressed(e:KeyboardEvent):void{ switch(e.keyCode){ case 37: character.x-=speed; break; case 38: character.y-=speed; break; case 39: character.x+=speed; break; case 40: character.y+=speed; break; default: //do nothing } }
This has been tested and works.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|