1

サークル内に敵をスポーンする方法 (XNA プログラミング) について誰か教えてください。

ウィンドウの境界のすぐ外側にある円の円周に沿って敵をランダムにスポーンさせたい. ウィンドウの中央を通って、開始位置の反対側 (またはできるだけ近く) に直線で移動するようにします。

理想的には、これにより、一見全方向から敵がランダムにやってくる環境が作成されます。

これが私の敵(「バディ」)クラスです。SetupTraveling ゲーム ステートでの位置を処理します。私がやっていることは実際には機能していないので、どんな助けでも大歓迎です。

public class Baddies : Sprite
{
    public enum State
    {
        Inactive,
        SetupTraveling,
        Traveling,
        SetupInactive,
    }

    public State CurrentState
    {
        set
        {
            currentState = value;
            framesInStateCount = 0;
        }
        get
        {
            return currentState;
        }
    }

    int framesInStateCount = 0;
    State currentState = State.SetupInactive;    

    public Baddies()
    {
        Image = getImage("Bad");
        Scale = .2f;
        Rotation = 0f;
        DRotation = .05f;
        TurnedOn = true;
        BounceOn = false;
        WrapOn = false;
        Gravity = 0f;
    }

    public override void Update()
    {
        framesInStateCount++;
        switch (currentState)
        {
            case State.Inactive:
                if (RandOneIn(100)) CurrentState = State.SetupTraveling;
                break;
            case State.SetupTraveling:
                        PositionX = ((Game1.vGameWidth + 100)/2) * (float)Math.Cos(Glob.rnd(0.0, 2.0 * Math.PI));
                        PositionY = ((Game1.vGameHeight + 100)/2) * (float)Math.Sin(Glob.rnd(0.0, 2.0 * Math.PI));                        
                        DDPositionY = 0;
                        DPositionY = -1;
                        DPositionX = 1f;
                        DDPositionX = 0f;                          

                CurrentState = State.Traveling;
                break;
            case State.Traveling:
                if (PositionX > Game1.vGameWidth + (Image.Width / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionX < -500f - (Image.Width / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionY > Game1.vGameHeight + (Image.Height / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                if (PositionY < 0 - (Image.Height / 2) * Scale)
                {
                    currentState = State.SetupInactive;
                }
                break;
            case State.SetupInactive:
                PositionX = -300f;
                DPositionX = 0f;
                DPositionY = 0f;
                DDPositionX = 0f;
                CurrentState = State.Inactive;

                break;                
        }     

        base.Update();
    } 
}
4

2 に答える 2

4

三角法によるその他のアプローチ:

  1. 円の半径を決定します...通常は次のとおりです。

    raqdius = sqrt(Viewport.Size.Width^2 + Viewport.Size.Height^2) / 2;
    
  2. ランダムな角度を生成する

    angle = (float) Random.NextDouble() * MathHelper.PI * 2; 
    
  3. あなたの座標は

    x = ViewPort.Center.X + radius * Math.Cos(angle);
    y = ViewPort.Center.Y + radius * Math.Sin(angle);
    
于 2012-12-10T22:36:24.457 に答える
1

これはどう:

    1. あなたの円の半径が何であるかを決定し、それをrと呼びます
    2. [-r, r] から X 値を生成する
    3. Y = sqrt(r^2 - X^2)
    4. Y を Y または -Y にランダムに設定します。あなたの座標はX、Yになります

于 2012-12-10T20:50:44.590 に答える