AS3 如何停止循环旋转?

发布于 2024-12-08 10:44:31 字数 1113 浏览 0 评论 0原文

从上面看,我的角色动画剪辑水平和垂直移动。 在起始位置,角色的鼻子面向屏幕顶部,因此当按下左键时,它应该向左旋转 90 度并面向左 + 继续朝左移动。

因为我使用了移动函数 + 旋转,所以现在它会进行 90 度旋转并向左旋转。 我理解为什么会发生这种情况,但我对你的问题是;

如何让它只旋转一次角度,并继续朝着那个方向运动?

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: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;
            char.rotation -= 90;

        }

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: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;
            char.rotation -= 90;

        }

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

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

发布评论

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

评论(1

勿忘初心 2024-12-15 10:44:31

当按下按键时设置角色状态变量,然后使用该状态来为角色设置动画可能会有所帮助。

例如,设置按下按键时的旋转以及 x 和 y 速度,然后使用这些值更新帧事件上的字符 x 和 y 以及旋转。

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;

        }

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;

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