c++ - 概述 rpg 平铺空间

标签 c++ tiles collision

我正在尝试将其设置为角色在图 block 中的位置,当他们向上或向下移动时它会移动到下一个图 block ,但我不确定该怎么做。现在,我设置了角色移动像素的位置,但我希望它移动 1 个方格。

现在的代码是这样的,它可以工作,但在像素模式下会出现故障。我相信如果它是按 block 进行的,它可能会更好,但我还是会更改它。

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

最佳答案

如果您希望角色一次移动一个方 block /方 block /方 block ,只需将 Sprite 移动方 block 宽度(或高度)的像素数即可。

const int tile_width = 32; // or something

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

关于c++ - 概述 rpg 平铺空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/870225/

相关文章:

c++ - Getter 函数的 Const 正确性

java - Spring Boot Tiles - java.lang.NoClassDefFoundError : org/apache/commons/beanutils/MethodUtils

java - 其中哪一个是使用 tiles 的更好方法

c# - SetActive 在 Unity3d 中不起作用?

java - Java String 哈希码溢出的后果

C++ 结构定义

c++ - std::uint8_t 的底层类型

c++ - Collision c++ 2d 游戏,防止玩家进入瓷砖

c++ - 为什么mapped_file::data 返回 char* 而不是 void*

spring-mvc - Tiles 3.0 和 Spring MVC 设置