AS3 character movement from one frame to next
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: AS3 character movement from one frame to next

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1

    AS3 character movement from one frame to next

    Hi

    I am quite new to AS3 and am really enjoying learning it.
    My problem is I need to move a character from one frame to another.

    I know how to move him and make him go from one frame to the next scene by having him move with the arrow keys to a hit point, but I cant figure out how to do this below

    If the character walks right to the end of the stage he should appear on the left of the next frame. If
    he walks to the left of the stage he should appear on the right of the previous scene

    Any help would be great, thanks guys.

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    106
    You could handle this inside of your key pressed object, and see use Logic to see if he has moved off of the stage width. For example, if your stage is 550px wide, when the x position of your character is greater than 550, then you would go to Scene 2. When the position is less than 0 - character.width then go to past scene.

    This code currently works for two scenes. If you have more than this, you will need to add logic to determine which sceen you are in:
    var amtMoved:int = 5;
    var guyX:Number;

    function keyPressed(event:KeyboardEvent):void
    {
    guyX=guy.x
    if (event.keyCode == 40)
    {
    trace("down");
    guy.y = guy.y + amtMoved;
    }
    if (event.keyCode == 38)
    {
    trace("up");
    guy.y = guy.y - amtMoved;
    }
    if (event.keyCode == 37)
    {
    trace("left");
    guy.x = guy.x - amtMoved;
    }
    if (event.keyCode == 39)
    {
    trace("right");
    guy.x = guy.x + amtMoved;
    }
    if (guy.x > 550)
    {
    gotoAndStop(1,"Scene 2");
    guy.x = guyX-550;

    }
    if (guy.x < 0-guy.width)
    {
    gotoAndStop(1,"Scene 1");
    guy.x = guyX+550;
    }
    }
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

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

Poll by Flashkit.com