开发者

As3 How to flip a movieclip to face movement direction?

Working on a maze game. When the left开发者_StackOverflowkey is pressed the movieclip (char) should turn 90 degrees to the left.

Correct me if i'm wrong but I thought I could use this code;

char.scaleX *= -1;

However, the most important thing is that the character doesnt go through the walls of the maze. And I think thats my problem for implementing the code above. Because it doesnt work properly when i put in here;

    if(!mazehit) {

                    char.y += speed;
                    char.scaleX *= -1;


                }

My question to you is, where do I have to put the code to flip the movieclip?

var leftArrow, rightArrow, upArrow, downArrow: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;
    }
    if (event.keyCode == Keyboard.RIGHT) {
        rightArrow = true;
    }
    if (event.keyCode == Keyboard.UP) {
        upArrow = true;
    }
    if (event.keyCode == Keyboard.DOWN) {
        downArrow = true;
    }
}

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

function everyFrame(event:Event):void {
    var mazehit:Boolean = 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;




        }

    } else if (rightArrow) {
        for(var j:int = 0; j < speed; j++) {
            if(bounds.hitTestPoint(char.x + charRadius + j, char.y, true)) {

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

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

                mazehit = true;
                break;
            }
        }
        if(!mazehit) {
            char.y -= speed;
        }

    } else if (downArrow) {

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

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

            char.y += speed;


        }
    }
}

thank you for your time


I would update the direction depending on the speed:

char.scaleX = (speed > 0) ? 1 : -1;

Or, by the keys that have been pressed:

if(keyLeft && !keyRight)
{
    char.scaleX = -1;
}
else if(keyRight && !keyLeft)
{
    char.scaleX = 1;
}
else
{
    // keep current direction
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜