1

我试图让它在角色在瓷砖中的位置,当他们向上或向下移动时,它会移动到下一个瓷砖,但我不知道该怎么做。现在,我将它设置为角色移动像素的位置,但我希望它移动 1 个正方形。

现在的代码是这样的,它可以工作,但是在像素模式下会出现故障。我相信如果它是块它可能会更好,但无论如何我可能会改变它。

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;
}
}
4

1 回答 1

2

如果您希望角色一次移动一个图块/正方形/块,只需将精灵移动图块宽(或高)的像素数。

const int tile_width = 32; // or something

// and then
sprite->x += tile_width;
于 2009-05-15T22:37:08.847 回答