Cartman999
Lieutenant
- Registriert
- Aug. 2007
- Beiträge
- 802
Ich bekomme es nicht hin eine neue Instanz von meiner Animation-Klasse zu initialisieren. Ich gehe davon aus dass es an der Syntax liegt. Jedoch habe ich durch googeln kein ähnliches Beispiel gefunden(nur Beispiele ohne Parameter). Ich möchte die Parameter von Animation in der Game1 Klasse manchmal ändern.
Animation-Klasse:
Game1 Klasse:
In der letzten Zeile von Game1(animation=...) bekomme ich folgende 3 Fehlermeldungen:
Invalid token '=' in class, struct, or interface member declaration
Method must have a return type
Identifier expected
Was ist da falsch?
Animation-Klasse:
Code:
public class Animation
{
private int _animIndex;
public TimeSpan PassedTime { get; private set; }
public Rectangle[] SourceRects { get; private set; }
public Texture2D Texture {get; private set; }
public TimeSpan Duration { get; private set; }
public Animation(Rectangle[] sourceRects, Texture2D texture, TimeSpan duration)
{
for (int i = 0; i < sourceRects.Length; i++)
{
sourceRects[i] = new Rectangle((sourceRects.Length - 1 - i) * (Texture.Width / sourceRects.Length), 0, Texture.Width / sourceRects.Length, Texture.Height);
}
SourceRects = sourceRects;
Texture = texture;
Duration = duration;
}
public void Update(GameTime dt)
{
PassedTime += dt.ElapsedGameTime;
if (PassedTime > Duration)
{
PassedTime -= Duration; // zurücksetzen
}
var percent = PassedTime.TotalSeconds / Duration.TotalSeconds;
_animIndex = (int)Math.Round(percent * (SourceRects.Length - 1));
}
public void Draw(SpriteBatch batch)
{
batch.Draw(Texture, new Rectangle(0, 0, Texture.Width / SourceRects.Length, Texture.Height), SourceRects[_animIndex], Color.White);
}
}
Code:
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Animation animation;
Texture2D enemy;
Rectangle[] enemyframes;
TimeSpan time;
animation = new Animation(enemyframes, enemy, time);
Invalid token '=' in class, struct, or interface member declaration
Method must have a return type
Identifier expected
Was ist da falsch?