• cocos2dx tiledmap 碰撞检测


    bool HelloWorld::init()
    {
        //////////////////////////////
        // 1. super init first
        if ( !CCLayer::init() )
        {
            return false;
        }
        
        CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
        CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
        map=CCTMXTiledMap::create("1.tmx");
        addChild(map);
    
        CCTMXObjectGroup* objgroup=map->objectGroupNamed("objLayer");
        CCDictionary* dic=objgroup->objectNamed("hero");
        float x=dic->valueForKey("x")->floatValue();
        float y=dic->valueForKey("y")->floatValue();
        float id=dic->valueForKey("id")->floatValue();
        const char* idstr=dic->valueForKey("id")->getCString();
        CCLOG("%f %f %f",x,y,id);
        CCLOG(idstr);
    
        hero=CCSprite::create("hero.png");
        hero->setPosition(ccp(x,y));
        addChild(hero);
        
        CCMoveBy *move=CCMoveBy::create(2,ccp(winSize.width/2,-winSize.height/2));
        hero->runAction(move);
        schedule(schedule_selector(HelloWorld::judeCollion));
        return true;
    }
    
    void HelloWorld::judeCollion(float dt)
    {
        if(hero!=NULL)
        {
            CCTMXLayer* collionLayer=map->layerNamed("collionLayer");
            CCPoint tilePos=positionToTid(hero->getPosition());
            if(collionLayer->tileGIDAt(tilePos))
            {
                CCLOG("collion");
                collionLayer->removeTileAt(tilePos);
            }
        }
        
    }
    CCPoint HelloWorld::positionToTid(CCPoint position)
    {
        int x = position.x /map->getTileSize().width;
        int y = (map->getMapSize().height*map->getTileSize().height 
            - position.y) /map->getTileSize().height;
        return ccp(x, y);
    }
  • 相关阅读:
    推荐大家使用的CSS书写规范、顺序
    只能输入数字的文本框
    js和jQuery 获取屏幕高度、宽度
    jquery插件开发规范
    ie下使用firebug
    equals和==的使用
    引用数据类型的赋值
    数组工具Arrays的基本使用
    冒泡排序
    使用数组对杨辉三角练习
  • 原文地址:https://www.cnblogs.com/yufenghou/p/4009524.html
Copyright © 2020-2023  润新知