简体   繁体   English

基于瓦片的 map 和碰撞; 卡住

[英]Tile based map and collision; getting stuck

I'm trying to make my character move around a tile map with collisions.我试图让我的角色在 map 上移动并发生碰撞。 Everything works fine except for one thing.除了一件事,一切都很好。 I show you a picture with the problem:我给你看一张有问题的图片:

http://i.imgur.com/bcyz5.jpg That is, when I reach a tile above then I can not move anywhere.也就是说,当我到达上方的瓷砖时,我无法移动任何地方。 If you come from the left, I can not move either up or down.如果你从左边来,我不能上下移动。 If you reach the bottom, I can move to the left but not right.如果你到达底部,我可以向左移动,但不能向右移动。 And when you reach the right I can move in any direction.当你到达右边时,我可以向任何方向移动。

Honestly I have no idea what may be failing.老实说,我不知道什么可能会失败。 I think it has to do with if (...), because if I change the order, the addresses where I can move change:/我认为这与 if (...) 有关,因为如果我更改顺序,我可以移动的地址会更改:/

Here I leave some code:在这里我留下一些代码:

boolean collision = false;

if(Keyboard.isKeyDown(Keyboard.KEY_UP)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(UpTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(0.0f, -vel);
}
if(Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(LeftTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(-vel, 0.0f);
}
if(Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(DownTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(0.0f, vel);
}
if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
    for(int i = 0; i < map.GetNumLayers(); i++) {
        if(RightTile(map, i) > 128) {
            collision = true;
        }
    }

    if(!collision) AddPos(vel, 0.0f);
}

This will be easier of you separate your game's model from the view shown above.如果您将游戏的 model 从上面显示的视图中分离出来,这将更容易。 This example shows one approach, while this more elaborate example models a related grid based game.这个例子展示了一种方法,而这个更精细的例子模拟了一个相关的基于网格的游戏。

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

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