通知拉伸方法停止對象。它存在且語法合理。
public class defaultObject
{
// variables are here
public defaultObject(Vector2 nPos, float nRotation, Texture2D nTexture, int nWidth, int nHeight)
{
// constructor business
}
public virtual void update()
{
rectangle = new Rectangle((int)position.X, (int)position.Y, width, height);
origin = new Vector2(texture.Width/2f, texture.Height/2f);
}
public virtual void draw(SpriteBatch spatch)
{
spatch.Draw(texture, rectangle, null, Color.Green, rotation, origin, SpriteEffects.None, 1f);
}
}
派生類
無需重寫draw方法,只有更新方法。
public class block: defaultObject
{
public block(Vector2 nPos, float nRotation, Texture2D nTexture, int nWidth, int nHeight)
: base(nPos, nRotation, nTexture, nWidth, nHeight)
{ }
public override void update()
{
if (rotation > (float)(Math.PI * 2))
{
rotation = 0;
}
if (rotation < 0)
{
rotation = (float)Math.PI * 2;
}
}
}
Game1.cs
假設所有其他的XNA必需品都在那裏,我簡單說明閱讀
問題:當我嘗試畫塊,它不會出現。爲什麼?
public class Game1 : Microsoft.Xna.Framework.Game
{
block a;
a = new block(new Vector2(windowSize.X/4f, windowSize.X/2), 3.1415f, rectangleTexture, 300, 50);
}
protected override void Update(GameTime gameTime)
{
//keyboard input to manipulate a is here
a.update();
}
protected override void Draw(GameTime gameTime)
{
a.draw(spriteBatch);
}
}
在我介紹這個繼承業務之前,該塊是可見的。
爲什麼defaultObject.draw()設置爲虛擬? – Scherling
我可能需要進一步將其更改爲開發。 – ShadowByte