A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [CS3] How do I convert this from AS2 to AS3?

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    22

    [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;
    };
    }

  2. #2
    Señor Member Mavrisa's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    506
    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

  3. #3
    Señor Member Mavrisa's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    506
    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

  4. #4
    Señor Member Mavrisa's Avatar
    Join Date
    Oct 2005
    Location
    Canada
    Posts
    506
    sorry for the double post
    Haikus are easy
    But sometimes they don't make sense
    Refrigerator

  5. #5
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    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 
    37character.x-=speed; break;
        case 
    38character.y-=speed; break;
        case 
    39character.x+=speed; break;
        case 
    40character.y+=speed; break;
        default: 
    //do nothing
      
    }

    This has been tested and works.
    Follow me on Twitter: http://twitter.com/jasondefra

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center