简体   繁体   English

C ++中的平铺运动

[英]Tiled movement in C++

here's what I'm working with now 这就是我现在正在处理的

if (osl_keys->held.down)
{
sprite_position = DOWN;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->y += 16;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->y += 16;
}
if (osl_keys->held.up)
{
sprite_position = UP;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->y -= 16;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->y -= 16;
}
if (osl_keys->held.right)
{
sprite_position = RIGHT;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->x += 16;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->x += 16;
}
if (osl_keys->held.left)
{
sprite_position = LEFT;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->x -= 16;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->x -= 16;
}
sceKernelDelayThread(750000);

and I have difinantly decided to have each sprite be 32x32 so collision may be more aplicable 而且我已经决定将每个子画面设置为32x32,因此碰撞可能更适用

by lokiare1 you still have to check for collision with the tiles. 通过lokiare1,您仍然必须检查与瓷砖的碰撞。 The most thorough way to do collision is to check to see the location of each visible pixel of a sprite against every other pixel of every other sprite, then return a pointer to the sprite that is collided with. 进行碰撞的最彻底的方法是检查小精灵的每个可见像素相对于其他小精灵的每个其他像素的位置,然后将指针返回到与之碰撞的小精灵。 This would be very slow. 这将非常慢。 The method I describe should be a slow and dirty working collision detection. 我描述的方法应该是缓慢而肮脏的工作冲突检测。 If I'm wrong please tell me how I'm wrong. 如果我错了,请告诉我我错了。

This is the part i am having trouble imagining(I like to make sure things work in my head before I try it out that way I dont hop to something random) I have spent a few amount of nights sitting in the shower trying to think of a way or it to check if something was there 这是我难以想象的部分(我想确保事情在脑海中起作用,然后再尝试那种方式,我不会跳到随机的东西)我花了几个晚上坐在淋浴间,想着一种检查是否有东西的方法

Im thinking of something that presets that tile x,y is solid so if a solid object is infront of it dont move else move Im working in my post >.> 我想到的是预设为平铺x,y的物体是固体的,因此如果固体物体在其前面,则不要移动,否则移动Im在我的帖子中工作>。

if (osl_keys->held.down)
{
if (y+1 == bush=>y)
{
sprite_march = 4;
SpriteAnimate();
else
{
sprite_position = DOWN;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->y += 16;
SpriteAnimate();
sceKernelDelayThread(20000);
sprite->y += 16;
}  

But then again how would i be able to shorten the amount of code by just asking (y+1 == solid) im not sure how to go about that 但是再说一次,我怎么能通过问(y + 1 == solid)我不知道如何解决这个问题而缩短代码量

This isn't an answer but a general style note. 这不是答案,而是一般的样式说明。 Do you notice that your code is repetitive? 您是否注意到您的代码是重复的? Because it is. 因为它是。 Your first post of code can be shortened a lot: 您的第一篇代码可以大大缩短:

int down_direction = 0, right_direction = 0;
if (osl_keys->held.down)
{
    down_direction = 16;
    sprite_position = DOWN;
}
else if (osl_keys->held.up)
{
    down_direction = -16;
    sprite_position = UP;
}
else if (osl_keys->held.right)
{
    right_direction = 16;
    sprite_position = RIGHT;
}
else if (osl_keys->held.left)
{
    right_direction = -16;
    sprite_position = LEFT;
}

for (int i = 0; i < 2; ++i)
{
    SpriteAnimate();
    sceKernelDelayThread(20000);
    sprite->x += right_direction;
    sprite->y += down_direction;
}
sceKernelDelayThread(750000);

There, isn't that better? 在那里,不是更好吗?

To answer your question, you don't want to track the pixel position of your sprite. 要回答您的问题,您不想跟踪精灵的像素位置。 You want to track the row and column position. 您要跟踪行和列的位置。 Then just check to the side before you move, if the target tile is solid. 然后,如果目标图块是实心的,则在移动之前检查一下侧面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM