横屏小游戏--萝莉快跑源码分析二

游戏地图:

 

init()函数初始化游戏地图

    map = new GameObjMap();
    map->setAnchorPoint(ccp(0,1));
    map->setPosition(ccp(0,size.height));
    addChild(map,0);

进入GameObjMap类ccp文件,地图初始化过程
雾和山水图组成一张游戏背景,创建2张

    CCSprite *bg1 = CCSprite::create("back_1.png");//雾
    bg1->setScale(0.5);
    bg1->setAnchorPoint(ccp(0,1));
    bg1->setPosition(ccp(0, size.height) );
    this->addChild(bg1,0,0);

    CCSprite *bgdi1 = CCSprite::create("back_5.png"); //山水
    bgdi1->setAnchorPoint(ccp(0,0));
    bgdi1->setPosition(ccp(0,-124) );
    bg1->addChild(bgdi1,1);

    CCSprite *bg2 = CCSprite::create("back_1.png");
    bg2->setScale(0.5);
    bg2->setAnchorPoint(ccp(0,1));
    bg2->setPosition(ccp(size.width, size.height) );
    this->addChild(bg2,0,1);

    CCSprite *bgdi2 = CCSprite::create("back_5.png");
    bgdi2->setAnchorPoint(ccp(0,0));
    bgdi2->setPosition(ccp(0,-124) );
    bg2->addChild(bgdi2,1);


两张图滚动效果

    bg1->runAction(CCSequence::create(CCMoveBy::create(10,ccp(-480,0)),CCCallFuncN::create(this, callfuncN_selector(GameObjMap::bg1change)),NULL));//第1张先往左移动
    bg2->runAction(CCSequence::create(CCMoveBy::create(20,ccp(-960,0)),CCCallFuncN::create(this, callfuncN_selector(GameObjMap::bg2change)),NULL));//第2张跟上,这样避免出现黑框
 //移出屏幕重设位置,继续左移,无限循环
void GameObjMap::bg1change(CCNode* pSender){

    CCSprite *bg = (CCSprite *)this->getChildByTag(0);
    bg->setPosition(ccp(480,320));
    bg->runAction(CCSequence::create(CCMoveBy::create(20,ccp(-960,0)),CCCallFuncN::create(this, callfuncN_selector(GameObjMap::bg1change)),NULL));
    for(int i = 0;i < 5;i ++){//显示星星
        ((GameObjStar *)stars1->objectAtIndex(i))->set_visable(true);
    }
}
//移出屏幕重设位置,继续左移,无限循环
void GameObjMap::bg2change(CCNode* pSender){
    CCSprite *bg = (CCSprite *)this->getChildByTag(1);
    bg->setPosition(ccp(480,320));
    bg->runAction(CCSequence::create(CCMoveBy::create(20,ccp(-960,0)),CCCallFuncN::create(this, callfuncN_selector(GameObjMap::bg2change)),NULL));
    for(int i = 0;i < 5;i ++){
        ((GameObjStar *)stars2->objectAtIndex(i))->set_visable(true);
    }
}

在地图上添加星星

	stars1 = CCArray::createWithCapacity(5);
    stars2 = CCArray::createWithCapacity(5);
    for(int i = 0;i < 5;i ++){
        GameObjStar *obj = new GameObjStar();
        obj->setPosition(ccp(172 + 192 * i,350));
        stars1->addObject(obj);
        bg1->addChild(obj,3);
        obj = new GameObjStar();
        obj->setPosition(ccp(172 + 192 * i,350));
        stars2->addObject(obj);
        bg2->addChild(obj,3);
    }
    stars1->retain();
    stars2->retain();

 

在地图上添加林木、植物、道路,至此地图初始化完毕

for(int i = 0;i < 7;i ++){
        CCSprite *roaddi;
        CCSprite *plant;
        if(bg1shu[i] != -1){
           CCSprite *road;
           switch(bg1shu[i]){
            case 0:
                plant = CCSprite::create("back_2.png");
                plant->setAnchorPoint(ccp(0.5,0));
                plant->setPosition( ccp(128 * i + 64,117) );
                bg1->addChild(plant,1);
                road = CCSprite::create("road_2.png");
                road->setAnchorPoint(ccp(0,0));
                road->setPosition( ccp(128 * i,0) );
                roaddi = CCSprite::create("road_3.png");
                roaddi->setAnchorPoint(ccp(0,1));
                roaddi->setPosition( ccp(128 * i,0) );
                bg1->addChild(roaddi,1);
                break;
            case 1:
                road = CCSprite::create("road_1.png");
                road->setAnchorPoint(ccp(0,0));
                road->setPosition( ccp(26 + 128 * i,0) );
                roaddi = CCSprite::create("road_4.png");
                
                roaddi->setAnchorPoint(ccp(0,1));
                roaddi->setPosition( ccp(26 + 128 * i,0) );
                bg1->addChild(roaddi,1);
                break;
            case 2:
                plant = CCSprite::create("enemy.png");
                plant->setAnchorPoint(ccp(0.5,0));
                plant->setPosition( ccp(128 * i + 64,112) );
                bg1->addChild(plant,1);
                road = CCSprite::create("road_1.png");
                road->setFlipX(true);
                road->setAnchorPoint(ccp(0,0));
                road->setPosition( ccp(128 * i,0) );
                roaddi = CCSprite::create("road_4.png");
                roaddi->setFlipX(true);
                roaddi->setAnchorPoint(ccp(0,1));
                roaddi->setPosition( ccp(128 * i,0) );
                bg1->addChild(roaddi,1);
                break;
            case 3:
                road = CCSprite::create("road_5.png");
                road->setAnchorPoint(ccp(0,0));
                road->setPosition( ccp(128 * i,0) );
                break;
            
           }
           bg1->addChild(road,1);
        }
		
        if(bg2shu[i] != -1){
           CCSprite *road1;
           switch(bg2shu[i]){
            case 0:
                road1 = CCSprite::create("road_2.png");
                road1->setAnchorPoint(ccp(0,0));
                road1->setPosition( ccp(128 * i,0) );
                roaddi = CCSprite::create("road_3.png");
                
                roaddi->setAnchorPoint(ccp(0,1));
                roaddi->setPosition( ccp(128 * i,0) );
                bg2->addChild(roaddi,1);
                break;
            case 1:
                plant = CCSprite::create("back_3.png");
                plant->setAnchorPoint(ccp(0.5,0));
                plant->setPosition( ccp(128 * i + 128,117) );
                bg2->addChild(plant,1);
                road1 = CCSprite::create("road_1.png");
                road1->setAnchorPoint(ccp(0,0));
                road1->setPosition( ccp(26 + 128 * i,0) );
                roaddi = CCSprite::create("road_4.png");
                
                roaddi->setAnchorPoint(ccp(0,1));
                roaddi->setPosition( ccp(26 + 128 * i,0) );
                bg2->addChild(roaddi,1);
                break;
            case 2:
                road1 = CCSprite::create("road_1.png");
                road1->setFlipX(true);
                road1->setAnchorPoint(ccp(0,0));
                road1->setPosition( ccp(128 * i,0) );
                roaddi = CCSprite::create("road_4.png");
                roaddi->setFlipX(true);
                roaddi->setAnchorPoint(ccp(0,1));
                roaddi->setPosition( ccp(128 * i,0) );
                bg2->addChild(roaddi,1);
                break;
            case 3:
                road1 = CCSprite::create("road_5.png");
                road1->setAnchorPoint(ccp(0,0));
                road1->setPosition( ccp(128 * i,0) );
                break;
                
            }
            bg2->addChild(road1,1);
        }
    }
    state = 0;
}

 

你可能感兴趣的:(cocos2d,cocos2dx)