PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
My character zooms off the screen when I put a loop in.
A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: My character zooms off the screen when I put a loop in.

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    2

    My character zooms off the screen when I put a loop in.

    Hello there I am making a small game in Flash using AS3.
    My code allows you to be able to move your character the arrow keys, I put a loop in so that the character will move a lot more smoothly. but with this loop in my character just flys of the stage when i run the application.
    Actionscript Code:
    import flash.events.KeyboardEvent;
    import flash.events.Event;

    var xVelocity:int = 10;
    var yVelocity:int = 10;

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);

    function keyHit(event:KeyboardEvent):void
    {

        switch (event.keyCode)
        {
            case Keyboard.RIGHT :
                player1.x +=  xVelocity;
                break;
            case Keyboard.LEFT :
                player1.x -=  xVelocity;
                break;
            case Keyboard.UP :
                player1.y -=  yVelocity;
                break;
            case Keyboard.DOWN :
                player1.y +=  yVelocity;
                break;
        }
    }
    stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);

    function onFrameLoop(evt:Event):void
    {
        player1.x +=  xVelocity;
        player1.y -=  yVelocity;
    }

    ^^ is my loop
    When running the game there are no errors what so ever the character just zooms off the stage :/ Could do with a reply ASAP
    thanking you.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You are moving your character on each frame and on each keypress.

    If you only want it to move when you hold a key down, you will need to have a listener for key_down which sets the xVelocity or yVelocity (and does not move the character directly), and a listener for key_up which unsets the velocity. And your velocities should start at 0.

    If you want some other effect, you'll have to explain what you want.

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