Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Loop rectangles?

Started by Juvelbagarn Jul 28, 2011 at 11:32 PM 7 replies 2.2k views
Original Post
Juvelbagarn
Juvelbagarn
Hello, I'm new to this forum and pretty new to C# and XNA (Still lerning a lot of C# and i'm NOT giving that shit up cus i KNOW i can lern it if i reallt try).

ive made a 2d sprite -map, more or less.. it looks like this when the loop is finishd:
############
#-----------------#
#-----------------#
#-----------------#
############

Its all 32x32px squares and the sprites are drawn verry nice.. The only problem is.. I dont know how to loop the rectangles so i can find out the bounds.
the only rectangle that work is the far bottom right, I think i know the problem but not how to solv it.

I think i need several new names for the rectangles or loop them.. If you would be kind to explain it to me it would be verry helpfull.

here are some of my code(Its all in the DRAW method):


for (int i = 0; i < levels.level_0.GetLength(0); ++i)
for (int j = 0; j < levels.level_0.GetLength(1); ++j)
{

Vector2 tilePos = new Vector2(j * 32, i * 32);

if (levels.level_0[i, j] == 1)
{
mazeBounds = new Rectangle((int)tilePos.X, (int)tilePos.Y, 32, 32);
spriteBatch.Draw(mapBrick, tilePos, Color.White);
}

}



UPPDATE, this is the code im working with, the other one was the old one, from some wierd tutorial. Om sorry to confuse you.
boogyman19946
boogyman19946
Oh wait, I figured out what you're trying to find. You're trying to find the positioning of the tiles right?
Yo dawg, don't even trip.
Juvelbagarn
Juvelbagarn

Oh wait, I figured out what you're trying to find. You're trying to find the positioning of the tiles right?


Yea, i want the rectangles to loop, just like the sprites..
boogyman19946
boogyman19946
Ok, so what exactly do mazeBounds do and why do you keep resetting their position to the last tile's position?
Yo dawg, don't even trip.
Jansic
Jansic

[quote name='boogyman19946' timestamp='1311908886' post='4841973']
Oh wait, I figured out what you're trying to find. You're trying to find the positioning of the tiles right?


Yea, i want the rectangles to loop, just like the sprites..
[/quote]

I'm not quite sure what you mean, but it looks like you want the rectangles to grow to cover all the different kinds of tiles. Maybe if you initialise the rectangles outside the loop to 'empty' then using Rectangle.Union to get what you want?

mazeBounds = new Rectangle();
...
mazeBounds = Rectangle.Union(mazeBounds, new Rectangle((int)tilePos.X, (int)tilePos.Y, 32, 32));

Jans.
Juvelbagarn
Juvelbagarn

Ok, so what exactly do mazeBounds do and why do you keep resetting their position to the last tile's position?


mazeBounds tells me where the blocks are, i don't WANT em to reset, i want to create new ones, but i dont know how to.
boogyman19946
boogyman19946
But you already have the tile position so why do you need it in a rectangle? What happens to the mazeBounds outside of the loop? On every iteration you create a new Rectangle and replace the previous mazeBounds with a new one that's set at the current tile position. So when you get to the last tile, that's the only one that's really saved. You'll have to explain exactly what this code is supposed to do before we can tell ya what's wrong with it
Yo dawg, don't even trip.
Juvelbagarn
Juvelbagarn
Jansic was on to something there.. i tried it but not with the result i wanted..

I simply want a rectangle for every sprite i draw, so that i make something happen when my mouse sprite touches (intersects) with the sprites.

Simply, a maze, you tuch the walls you die and have to restart from "start position".. I don't know how to explain any better. As i said before, im verry new to OOP and C#/XNA. But i'm still lerning.
boogyman19946
boogyman19946
Oh wait... I understand now. You want to create a shape to represent a specific group of tiles so you can check if the mouse intersects those tiles right? XD I don't know if there is any class that can do that, but you might want to experiment with the Unions. Otherwise, you can try this:

(I wrote this without the Vector2 variable but if you like to keep it, go ahead.)

Rectangle tileBounds = new Rectangle(0, 0, 32, 32);
for (int i = 0; i < levels.level_0.GetLength(0); ++i) {
for (int j = 0; j < levels.level_0.GetLength(1); ++j)
{

tileBounds.x = i*32;
tileBounds.y = j*32;
// I don't think it's needed that you create new variables on each cycle
// if you're not saving it anywhere

// if you want to check for collision of mouse and tile, you have to do it
// before tileBounds gets overwritten with the values for the next tile.
// you can handle it in the if statements (you can use a switch instead)
bool mouseIntersection = CheckMouseIntersectionFunction(tileBounds);

if (levels.level_0[i, j] == 1)
{
// if you pass decide to pass a Rectangle to this function, don't forget
// to change the parameters (if you're the one that wrote it, otherwise you'll
// have to use the Vector2 Class
spriteBatch.Draw(mapBrick, tileBounds, Color.White);

if(mouseIntersection) {
// Handle mouse intersection here or wherever
}
}

}
}

( i don't really know any C# XD)

In this case, you're basically moving a 32x32 rectangle onto each tile in sequence and checking the mouse coordinates intersect with the boundaries of the tiles.
Yo dawg, don't even trip.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.