3D-kreativ
3D-kreativ

Reputation: 9299

Move sprite in the direction of the value from the rotation?

I'm new to XNA and I'm building an Asteroids game. I have some problems to get my spaceship to move forward in the direction I want. When I press the arrow up key, I want the engine to start and move the space ship in the same directions as the rotation of the spaceship? Help is preciated! I add the subclass that is my spaceship. The Draw method and its variables are in the main class. I'm not sure what to have in the EngineOn method?

 class Spaceship: GameObject
{

    // Konstruktor
    public Spaceship(Texture2D texture, Vector2 position): base(texture, position)
    {

    }

    public override void Update()
    {
        direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
        position += direction * speed;  //position = position + direction
        //base.Update();
    }

    // Metod som beräknar rymdskeppets färd i riktningen
    public void EngineOn()
    {
        Update();
    }

    // Metod som beräknar rymdskeppets rotation motsols
    public void RotateLeft()
    {
        rotation -= rotationSpeed;
    }

    // Metod som beräknar rymdskeppets rotation medsols
    public void RotateRight()
    {
        rotation += rotationSpeed;
    }

    // Metod som beräknar bana för avlossade skott
    public void Fire()
    {

    }
}

Upvotes: 4

Views: 3427

Answers (1)

AvrDragon
AvrDragon

Reputation: 7479

public void EngineOn()
{
    speed = 20;
}
public void EngineOff()
{
    speed = 0;
}

P.S. Your speed is CPU is dependent, multiply your speed on deltaTime

public override void Update(GameTime gameTime)
{
    direction = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation));
    position += direction * speed * gameTime.ElapsedGameTime.Milliseconds;
}

Upvotes: 3

Related Questions