我有一个2D rpg-ish的事情,当我按下某个键时,我希望我的角色挥剑,剑挥动由3个精灵组成。
这是我到目前为止所做的代码: 编辑:根据要求,我已经在我的攻击动画代码所在的地方进行了复制。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace RPGJensLomanderV2
{
class Player : Characters
{
KeyboardState ks;
Camera camera;
KeyboardState oldKs;
int attackTime;
Vector2 origin = Vector2.Zero;
bool isAnimationRunning = false;
public Player(GameScreen gameScreen, Camera camera)
: base(gameScreen)
{
health = 10;
this.camera = camera;
direction = Vector2.Zero;
movementSpeed = 1;
currentSprite = new Rectangle(229, 30, 14, 16);
}
public override void Load(ContentManager content)
{
spawnPoint = gameScreen.destSourceRecISE.Where(t => t.Value.X == 78 && t.Value.Y == 26).First().Key;
position = spawnPoint;
gameScreen.destSourceRecISE.Remove(spawnPoint);
base.Load(content);
}
public override void Update(GameTime gameTime)
{
ks = Keyboard.GetState();
timeSprite += gameTime.ElapsedGameTime.Milliseconds;
Attack(gameTime);
attackTime = 0;
Movement(gameTime);
camera.Position = position;
oldKs = ks;
}
private void Movement(GameTime gameTime)
{
if (ks.IsKeyDown(Keys.Right) && ks.IsKeyDown(Keys.Up))
{
direction = new Vector2(1, -1);
directionPointer = 1;
if (currentSprite.X != 140 && currentSprite.X != 154)
currentSprite = new Rectangle(140, 30, 14, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 140)
currentSprite = new Rectangle(154, 30, 14, 16);
else if (currentSprite.X == 154)
currentSprite = new Rectangle(140, 30, 14, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Right) && ks.IsKeyDown(Keys.Down))
{
direction = new Vector2(1, 1);
directionPointer = 2;
if (currentSprite.X != 229 && currentSprite.X != 245)
currentSprite = new Rectangle(229, 30, 16, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 229)
currentSprite = new Rectangle(245, 30, 16, 16);
else if (currentSprite.X == 245)
currentSprite = new Rectangle(229, 30, 16, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Left) && ks.IsKeyDown(Keys.Up))
{
direction = new Vector2(-1, -1);
directionPointer = 1;
if (currentSprite.X != 140 && currentSprite.X != 154)
currentSprite = new Rectangle(140, 30, 14, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 140)
currentSprite = new Rectangle(154, 30, 14, 16);
else if (currentSprite.X == 154)
currentSprite = new Rectangle(140, 30, 14, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Left) && ks.IsKeyDown(Keys.Down))
{
direction = new Vector2(-1, 1);
directionPointer = 2;
if (currentSprite.X != 229 && currentSprite.X != 245)
currentSprite = new Rectangle(229, 30, 16, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 229)
currentSprite = new Rectangle(245, 30, 16, 16);
else if (currentSprite.X == 245)
currentSprite = new Rectangle(229, 30, 16, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Left))
{
direction = new Vector2(-1, 0);
directionPointer = 3;
if (currentSprite.X != 198 && currentSprite.X != 214)
currentSprite = new Rectangle(198, 30, 16, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 198)
currentSprite = new Rectangle(214, 30, 15, 16);
else if (currentSprite.X == 214)
currentSprite = new Rectangle(198, 30, 16, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Right))
{
direction = new Vector2(1, 0);
directionPointer = 4;
if (currentSprite.X != 168 && currentSprite.X != 184)
currentSprite = new Rectangle(168, 30, 16, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 168)
currentSprite = new Rectangle(184, 30, 16, 16);
else if (currentSprite.X == 184)
currentSprite = new Rectangle(168, 30, 16, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Up))
{
direction = new Vector2(0, -1);
directionPointer = 1;
if (currentSprite.X != 140 && currentSprite.X != 154)
currentSprite = new Rectangle(140, 30, 14, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 140)
currentSprite = new Rectangle(154, 30, 14, 16);
else if (currentSprite.X == 154)
currentSprite = new Rectangle(140, 30, 14, 16);
timeSprite = 0;
}
}
else if (ks.IsKeyDown(Keys.Down))
{
direction = new Vector2(0, 1);
directionPointer = 2;
if (currentSprite.X != 229 && currentSprite.X != 245)
currentSprite = new Rectangle(229, 30, 16, 16);
if (timeSprite >= 100)
{
if (currentSprite.X == 229)
currentSprite = new Rectangle(245, 30, 16, 16);
else if (currentSprite.X == 245)
currentSprite = new Rectangle(229, 30, 16, 16);
timeSprite = 0;
}
}
else
{
direction = Vector2.Zero;
timeSprite = 0;
}
position += movementSpeed * direction;
}
private void Attack(GameTime gameTime)
{
if (ks.IsKeyDown(Keys.A) /*&& oldKs.IsKeyUp(Keys.A)*/)
{
isAnimationRunning = true;
}
while (isAnimationRunning && directionPointer == 1)
{
attackTime += gameTime.ElapsedGameTime.Milliseconds;
if (attackTime > 1 && attackTime < 100)
{
currentSprite = new Rectangle(3, 46, 32, 16);
}
if (attackTime < 200 && attackTime > 100)
{
currentSprite = new Rectangle(33, 61, 32, 32);
}
if (attackTime > 200 && attackTime < 300)
{
currentSprite = new Rectangle(61, 65, 16, 32);
}
if (attackTime > 300)
{
attackTime = 0;
currentSprite = new Rectangle(140, 30, 14, 16);
isAnimationRunning = false;
}
}
if (directionPointer == 2 && isAnimationRunning)
{
attackTime += gameTime.ElapsedGameTime.Milliseconds;
if (attackTime < 100)
{
currentSprite = new Rectangle(4, 67, 32, 16);
}
else if (attackTime < 200 && attackTime > 100)
{
currentSprite = new Rectangle(35, 66, 32, 32);
}
else if (attackTime > 200 && attackTime < 300)
{
currentSprite = new Rectangle(69, 66, 16, 32);
}
else if (attackTime > 300)
{
attackTime = 0;
isAnimationRunning = false;
currentSprite = new Rectangle(229, 30, 16, 16);
}
}
if (directionPointer == 3 && isAnimationRunning)
{
attackTime += gameTime.ElapsedGameTime.Milliseconds;
if (attackTime < 100)
{
currentSprite = new Rectangle(5, 138, 16, 32);
}
else if (attackTime < 200 && attackTime > 100)
{
currentSprite = new Rectangle(20, 138, 32, 32);
}
else if (attackTime > 200 && attackTime < 300)
{
currentSprite = new Rectangle(53, 154, 32, 16);
}
else if (attackTime > 300)
{
attackTime = 0;
isAnimationRunning = false;
currentSprite = new Rectangle(198, 30, 16, 16);
}
}
if (directionPointer == 4 && isAnimationRunning)
{
attackTime += gameTime.ElapsedGameTime.Milliseconds;
if (attackTime < 100)
{
currentSprite = new Rectangle(5, 102, 16, 32);
}
else if (attackTime < 200 && attackTime > 100)
{
currentSprite = new Rectangle(20, 102, 32, 32);
}
else if (attackTime > 200 && attackTime < 300)
{
currentSprite = new Rectangle(51, 118, 32, 16);
}
else if (attackTime > 300)
{
attackTime = 0;
currentSprite = new Rectangle(168, 30, 16, 16);
isAnimationRunning = false;
}
}
}
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, currentSprite, Color.White, 0f, origin, 1f, SpriteEffects.None, 0f);
}
}
}
现在,当我挥杆时,它并没有超过攻击的第一个精灵,而且我不完全确定如何实现它。如果有人可以给我一个小费或类似的说明我可以通过整个代码来完成一个按钮的全面展开,我将不胜感激?先谢谢!
答案 0 :(得分:1)
定义全局变量
bool isAnimationRunning = false;
然后像这样更改你的代码:
if (ks.IsKeyDown(Keys.A) && directionPointer == 1 && oldKs.IsKeyUp(Keys.A))
{
isAnimationRunning = true;
}
if(isAnimationRunning)
{
attackTime += gameTime.ElapsedGameTime.Milliseconds;
if (attackTime > 1 && attackTime < 100)
{
currentSprite = new Rectangle(3, 46, 32, 16);
}
else if (attackTime < 200 && attackTime > 100)
{
currentSprite = new Rectangle(33, 61, 32, 32);
}
else if (attackTime > 200 && attackTime < 300)
{
currentSprite = new Rectangle(61, 65, 16, 32);
}
else if (attackTime > 300)
{
attackTime = 0;
isAnimationRunning = false;
}
}