2016-09-28 2 views
0

J'essaie de faire un petit jeu pour ma classe de programmation au collège et j'ai un problème en essayant de créer un rectangle de collision.Impossible de créer un rectangle de collision

Lorsque j'essaie d'utiliser la largeur et la hauteur de ma texture. Je reçois une erreur me disant que je ne peux pas convertir d'un float à un int. Mais la taille de pixel de l'image n'est pas une valeur flottante?

Voici le code que j'ai dans mon jeu d'objets de classe (il y a beaucoup de commentaires pour aider directement où les choses sont censées aller):

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

vous avez un '}' supplémentaire à la fin de votre code avez-vous oublié d'inclure la partie 'namespace {' – MethodMan

Répondre

0

Dès le départ, je vous suggère de calculer vos collisions en utilisant le mouvement aussi. Ceci empêchera plus de pépins (par exemple en traversant les murs). Un bon moyen de faire ceci est de gonfler (temporairement) le rectangle cible par la taille du rectangle mobile. Ensuite, vous effectuez une ligne à l'intersection de la boîte englobante. Cela vous donnera un point d'intersection, tout en étant plus sûr contre les pépins. Essayez cette ligne lors de la construction de votre rectangle de collision.

0

Il convertira la hauteur & largeur flotteurs en entiers. Le + 1 est optionnel.

CollisionRect = new Rectangle(Position.X, Position.Y, (int)Art.Width + 1, (int)Art.Height + 1);