2

从事迷宫游戏。当按下左键时,movieclip (char) 应该向左旋转 90 度。

如果我错了,请纠正我,但我认为我可以使用此代码;

char.scaleX *= -1;

然而,最重要的是角色没有穿过迷宫的墙壁。我认为这就是我实现上述代码的问题。因为当我放在这里时它不能正常工作;

    if(!mazehit) {

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


                }

我的问题是,我必须将代码放在哪里才能翻转影片剪辑?

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;


        }
    }
}

感谢您的时间

4

1 回答 1

3

我会根据速度更新方向:

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

或者,通过已按下的键:

if(keyLeft && !keyRight)
{
    char.scaleX = -1;
}
else if(keyRight && !keyLeft)
{
    char.scaleX = 1;
}
else
{
    // keep current direction
}
于 2011-10-05T10:46:50.213 回答