Sprite 拍摄雪碧照片

Sprite 拍摄雪碧照片,sprite,xna-4.0,Sprite,Xna 4.0,我有一个精灵在屏幕底部从左到右移动,如果你按下左键和右键 我希望能够从屏幕底部移动的精灵中拍摄一些东西(任何我想要的精灵),让精灵直接向上移动 我怎样才能做到这一点呢?不要完全复制和粘贴这些内容,但当您拆分这些类时,会出现类似的情况: namespace SpaceInvadersGame { class Player : Microsoft.Xna.Framework.Game { Texture2D PlayerTexture; Ve

我有一个精灵在屏幕底部从左到右移动,如果你按下左键和右键

我希望能够从屏幕底部移动的精灵中拍摄一些东西(任何我想要的精灵),让精灵直接向上移动


我怎样才能做到这一点呢?

不要完全复制和粘贴这些内容,但当您拆分这些类时,会出现类似的情况:

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)
        {

        }
    }
}
名称空间游戏
{ 
职业玩家:Microsoft.Xna.Framework.Game
{ 
纹理2D播放器纹理;
矢量2播放位置;
公共玩家()
{ 
} 
受保护的覆盖void LoadContent()
{ 
PlayerTexture=Content.Load(@“Images/freshman2”);;
PlayerPosition=Vector2.0;
base.LoadContent();
}
公共向量2 GetPosition()
{
返回此.PlayerPosition;
}
公共无效更新()
{ 
KeyboardState KeyboardState=Keyboard.GetState();
if(keyboardState.IsKeyDown(Keys.Left))
freshamPos.X-=新鲜速度;
if(keyboardState.IsKeyDown(Keys.Right))
freshamPos.X+=新鲜速度;
if(keyboardState.IsKeyDown(Keys.Space))
项目符号=新项目符号(本);
} 
公共作废抽签(SpriteBatch SpriteBatch)
{ 
} 
} 
}
命名空间空间游戏
{
类项目符号:Microsoft.Xna.Framework.Game
{
纹理2d,纹理;
矢量2位置;
玩家和玩家;
公共子弹(玩家)
{
this.thePlayer=玩家;
}
受保护的覆盖void LoadContent()
{
BulletTexture=Content.Load(@“图像/项目符号”);;
BulletPosition=thePlayer.GetPosition();
base.LoadContent();
}
公共无效更新()
{
//在这里,您只需执行以下操作:
//位置Y+=1;
}
公共作废抽签(SpriteBatch SpriteBatch)
{
}
}
}

当按下射击按钮时,你应该在玩家的坐标处创建一个新的子弹对象,并在你想要的方向上给它一个速度。你能详细解释一下吗?或者有关于这个的教程吗?我刚刚开始学习XNA,这就是为什么我一无所知。我已经在谷歌上搜索过了,这些例子通常显示他们已经完成的当前项目或其他不同类别的项目。我只想加载一个精灵,当我按下空格键时,只需让精灵出现在我移动的精灵的位置,然后让精灵向上移动,直到它碰到什么东西或离开屏幕。好吧,首先你必须知道用户按下了空格键。你做到了吗?如果(keyboardState.IsKeyDown(Keys.Space)),那就照我说的做吧,酷,所以现在你需要一个shot()方法。在调用Shoot()的“if”语句中。