As3 如何翻转影片剪辑以面向运动方向?

发布于 2024-12-08 18:56:12 字数 2645 浏览 0 评论 0原文

正在开发迷宫游戏。当按下左键时,影片剪辑(字符)应向左旋转 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;


        }
    }
}

谢谢您的宝贵时间

Working on a maze game. When the leftkey 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-12-15 18:56:12

我会根据速度更新方向:

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

或者,通过按下的键:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文