首页 > 代码库 > 横屏小游戏--萝莉快跑源码分析三

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

主角出场:

初始化主角

    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{//背景图2
		temp = (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;
};