开发者

Cocos2d Scrolling game TileMap Collision

开发者 https://www.devze.com 2023-02-13 23:21 出处:网络
I have different tile layers that I move at different speeds, my player is fixed in place(horizontally). Now how do I detect that my player is colliding width开发者_如何学运维 a tile? How do I know th

I have different tile layers that I move at different speeds, my player is fixed in place(horizontally). Now how do I detect that my player is colliding width开发者_如何学运维 a tile? How do I know the coordinates of the tiles in the different layers?


I figured this out with the help of this tutorial http://paulsonapps.wordpress.com/2010/03/12/tutorial-1-tilemap-with-collision-game-cocos2d/

-(void)handleCollision
{
    for (CCTMXLayer *lr in layers) {
        // Determine the four corners of my player
        int tlXright = floor(player.playerSprite.position.x+player.playerSprite.contentSize.width-lr.position.x);
        int tlXleft = floor(player.playerSprite.position.x-lr.position.x);
        int tlYup = floor(player.playerSprite.position.y+player.playerSprite.contentSize.height);
        int tlYdown = floor(player.playerSprite.position.y);


        //Convert our Map points 
        CGPoint nodeSpace1 = [tileMap convertToNodeSpace:ccp(tlXright,tlYdown)];
        pos1X = floor(nodeSpace1.x / tileMap.tileSize.width);
        pos1Y = floor(tileMap.mapSize.height - (nodeSpace1.y / tileMap.tileSize.height));

        CGPoint nodeSpace2 = [tileMap convertToNodeSpace:ccp(tlXright,tlYup)];
        pos2X = floor(nodeSpace2.x / tileMap.tileSize.width);
        pos2Y = floor(tileMap.mapSize.height - (nodeSpace2.y / tileMap.tileSize.height));

        CGPoint nodeSpace3 = [tileMap convertToNodeSpace:ccp(tlXleft,tlYdown)];
        pos3X = floor(nodeSpace3.x / tileMap.tileSize.width);
        pos3Y = floor(tileMap.mapSize.height - (nodeSpace3.y / tileMap.tileSize.height));

        CGPoint nodeSpace4 = [tileMap convertToNodeSpace:ccp(tlXleft,tlYup)];
        pos4X = floor(nodeSpace4.x / tileMap.tileSize.width);
        pos4Y = floor(tileMap.mapSize.height - (nodeSpace4.y / tileMap.tileSize.height));


        unsigned int gid5 = [lr tileGIDAt:ccp(pos1X,pos1Y)];
        unsigned int gid6 = [lr tileGIDAt:ccp(pos2X,pos2Y)];
        unsigned int gid7 = [lr tileGIDAt:ccp(pos3X,pos3Y)];
        unsigned int gid8 = [lr tileGIDAt:ccp(pos4X,pos4Y)];


        //NSLog(@"gid5:%d gid6:%d gid7:%d gid8:%d",gid5,gid6,gid7,gid8);
        / NSLog(@"pos1x:%d pos1y:%d pos4x:%d pos4y:%d",pos1X ,pos1Y,pos4X,pos4Y);
        if (gid5 == 5) {
            [lr removeTileAt:ccp(pos1X,pos1Y)];
        }


        if (gid6 == 5) {
            [lr removeTileAt:ccp(pos2X,pos2Y)];

        }

        if (gid7 == 5) {
            [lr removeTileAt:ccp(pos3X,pos3Y)];
        }

        if (gid8 == 5) {
            [lr removeTileAt:ccp(pos4X,pos4Y)];
        }
    }
}   

to know the coordinate of the tile, i subtracted the layer position to the position of the player

 
int tlXright = floor(player.playerSprite.position.x+player.playerSprite.contentSize.width-lr.position.x);
 

and converted it like in the tutorial.

CGPoint nodeSpace1 = [tileMap convertToNodeSpace:ccp(tlXright,tlYdown)];
    pos1X = floor(nodeSpace1.x / tileMap.tileSize.width);
    pos1Y = floor(tileMap.mapSize.height - (nodeSpace1.y / tileMap.tileSize.height));

...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号