如果您按下左右键,我有一个精灵会在屏幕底部从左向右移动。
我希望能够从屏幕底部移动的精灵中拍摄一些东西(我想要的任何精灵),并让该精灵直接向上。
我怎么能这样做?
不要完全复制和粘贴它,但是当你拆分这些类时它会看起来像这样:
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)
{
}
}
}