views:

133

answers:

1

I'm trying to make it where the character is in a tile and when they move up or down it moves to the next tile but I'm not sure how to do that. Right now, I have it set up where the character moves by pixels but I want it to move by 1 square.

The code right now is this, and it works, but it's glitchy in pixel mode. I believe if it was by blocks it might work better but I might change it anyway.

float spritewidth  = sprite->stretchX;
float spriteheight = sprite->stretchY;
float bushwidth  = bush->stretchX;
float bushheight = bush->stretchY;
//Basic border collision
if (sprite->x <= 0)
 sprite->x = 0;

if (sprite->y <= 0)
 sprite->y = 0;

if (sprite->x >= 455)
 sprite->x = 455;

if (sprite->y >= 237)
 sprite->y = 237;

if ( (sprite->x + spritewidth > bush->x) && (sprite->x < bush->x + bushwidth) && (sprite->y + spriteheight > bush->y) && (sprite->y < bush->y + bushheight) ) 
{
         bushcol = 1;               
}
else
{
        bushcol = 0;      
}

if (osl_keys->held.down)
{
if (bushcol == 1) 
{
sprite->y = bush->y - spriteheight - 3;
        bushcol = 0; 
}
else
{
        bushcol = 0; 
        sprite->y += 3;
}
}
if (osl_keys->held.up)
{
 if (bushcol == 1) 
{
    sprite->y = bush->y + bushheight + 3;
    bushcol = 0;
}
    else
{ 
        bushcol = 0; 
        sprite->y -= 3;
}
}
if (osl_keys->held.right)
{
 if (bushcol == 1) 
{
    sprite->x = bush->x - spritewidth - 3;
    bushcol = 0;
}
    else
{ 
         bushcol = 0; 
    sprite->x += 3;}
}
if (osl_keys->held.left)
{
        if (bushcol == 1) 
{
    sprite->x = bush->x + bushwidth + 3;
    bushcol = 0; 
}
    else
{ 
        bushcol = 0; 
        sprite->x -= 3;
}
}
+2  A: 

If you want the character to move one tile/square/block at a time, just move the sprite the number of pixels the tile is wide (or tall).

const int tile_width = 32; // or something

// and then
sprite->x += tile_width;
Andreas Magnusson