A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Player face direction using arrow keys?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8

    Player face direction using arrow keys?

    Hi!
    I wouldn't think this to be a very difficuly question to find a solution to. My question is; I have a movieclip with instance name "player" and I am trying to layout it's basic movement properties. I already have made it so that the mc will move up, down, left, and right. Cake. What I am having trouble with is getting the mc to rotate in the designated direction (right = 0 degrees, up = 90 degrees, etc).

    I have tried using the xscale 100/-100 strategy, but that doen't do anything for up and down movements.

    My code so far is as follows:

    Actionscript Code:
    playerDirection = 0;

    setInterval(function () {
    if (Key.isDown(Key.RIGHT)) {
    setDirection(0);
    _root.player._x += 3;
    }
    if (Key.isDown(Key.LEFT)) {
    setDirection(1);
    _root.player._x -= 3;
    }
    if (Key.isDown(Key.DOWN)) {
    _root.player._y += 3;
    }
    if (Key.isDown(Key.UP)) {
    _root.player._y -= 3;
    }
    updateAfterEvent();
    }, 10);

    function setDirection(param){
    if(param==0){
    player._xscale = 100
    }
    else {
    player._xscale = -100
    }
    }

    Any help is much appreciated

  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    Also:
    I'm not so much concerned with getting the mc to rotate as I am to get it to "snap" to a certain angle.

  3. #3
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Hello, and welcome to FlashKit Forums

    Ever heard of _rotation? It's exactly what you need

    Actionscript Code:
    setInterval(function () {
    if (Key.isDown(Key.RIGHT)) {
    _root.player._x += 3;
    _root.player._rotation = 90;
    }
    if (Key.isDown(Key.LEFT)) {
    _root.player._x -= 3;
    _root.player._rotation = 270;
    }
    if (Key.isDown(Key.DOWN)) {
    _root.player._y += 3;
    _root.player._rotation = 180;
    }
    if (Key.isDown(Key.UP)) {
    _root.player._y -= 3;
    _root.player._rotation = 0; // or 360
    }
    updateAfterEvent();
    }, 10);

    This is the first time I've seen someone use setInterval with character movement, and it's not a bad idea - the character really moved smoothly even though the Framerate was set to 12. But, if you want the character to also move and face diagonally, you'll have to use the onEnterFrame loop. Which is oftenly used with character movement and other stuff on movieclips. So a code like this, rather on the movieclip actions, would solve that problem:

    Actionscript Code:
    onClipEvent(load){
        speed = 10;
    }

    onClipEvent(enterFrame){
        if(Key.isDown(Key.RIGHT)){
            this._x += speed;
            this._rotation = 90;
        }
        if(Key.isDown(Key.LEFT)){
            this._x -= speed;
            this._rotation = 270;
        }
        if(Key.isDown(Key.DOWN)){
            this._y += speed;
            this._rotation = 180;
        }
        if(Key.isDown(Key.UP)){
            this._y -= speed;
            this._rotation = 0;
        }
        if(Key.isDown(Key.RIGHT) && Key.isDown(Key.DOWN)){
            this._rotation = 135;
        }
        if(Key.isDown(Key.DOWN) && Key.isDown(Key.LEFT)){
            this._rotation = 225;
        }
        if(Key.isDown(Key.LEFT) && Key.isDown(Key.UP)){
            this._rotation = 315;
        }
        if(Key.isDown(Key.UP) && Key.isDown(Key.RIGHT)){
            this._rotation = 45;
        }
    }

    The upper half is for the movements (the enterFrame loop will automatically cover the diagonal movement part), and the lower half is for making the player face diagonally when moving diagonally. Though, enterFrame is more efficient than setInterval, it doesn't run as smoothly, 'cause the updateAfterEvent() won't work there - so you'd manually have to change the Framerate!

    Hope this help you
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    This works excellently. I learned something new so that is good. Thank you so much!

  5. #5
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    Quote Originally Posted by Astrodynamic View Post
    This works excellently. I learned something new so that is good. Thank you so much!
    No problem, I also learned something new - movement with setInterval
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

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