0

我将 oslib 与 pspsdk 工具链一起使用,由于某种原因,这并没有按照我认为的方式工作

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;

 //Bush
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 == 0)
   {
     sprite->y += 4;
     sprite_position = DOWN;
     SpriteAnimate();
   }
   else
   { 
     sprite->y -= 6;
     bushcol = 0;
   }
 }

 if (osl_keys->held.up)    
 {
   if (bushcol == 0)
   {
     sprite->y -= 4;
     sprite_position = UP;
     SpriteAnimate();
   }
   else
   { 
     sprite->y += 6;
     bushcol = 0;
   }
 }

 if (osl_keys->held.right)
 {
   if (bushcol == 0)
   {
     sprite->x += 4;
     sprite_position = RIGHT;
     SpriteAnimate();
   }
   else
   { 
     sprite->x -= 6;
     bushcol = 0;
   }
 }

 if (osl_keys->held.left)
 {
   if (bushcol == 0)
   {
     sprite->x -= 4;
     sprite_position = LEFT;
     SpriteAnimate();
   }
   else
   { 
     sprite->x += 6;
     bushcol = 0;
   }
 }

当我试图离开时,精灵开始向与灌木相反的方向移动,但最终还是自由落下

任何更好的碰撞方法或建议

我什至为每个按钮都试过这个,但仍然没有运气

if (osl_keys->held.down)
{
  if ( (sprite->x + spritewidth > bush->x) &&
       (sprite->x < bush->x + bushwidth) && 
       (sprite->y + spriteheight > bush->y) &&
       (sprite->y < bush->y + bushheight) ) 
  {
    sprite->y -= 4; 
  }
  else
  {
    sprite->y += 2;
    sprite_position = DOWN;
    SpriteAnimate();
  }
}
4

1 回答 1

1

您可以做的一件事是,当角色撞到灌木丛时,您可以改变他的位置,而不是让角色“向后移动”。

我的意思是这样的:(仅用于示例)。

 if (osl_keys->held.up)    
 {
   if (bushcol == 0)
   {
     sprite->y -= 4;
     sprite_position = UP;
     SpriteAnimate();
   }
   else
   { 
     sprite->y = bush->y + 2;
     bushcol = 0;
   }
 }

这样,每当精灵碰撞时,它只是设置位置而不是让它向后移动。

还有其他方法可以进行碰撞检测,但我现在太累了,无法立即做出一个智能的、可读性差得多的答案……在谷歌上搜索会发现很多结果。

于 2009-05-13T23:32:50.543 回答