开发者

AS3 how can I stop rotation from looping?

开发者 https://www.devze.com 2023-04-11 16:39 出处:网络
Seen from above, my character movieclip moves horizontally and vertically. In starting position the characters nose is facing the top of the screen so when left key is pressed it should rotate 90 degr

Seen from above, my character movieclip moves horizontally and vertically. In starting position the characters nose is facing the top of the screen so when left key is pressed it should rotate 90 degrees to the left and face left + continue the movement facing left.

Because I use a movement function + the rotation it now kind off loops the 90 degree rotation and swirls around in circles to the left. I understand why this is happening, but my question to you is;

How can I make it rotate the degree angle only once, and continue the movement facing that direction?

var leftArrow:Boolean;
var speed:Number = 4;
var charRadius:Number = 10;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function keyPressed(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.LEFT) {
        leftArrow = true;
    }

}

function keyReleased(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.LEFT) {
        leftArrow = false;
    }
}

function everyFrame(event:Event):void {
    var mazehit:Bo开发者_C百科olean = false;
    if (leftArrow) {

        for(var i:int = 0; i < speed; i++) {
            if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {



                mazehit = true;
                break;
            }
        }
        if(!mazehit) {

            char.x -= speed;
            char.rotation -= 90;

        }


It may help to set the character state variables when the key is pressed and then use that state to animate the character.

For example, set the rotation and x and y velocity when the key is pressed, then use those values to update the characters x and y and rotation on the frame event.

var leftArrow:Boolean;
var rightArrow:Boolean;
var speed:Number = 4;
var vx:Number = 0;
var vy:Number = 0;
var heading:Number = 0;
var charRadius:Number = 10;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);

function keyPressed(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.LEFT) {
        vx = -speed;
        vy = 0;
        heading = 90;
        leftArrow = true;
    }
    else if (event.keyCode == Keyboard.RIGHT) {
        vx = speed;
        vy = 0;
        heading = -90;
        rightArrow = true;
    }


}

function keyReleased(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.LEFT) {
        leftArrow = false;
    }
    else if (event.keyCode == Keyboard.RIGHT) {
        rightArrow = false;
    }
}

function everyFrame(event:Event):void {
    var mazehit:Boolean = false;
    if (leftArrow || rightArrow) {

        for(var i:int = 0; i < speed; i++) {
            if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {
                mazehit = true;
                break;
            }
        }
        if(!mazehit) {

            char.x += vx;
            char.y += vy;
            char.rotation = heading;

        }
0

精彩评论

暂无评论...
验证码 换一张
取 消