2016-09-28 6 views
0

私は大学でプログラミングクラス用の小さなゲームを作成しようとしていますが、衝突のための矩形を作成しようとして問題が発生しています。コリジョン矩形を作成できません

私は自分のテクスチャから幅と高さを試してみます。浮動小数点から整数に変換できないというエラーが表示されます。しかし、画像のピクセルサイズは浮動小数点値ではありませんか?

class ButtonSprite 
{ 
    public Texture2D Art; 
    public Vector2 Position; 

    public ButtonSprite(Vector2 pos, Texture2D tex) 
    { 
     // Copy the texture "tex" into the "Art" class variable 
     Art = tex; 
     // Copy the vector "pos" into the "Position" class variable 
     Position = pos; 
    } 

    public void DrawMe(SpriteBatch sb, Color col) 
    { 
     // use the spritebatch "sb" to draw the sprite at "Position" using the texture "Art" with the tint from "col" 
     sb.Draw(Art, Position, col); 
    } 
} 

class PlayerSprite 
{ 
    public Texture2D Art; 
    public Vector2 Position; 
    public Rectangle CollisionRect; 

    public PlayerSprite(Vector2 pos, Texture2D tex) 
    { 
     // Copy the texture "tex" into the "Art" class variable 
     Art = tex; 
     // Copy the vector "pos" into the "Position" class variable 
     Position = pos; 
     // create a new CollisionRect Rectangle using the X and Y from Position and the Width and Height from Art 
     CollisionRect = new Rectangle(Position.X, Position.Y, Art.Width, Art.Height); 
    } 

    public void UpdateMe(ButtonState leftB, ButtonState rightB, ButtonState downB, ButtonState upB) 
    { 
     // if leftB is pressed 
     if (leftB == ButtonState.Pressed) 
     { 
      // subtract 1 from the X that belongs to Position 
      Position.X -= 1; 
     } 
     // endif 

     // if rightB is pressed 
     if (rightB == ButtonState.Pressed) 
     { 
      // add 1 to the X that belongs to Position 
      Position.X += 1; 
     } 
     // endif 

     // if downB is pressed 
     if (downB == ButtonState.Pressed) 
     { 
      // add 1 to the Y that belongs to Position 
      Position.Y += 1; 
     } 
     // endif 

     // if upB is pressed 
     if (upB == ButtonState.Pressed) 
     { 
      // subtract 1 from the Y that belongs to Position 
      Position.Y -= 1; 
     } 
     // endif 

     // set the X that belongs to CollisionRect to equal the integer version of the X that belongs to Position 
     // set the Y that belongs to CollisionRect to equal the integer version of the Y that belongs to Position 
    } 

    public void DrawMe(SpriteBatch sb) 
    { 
     // use the spritebatch "sb" to draw the sprite at "Position" using the texture "Art" with a white tint 
     sb.Draw(Art, Position, Color.White); 
    } 
} 
} 
+0

あなたのコードの最後に '}'がありますが、 'namespace {'部分 – MethodMan

答えて

0

右バットオフ、私はあなたが計算することをお勧め:ここ

は、私は私のゲームを持っているコードは、クラス(物事が行くように意図されている直接支援するための多くのコメントがあります)をオブジェクトでありますあなたの衝突も動きを使って。これにより、より多くのグリッチ(例えば、壁を通過する)を防止することができる。

これを行う良い方法は、ターゲット矩形を(一時的に)移動矩形のサイズだけ膨張させることです。次に、線と境界線の交点を実行します。これは、交差点を与え、グリッチに対して安全です。

0

衝突矩形を構築するときにこの行を試してください。これは、高さを&の幅の浮動小数点を整数に変換します。 + 1はオプションです。

CollisionRect = new Rectangle(Position.X, Position.Y, (int)Art.Width + 1, (int)Art.Height + 1); 
関連する問題