Ref: XNA 4.0 Your First Game - XNA Game Studio in 2D
1. 準備 Test1.png
2. 方案總管 Content (右建)加入 現有項目(精靈圖檔Test1.png)
3. LoadContent 方法
3.1 宣告全域變數
Texture2D myTexture; //3-1
Vector2 spritePosition = Vector2.Zero; // 3-2
Vector2 spriteSpeed = new Vector2(250.0f, 0f); // 精靈速度 3-3
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
myTexture = Content.Load<Texture2D>("Test1"); // 3-4
}
4. Draw 方法
4.1-4.3 繪出精靈(Sprite)
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend); // 4.1
spriteBatch.Draw(myTexture, spritePosition, Color.White); // 4.2
spriteBatch.End(); // 4.3
5. 移動精靈(Sprite)指令
// TODO: Add your update logic here
UpdateSprite(gameTime); // 5.1
// -------- 自建更新方法
base.Update(gameTime);
}
void UpdateSprite(GameTime gameTime)
{
// Move the sprite by speed, scaled by elapsed time.
spritePosition += ... 略
沒有留言:
張貼留言