我最近開始使用monogame,一切都很順利,直到發生這種情況。 我不是很有經驗所以這可能只是看起來愚蠢的,但我已經竭盡所能,這是我唯一的選擇,所以這是我的構造函數:Visual Studio無法識別我的構造函數?
public void qlayer(Vector2 position, Texture2D texture)
{
this.position = position;
this.texture = texture;
}
「qlayer」是我的課的名稱,但它一直說:「成員名稱不能和它們的封閉類型相同」,如果我不想構造一個構造函數,這將是有意義的!
爲安全起見,這裏的整個類:
class qlayer
{
Vector2 position;
Point speed;
Rectangle hitbox;
Texture2D texture;
Point currentFrame;
int timeSinceLastFrame = 0;
int millisecondsPerFrame;
bool isFront = true;
Point sheetSize = new Point(2,3);
public void qlayer(Vector2 position, Texture2D texture, SpriteBatch spritebatch)
{
this.position = position;
this.texture = texture;
}
enum PlayerAni
{
left, front, right
};
PlayerAni currentAni = PlayerAni.front;
public void Update(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame = 0;
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
++currentFrame.Y;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Left) || Keyboard.GetState().IsKeyDown(Keys.A))
{
position.X -= 3;
currentAni = PlayerAni.left;
}
else
{
if (Keyboard.GetState().IsKeyDown(Keys.Right) || Keyboard.GetState().IsKeyDown(Keys.D))
{
position.X += 3;
currentAni = PlayerAni.right;
}
else
{
currentAni = PlayerAni.front;
}
}
}
public void Draw(GameTime gameTime, SpriteBatch spritebatch)
{
spritebatch.Begin();
spritebatch.Draw(texture,
position, new Rectangle(currentFrame.X * 76,
currentFrame.Y * 54,
76, 54),
Color.White, 0, Vector2.Zero,
1f, SpriteEffects.None, 0);
spritebatch.End();
}
}
'公共無效qlayer(...)'不具有構造函數。語法錯誤:構造函數看起來像'public qlayer(...)' – elgonzo
刪除'void'關鍵字。 –