0

我一直在研究角色移动,我创建了一种方法,可以在按下向上键时让角色跳跃,但角色的移动没有​​响应。
任何人都可以看到有什么问题吗?这是我的代码:

构造函数

public PlayerSprite(string name, string magicTextureName,Vector2 position, Vector2 velocity, AnimatedTextureData textureData, SpritePresentationInfo spritePresentationInfo,SpritePositionInfo spritePositionInfo, Keys leftKey, Keys rightKey, int frameRate, int startFrame, bool bRepeatAnimation)
  : base(name, textureData, spritePresentationInfo, spritePositionInfo, frameRate, startFrame, bRepeatAnimation)

接受位置和速度。

这是在更新。

if (SpriteManager.GAME.KEYBOARDMANAGER.isFirstKeyPress(Keys.Up))
{
    bPause = false;
    updateJump();
}

更新跳转函数

 public void updateJump()
 {
     jumpPosition += jumpVelocity;
     if (Keyboard.GetState().IsKeyDown(Keys.Right))
         jumpVelocity.X = 3f;
     else if (Keyboard.GetState().IsKeyDown(Keys.Left))
         jumpVelocity.X = -3f;
     else
         jumpVelocity.X = 0f;

     if (Keyboard.GetState().IsKeyDown(Keys.Up))
     {
         jumpPosition.Y -= 10f;
         jumpVelocity.Y -= 5f;
         hasJumped = true;
     }
     if (hasJumped == true)
     {
         float i = 1;
         jumpVelocity.Y += 0.15f * i;
     }
     if (jumpPosition.Y + animatedTextureData.Height() >= 450)
     {
         hasJumped = false;
     }
     if (hasJumped == false)
     {
         jumpVelocity.Y = 0f;
     }
 }

我在主要的地方称之为

 AnimatedTextureData playerAnimatedTextureData = (AnimatedTextureData)textureManager.Get("PlayerAnimation");
 SpritePresentationInfo playerAnimatedPresentationInfo = new SpritePresentationInfo(playerAnimatedTextureData.FULLSOURCERECTANGLE, 1);
 SpritePositionInfo playerAnimatedPositionInfo = new SpritePositionInfo(new Vector2(100, 700), playerAnimatedTextureData.Width(), playerAnimatedTextureData.Height(), 0, 2, playerAnimatedTextureData.CENTREORIGIN);

 this.playerSprite = new PlayerSprite("PlayerAnimation", "FireballAnimation", new Vector2(50, 50), new Vector2(50, 50), playerAnimatedTextureData, playerAnimatedPresentationInfo, playerAnimatedPositionInfo,Keys.Left, Keys.Right, 10, 0, true);
 spriteManager.Add(playerSprite);
4

1 回答 1

3

我的猜测:我不知道这个函数被编程做什么,但如果它只检测到按下向上键,那么跳转在第一帧之后不再更新。

if (SpriteManager.GAME.KEYBOARDMANAGER.isFirstKeyPress(Keys.Up))
{
    bPause = false;
    updateJump();
}

一旦你更正了这个并且在每个更新周期都会调用 updateJump() 函数,那么只要你按住向上键,火球就会很快地离开屏幕。

于 2013-11-06T08:47:58.893 回答