如果你按下左右键,我有一个精灵从左到右移动到屏幕的底部。
我希望能够从屏幕底部移动的精灵拍摄一些东西(我想要的任何精灵)并让精灵直接向上移动。
我怎么能这样做?
答案 0 :(得分:1)
请勿复制并粘贴此内容,但在拆分这些类时,它会看起来像这样:
namespace SpaceInvadersGame
{
class Player : Microsoft.Xna.Framework.Game
{
Texture2D PlayerTexture;
Vector2 PlayerPosition;
public Player()
{
}
protected override void LoadContent()
{
PlayerTexture = Content.Load<Texture2D>(@"Images/freshman2");;
PlayerPosition = Vector2.Zero;
base.LoadContent();
}
public Vector2 GetPosition()
{
return this.PlayerPosition;
}
public void Update()
{
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Left))
freshamPos.X -= freshmanSpeed;
if (keyboardState.IsKeyDown(Keys.Right))
freshamPos.X += freshmanSpeed;
if(keyboardState.IsKeyDown(Keys.Space))
theBullet = new Bullet(this);
}
public void Draw(SpriteBatch SpriteBatch)
{
}
}
}
namespace SpaceInvadersGame
{
class Bullet : Microsoft.Xna.Framework.Game
{
Texture2D BulletTexture;
Vector2 BulletPosition;
Player thePlayer;
public Bullet(Player player)
{
this.thePlayer = player;
}
protected override void LoadContent()
{
BulletTexture = Content.Load<Texture2D>(@"Images/bullet");;
BulletPosition = thePlayer.GetPosition();
base.LoadContent();
}
public void Update()
{
//in here is where you would just do something like:
//BulletPosition.Y += 1;
}
public void Draw(SpriteBatch SpriteBatch)
{
}
}
}