从旋转的值方向移动精灵?

时间:2012-02-23 10:05:48

标签: c# .net xna

我是XNA的新手,我正在构建一个小行星游戏。我有一些问题需要让我的飞船朝着我想要的方向前进。当我按下向上箭头键时,我希望引擎启动并将太空船的方向与太空船的旋转方向相同?帮助是精确的!我添加了我的宇宙飞船的子类。 Draw方法及其变量在主类中。我不确定EngineOn方法中有什么用?

 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()
    {

    }
}

1 个答案:

答案 0 :(得分:3)

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

P.S。您的速度取决于CPU,将您的速度乘以deltaTime

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