【问题标题】:Character jumping while moving right and left AS3左右移动时角色跳跃 AS3
【发布时间】:2017-12-22 06:17:51
【问题描述】:

我正在制作一个主角四处移动和跳跃的平台游戏。

我希望角色分别左右跳跃。也许同时使用两把钥匙,然后落在地板上。我的角色影片剪辑符号是Naruto,我的地板影片剪辑符号是floor

我的项目文件可以在这里找到:Naruto Game

为了做到这一点,我有一个主影片剪辑,其中包含所有其他影片剪辑,例如 “向右跳”“向左跳”

我遇到的问题是,当用户向右移动时,我希望角色在跳跃时面向右侧(左侧也是如此)。

 import flash.events.KeyboardEvent;
 import flash.ui.Keyboard;
 import flash.display.MovieClip;
 import flash.events.Event;
 import flash.display.Stage;

 naruto.gotoAndStop("stance");
 var rightPressed: Boolean = new Boolean(false);
 var leftPressed: Boolean = new Boolean(false);
 var upPressed: Boolean = new Boolean(false);
 var downPressed: Boolean = new Boolean(false);
 var narutoSpeed: Number = 10;
 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
 stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
 stage.addEventListener(Event.ENTER_FRAME, gameLoop);


 function keyDownHandler(keyEvent: KeyboardEvent): void {
    if (keyEvent.keyCode == Keyboard.RIGHT) {
        rightPressed = true;
    } else if (keyEvent.keyCode == Keyboard.LEFT) {
        leftPressed = true;
    } else if (keyEvent.keyCode == Keyboard.UP) {
        upPressed = true;
    } else if (keyEvent.keyCode == Keyboard.DOWN) {
        downPressed = true;
    }

 }

 function keyUpHandler(keyEvent: KeyboardEvent): void {
    if (keyEvent.keyCode == Keyboard.RIGHT) {
        rightPressed = false;
        naruto.gotoAndStop("standright")
    } else if (keyEvent.keyCode == Keyboard.LEFT) {
        leftPressed = false;
        naruto.gotoAndStop("standleft")
    } else if (keyEvent.keyCode == Keyboard.UP) {
        upPressed = false;
        naruto.gotoAndStop("stance")
    } else if (keyEvent.keyCode == Keyboard.DOWN) {
        downPressed = false;
        naruto.gotoAndStop("stance")
    }

 }

 function gameLoop(loopEvent: Event): void {
    if (rightPressed) {
        naruto.x += narutoSpeed;
        naruto.gotoAndStop("right");


    } else if (leftPressed) {
        naruto.x -= narutoSpeed;
        naruto.gotoAndStop("left");

    } else if (upPressed) {

        naruto.gotoAndStop("jumpright");

    }

 }

感谢能解决这个问题的人,我已经努力解决这个问题一个星期了!非常感谢!

【问题讨论】:

  • 请编辑您的问题以包含您尝试使用的代码。虽然指向您的项目的链接可能会有所帮助,但并不是每个人都愿意从陌生人那里下载文件,并且解决您的问题所需的一切都应该包含在问题本身中。
  • 好的,先生,我将添加我的代码。
  • 你知道如何解决这个问题吗?
  • 我仍然很难理解问题所在。您希望必须按下两个键进行左/右行走并且您不知道如何处理同时击键?还是您遇到了某种错误或意外结果?
  • 不,我希望角色在必要时跳跃时面朝右,必要时跳跃时面左。

标签: actionscript-3 flash animation game-physics


【解决方案1】:

要解决这个问题,您需要将您的 else 语句更改为附加的if 语句(这样您就可以同时获得两个动作 - 跳转和右移,而不是其中一个)。

这是一个代码示例:

function gameLoop(loopEvent: Event): void {
    //If the right key is pressed, and the left key is NOT pressed.
    if (rightPressed && !leftPressed) {
        naruto.x += narutoSpeed;
        naruto.gotoAndStop("right");
    }

    if(leftPressed && !rightPressed) {
        naruto.x -= narutoSpeed;
        naruto.gotoAndStop("left");
    }

    if (upPressed) {
        //if up is pressed, and right is pressed
        if(rightPressed && !leftPressed) naruto.gotoAndStop("jumpright");
        if(leftPressed && !rightPressed) naruto.gotoAndStop("jumpleft");
    }

    if(leftPressed && rightPressed && !upPressed){
        naruto.gotoAndStop("stance");  //if left & right are pressed, they negate each other and you play stance
    }
}

使用此代码,如果按下右键,naruto 将向右移动。如果向上也按下了,那么除了向右移动之外,它还会从right状态切换到jumpright状态。

【讨论】:

  • 你能回答我的问题吗
  • 你能回答我的问题吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
相关资源
最近更新 更多