我一直在研究角色移动,我创建了一种方法,可以在按下向上键时让角色跳跃,但角色的移动没有响应。
任何人都可以看到有什么问题吗?这是我的代码:
构造函数
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);