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

来源:互联网 发布:matlab2015b mac 迅雷 编辑:程序博客网 时间:2024/04/27 20:25

主角出场:

初始化主角

    hero = new GameObjHero();    hero->setScale(0.5);    hero->setPosition(ccp(100,160));hero->setVisible(false);    addChild(hero,1);


进入GameObjHero类ccp文件
创建主角及动作

this->setContentSize(CCSizeMake(85,90));//接收触摸事件    CCDirector* pDirector = CCDirector::sharedDirector();    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);    CCSprite * obj = CCSprite::create("s_hurt.png");//受伤    hurt = obj->getTexture();    obj = CCSprite::create("s_jump.png"); //跳跃    jump = obj->getTexture();    mainsprite = CCSprite::create("s_1.png"); //第1个动作    //主角奔跑的动画帧    CCAnimation * animation = CCAnimation::create();    animation->addSpriteFrameWithFileName("s_1.png");    animation->addSpriteFrameWithFileName("s_2.png");    animation->addSpriteFrameWithFileName("s_3.png");    animation->addSpriteFrameWithFileName("s_4.png");    animation->addSpriteFrameWithFileName("s_5.png");    animation->addSpriteFrameWithFileName("s_6.png");    animation->setDelayPerUnit(0.1f);    animation->setRestoreOriginalFrame(true);    //运行动画,无限循环    mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));    state = 0;    addChild(mainsprite);


本层触摸事件处理,触摸开始函数设置主角跳跃

bool GameObjHero::ccTouchBegan(CCTouch* touch, CCEvent* event){    if(((GameMain *)this->getParent())->isover)//游戏结束则不响应        return false;//setTheState(1);    //设置运动状态    this->setTheState(1);    return true;   }


setTheState函数用来设置主角几种运动状态,1表示跳跃、2表示受伤、0表示奔跑

void GameObjHero::setTheState(short var){CCLOG("State = %d",state);if(state == var)return;state = var;switch(state){case 1://跳跃//先停止奔跑this->stopAllActions();mainsprite->stopAllActions();//跳跃空中的姿势mainsprite->setTexture(jump);//运行跳跃动作,完成后继续奔跑this->runAction(CCSequence::create(CCJumpBy::create(2.5,ccp(0,0),100,1),CCCallFuncN::create(this, callfuncN_selector(GameObjHero::jumpend)),NULL));//跳跃完继续奔跑break;case 2://受伤this->stopAllActions();mainsprite->stopAllActions();mainsprite->setTexture(hurt);//受伤时姿势this->runAction(CCSequence::create(CCBlink::create(3, 10),CCCallFuncN::create(this, callfuncN_selector(GameObjHero::hurtend)),NULL));//受伤了闪烁,然后继续奔跑((GameMain *)this->getParent())->setover();//受伤了游戏结束break;case 0://奔跑动画this->stopAllActions();mainsprite->stopAllActions();CCAnimation * animation = CCAnimation::create();animation->addSpriteFrameWithFileName("s_1.png");animation->addSpriteFrameWithFileName("s_2.png");animation->addSpriteFrameWithFileName("s_3.png");animation->addSpriteFrameWithFileName("s_4.png");animation->addSpriteFrameWithFileName("s_5.png");animation->addSpriteFrameWithFileName("s_6.png");animation->setDelayPerUnit(0.1f);animation->setRestoreOriginalFrame(true);mainsprite->runAction(CCRepeatForever::create(CCAnimate::create(animation)));break;}}


游戏分数

    gamemark = new GameMark();    addChild(gamemark,4);


游戏结束语

    gameover = CCSprite::create("gameover.png");    gameover->setAnchorPoint(ccp(0.5,0.5));    gameover->setPosition(ccp(0,0));    gameover->setPosition(ccp(size.width/2,size.height/2 + 70));    gameover->setVisible(false);    gameover->setScale(0.5);    addChild(gameover,5);

游戏结束上一步菜单

CCMenuItemImage *pCloseItem = CCMenuItemImage::create("back.png","back.png", this, menu_selector(GameMain::menuBackCallback) );    pCloseItem->setPosition( ccp(size.width/2,size.height/2 - 50) );    pCloseItem->setScale(0.5);    CCMenu *pMenu = CCMenu::create(pCloseItem, NULL);    pMenu->setPosition( CCPointZero );    this->addChild(pMenu,5,25);    pMenu->setVisible(false);    pMenu->setEnabled(false);


游戏主页面中update函数用来检测主角是否掉下悬崖、主角是否吃了星星

//奔跑时检测主角是否掉下    if(hero->state == 0)       isherodrop();//检测   void GameMain::isherodrop(){// 获取背景图的的坐标(前面创建背景连接的2张图)CCPoint p1 = (map->getChildByTag(0))->getPosition();CCPoint p2 = (map->getChildByTag(1))->getPosition(); int temp;if(p1.x <= 100 && (p1.x + 480) >= 100) //主角在第1张背景图,主角x坐标100{temp = (100 - p1.x) / 64;//获取图块编号if(bg1shu[temp] == -1){//没有在图块上,则是掉下悬崖了CCLog("this one");hero->setTheState(2);//受伤}}else{//背景图2temp = (100 - p2.x) / 64;if(bg2shu[temp] == -1){CCLog("this two");hero->setTheState(2);} }}


主角吃星星

CCPoint p1 = (map->getChildByTag(0))->getPosition();    CCPoint p2 = (map->getChildByTag(1))->getPosition();for (int i = 0; i < 5; i++){if (p1.x <= 100 && (p1.x + 480) > 100){GameObjStar *obj = (GameObjStar *)(map->stars1)->objectAtIndex(i); //获取单个星星if(obj->get_visable() && isCollion(ccp(100,hero->getPosition().y + 62.5),ccp(p1.x + 86 + 96 * i,280),40,35,18.25,17.75))//星星可见且碰撞了{obj->set_visable(false);//星星消失gamemark->addnumber(200);//加分}} else{GameObjStar *obj = (GameObjStar *)(map->stars2)->objectAtIndex(i);if(obj->get_visable() && isCollion(ccp(100,hero->getPosition().y + 62.5),ccp(p2.x + 86 + 96 * i,280),40,35,18.25,17.75)){obj->set_visable(false);gamemark->addnumber(200);}}}

 

主角与星星碰撞bool GameMain::isCollion(CCPoint p1,CCPoint p2,int w1,int h1,int w2,int h2){//CCLOG("p1p2x(%f,%f) , p1.x-p2.x=%f  w1+w2=%d",p1.x,p2.x,abs(p1.x-p2.x),(w1 + w2));    if(abs(p1.x - p2.x) < w1 + w2 && abs(p1.y - p2.y) < h1 + h2){        return true;    }    return false;};



 

0 0