2017-10-23 5 views
0

というタイトルで、私はボンバーマンのクローンであるモノゲームでゲームを作ろうとしていると言いますが、それよりも多くのボンベをスポーンしようとすると、アニメーションはちょうど新しい場所に移動します。ゲームオブジェクトは想定された場所のままですが、アニメーションはありません。私は問題は、ロードされている同じアニメーションをロードすることですが、爆弾が出現するたびに新しいアニメーションを生成する方法を理解しているようです。 私はContent.Loadを使用してソートアニメーションを取得し、ボーンをclone()関数でアニメーションとともに生成します。複数のモノグラムをモノグラム同じアニメーション

var bombAni = new Dictionary<string, Animation>() { 
      {"Bomb", new Animation(Content.Load<Texture2D>("Obstacle/Bomb"), 3) }, 
     }; 

     _sprites = new List<Sprite>() { 

      new Player(animations) { 
       _bomb = new Bomb(bombAni), 
       Position = new Vector2(32, 32), 
       Input = new Input() { 
        Up = Keys.W, 
        Down = Keys.S, 
        Left = Keys.A, 
        Right = Keys.D, 
        Space = Keys.Space, 
       } 
      } 

     }; 

public void Play(Animation animation) { 
     if (animation == _animation) { 
      return; 
     } 

     _animation = animation; 

     _animation.CurFrame = 0; 

     _timer = 0; 
    } 

private void AddBomb(List<Sprite> sprites) { 

     var bomb = _bomb.Clone() as Bomb; 
     switch (_direction) { 
      case "Up": 
       bomb.Position = _position + new Vector2(0, -32); 
       break; 
      case "Down": 
       bomb.Position = _position + new Vector2(0, 32); 
       break; 
      case "Left": 
       bomb.Position = _position + new Vector2(-32, 0); 
       break; 
      case "Right": 
       bomb.Position = _position + new Vector2(32, 0); 
       break; 
     } 
     bomb.lifeSpan = 3f; 
     bomb.Parent = this; 
     bombCount++; 

     sprites.Add(bomb); 
    } 

public Sprite(Dictionary<string, Animation> animations) { 

     _animations = animations; 
     _animationManager = new AnimationManager(_animations.First().Value); 

    } 


namespace CompetenceWeek.scripts { 
public class Bomb : Sprite { 

    public float lifeSpan; 
    private float _timer; 

    public Bomb(Dictionary<string, Animation> animations) : base(animations) { 

    } 

    public Bomb(Texture2D texture) : base(texture) { 

    } 


    public override void Update(GameTime gameTime, List<Sprite> sprites) { 
     _timer += (float)gameTime.ElapsedGameTime.TotalSeconds; 

     SetAnimations(); 

     _animationManager.Update(gameTime); 

     if (_timer > lifeSpan) { 
      isDead = true; 
     } 

    } 

} 

}

namespace CompetenceWeek.scripts { 
public class Sprite : ICloneable { 

    protected AnimationManager _animationManager; 

    protected Dictionary<string, Animation> _animations; 

    protected Vector2 _position; 

    protected Texture2D _texture; 

    public bool Walkable { get; set; } = false; 

    public bool isDead = false; 


    public Player Parent { get; set; } 

    public Input Input; 

    public Vector2 Position { 
     get { return _position; } 
     set { 
      _position = value; 

      if(_animationManager != null) { 
       _animationManager.Posistion = _position; 
      } 
     } 
    } 

    public float Speed = 2.5f; 

    public Vector2 Velocity; 

    public virtual void Draw(SpriteBatch spriteBatch) { 

     if(_texture != null) { 
      spriteBatch.Draw(_texture, Position, Color.White); 
     } else if (_animationManager != null) { 
      _animationManager.Draw(spriteBatch); 
     } else { 
      throw new Exception("somthings not right"); 
     } 


    } 



    public Sprite(Dictionary<string, Animation> animations) { 

     _animations = animations; 
     _animationManager = new AnimationManager(_animations.First().Value); 

    } 

    public Sprite(Texture2D texture) { 
     _texture = texture; 
    } 

    public virtual void Update(GameTime gameTime, List<Sprite> sprites) { 
     SetAnimations(); 

     _animationManager.Update(gameTime); 

     Position += Velocity; 
     Velocity = Vector2.Zero; 


    } 

    protected virtual void SetAnimations() { 
     if (Velocity.X > 0) { 
      _animationManager.Play(_animations["PlayerRight"]); 
     } else if (Velocity.X < 0) { 
      _animationManager.Play(_animations["PlayerLeft"]); 
     } else if (Velocity.Y > 0) { 
      _animationManager.Play(_animations["PlayerDown"]); 
     } else if (Velocity.Y < 0) { 
      _animationManager.Play(_animations["PlayerUp"]); 
     } 
    } 

    public object Clone() { 
     return this.MemberwiseClone(); 
    } 
} 

}

+1

あなたは私たちとの関連コードを共有することはできますか? –

+0

それは今、申し訳ありませんが最初の投稿はここにあります^^; –

+0

Bombクラスのコードを表示できますか?あなたがクローン爆弾であるとき、あなたは既存の爆弾と同じアニメーションのインスタンスを参照しているように見えます。これは、シャロークローニングの問題の1つです。 –

答えて

0

問題は、このセグメントである:あなたがmemberwiseクローンを実行すると

protected Dictionary<string, Animation> _animations; 

public object Clone() { 
    return this.MemberwiseClone(); 
} 

、それは既存の参照を再利用し、簡易コピーを作成します参照型オブジェクトに変換します。これは、クローンが元のオブジェクトと同じアニメーション状態を共有することを意味します。

解決策は、爆弾を複製するたびに、すべてのアニメーションの新しいコピーをインスタンス化することです。このような

何か:

public object Clone() { 
    var bomb = (Bomb)this.MemberwiseClone(); 
    bomb.InitAnimations(new Dictionary<string, Animation>() { 
     {"Bomb", new Animation(Content.Load<Texture2D>("Obstacle/Bomb"), 3) }, 
    }; 
    return bomb; 
} 
+0

これは私が思っていることですが、それ以外の場所からgame.csクラスのContent.Loadにアクセスすることはできません。 –

+0

MonoGameを使って開発を進めているところでは、アプリケーションは、アプリケーションの他の部分で利用できます。これを行うには多くの方法があります。純粋な実装では、グローバルスペースで利用できるようにするだけです。https://github.com/EnigmaDragons/Astrocell/blob/master/MonoDragons.Core/Engine/GameInstance.csゲームインスタンスのコールを起動時に初期化することができます。 –

+0

さて、私はあなたがゲームインスタンスで何を理解するのに時間がかかったのですが、これを行うことで作業するようにしました public override object Clone(){ var bomb = new Bomb(new Dictionary (){ {"Bomb"、new Animation(GameInstance.TheGame.Content.Load ( "Obstacle/Bomb")、3)}、 }); リターン・ボム; } ありがとうございました! ^^ –

関連する問題