I realized I put this in the wrong forum, so here it is:
I made a collision detection in the Update() section, and this is the code:
if (player.playerRect.Intersects(turtleRect))
{
turtleRect.X = turtleRect.X - 60;
}
When I run the game, and collide with the turtle, nothing happens. I tried changing the
turtleRect.X = turtleRect.X - 60;
To
turtleRect.X = turtleRect.X - 600;
Because I thought I wasn't noticing the change. Still, nothing happened. Then I thought it was just the
turtleRect.X = turtleRect.X - 600
So I put that in the Update() method, outside of the if statement and it worked. Now I realize that it is the
if (player.playerRect.Intersects(turtleRect)) {
}
But I cannot figure out what is wrong with that. I have the playerRect in a different class, but I imported it into the main class. I know the Rectangles are moving because of the
turtleRect.X -= 1;
in the Update() method. The turtle moves across the screen. I have tested many things, and looked it up and I still can't figure it out. This is the rectangle collision, so could somebody also guide me on how to do the per pixel collision? Here is my Draw() method:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(background, viewPortRect, Color.White);
spriteBatch.Draw(turtle, turtleRect, Color.White);
spriteBatch.Draw(turtleSmall, turtleRect2, Color.White);
spriteBatch.Draw(turtleSmall2, turtleRect3, Color.White);
spriteBatch.Draw(turtleSmall3, turtleRect4, Color.White);
player.Draw(spriteBatch);
spriteBatch.DrawString(font, "Level " + score, new Vector2(20, 10), Color.Blue);
spriteBatch.End();
base.Draw(gameTime);
}
Any help is appreciated.
If you need the whole class, here is Game1.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Game1
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D turtle;
Texture2D turtleSmall;
Texture2D turtleSmall2;
Texture2D turtleSmall3;
Texture2D background;
Rectangle viewPortRect;
Rectangle turtleRect;
Rectangle turtleRect2;
Rectangle turtleRect3;
Rectangle turtleRect4;
public Texture2D texture;
public Rectangle playerRect;
public Vector2 position;
public Vector2 origin;
public Vector2 velocity;
SpriteFont font;
Character player;
int score = 1;
public int currentFrame;
public int frameHeight;
public int frameWidth;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
player = new Character(Content.Load<Texture2D>("flame"), new Vector2(100, 100), 47, 44);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
background = Content.Load<Texture2D>("grass");
turtle = Content.Load<Texture2D>("turtle");
font = Content.Load<SpriteFont>("SpriteFont1");
turtleSmall = Content.Load<Texture2D>("turtleSmall");
turtleSmall2 = Content.Load<Texture2D>("turtleSmall");
turtleSmall3 = Content.Load<Texture2D>("turtleSmall");
viewPortRect = new Rectangle(0, 0, graphics.GraphicsDevice.Viewport.Width, graphics.GraphicsDevice.Viewport.Height);
turtleRect = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width + 200), (GraphicsDevice.Viewport.Height - 66) - (turtle.Height / 2), 100, 75);
turtleRect2 = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width), (GraphicsDevice.Viewport.Height - 40) - (turtle.Height / 2), 50, 38);
turtleRect3 = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width -50), (GraphicsDevice.Viewport.Height - 40) - (turtle.Height / 2), 50, 38);
turtleRect4 = new Rectangle((GraphicsDevice.Viewport.Width) - (turtle.Width - 100), (GraphicsDevice.Viewport.Height - 40) - (turtle.Height / 2), 50, 38);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//Turtle Movement
turtleRect.X -= 1;
turtleRect2.X -= 1;
turtleRect3.X -= 1;
turtleRect4.X -= 1;
//Turtle Collision
if (player.playerRect.Intersects(turtleRect))
{
turtleRect.X -= 500;
}
player.Update(gameTime);
base.Update(gameTime);
}
private Vector2 CenterTexture(Texture2D turtle)
{
return new Vector2((GraphicsDevice.Viewport.Width) - (turtle.Width), (GraphicsDevice.Viewport.Height - 66) - (turtle.Height / 2));
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(background, viewPortRect, Color.White);
spriteBatch.Draw(turtle, turtleRect, Color.White);
spriteBatch.Draw(turtleSmall, turtleRect2, Color.White);
spriteBatch.Draw(turtleSmall2, turtleRect3, Color.White);
spriteBatch.Draw(turtleSmall3, turtleRect4, Color.White);
player.Draw(spriteBatch);
spriteBatch.DrawString(font, "Level " + score, new Vector2(20, 10), Color.Blue);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Here is the Character.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace Game1
{
class Character
{
public Texture2D texture;
public Rectangle playerRect;
public Vector2 position;
public Vector2 origin;
public Vector2 velocity;
public int currentFrame;
public int frameHeight;
public int frameWidth;
float timer;
float interval = 75;
bool hasJumped;
public Character(Texture2D newTexture, Vector2 NewPosition, int newFrameHeight, int newFrameWidth)
{
position = NewPosition;
texture = newTexture;
frameHeight = newFrameHeight;
frameWidth = newFrameWidth;
hasJumped = true;
}
public void Update(GameTime gameTime)
{
position += velocity;
playerRect = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
origin = new Vector2(playerRect.Width / 2, playerRect.Height / 2);
position = position + velocity;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
velocity.X = 2f;
AnimateRight(gameTime);
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
velocity.X = -2f;
AnimateLeft(gameTime);
}
else
{
velocity.X = 0f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Space) && hasJumped == false)
{
position.Y -= 3f;
velocity.Y = -3.5f;
hasJumped = true;
}
if (hasJumped == true)
{
float i = 1;
velocity.Y += 0.15f * i;
}
if (position.Y + texture.Height >= 450)
{
hasJumped = false;
}
if (hasJumped == false)
{
velocity.Y = 0f;
}
}
public void AnimateRight(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 3)
{
currentFrame = 0;
}
}
}
public void AnimateLeft(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 7 || currentFrame < 4)
{
currentFrame = 4;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, playerRect, Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
}
}
}