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

主角出场:

初始化主角

    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;
};

?

原文地址:https://www.cnblogs.com/llguanli/p/8453967.html

时间: 2024-08-01 21:07:11

横屏小游戏--萝莉快跑源代码分析三的相关文章

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

主角出场: 初始化主角 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 = CCDire

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

Cpp文件功能介绍 GameAboutScene.cpp 关于页面 GameMainScene.cpp游戏主页面 GameMark.cpp分数 GameMenuScene.cpp游戏主菜单 GameObjHero.cpp主角 GameObjMap.cpp游戏地图 GameObjStar.cpp星星 菜单主页面: 主菜单页面背景 CCSprite* bg = CCSprite::create("MainMenu.png"); bg->setScale(0.5); bg->se

竖屏小游戏--喵星战争源代码分析【完整】

 转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/26942311 源代码地址:http://download.csdn.net/detail/oyangyufu/7409593 Ccp文件介绍: GameMenuScene.cpp游戏主菜单 GameMainScene.cpp游戏主页面 GameObjHero.cpp主角 GameHeroBullet.cpp主角的子弹 GameObjEnemy.cpp敌人 GameEnemyBull

HTML5小游戏-绵羊快跑

用HTML5仿一款灵敏测试的flash小游戏程序 下载地址:http://www.huiyi8.com/divcss/<?php/**Author: Jamin* */if(substr_count($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){    ob_start('ob_gzhandler');}?><!DOCTYPE html><html><head><title>绵羊快跑</title>

静态分析第三发 so文件分析(小黄人快跑)

本文作者:i春秋作家--HAI_ 0×00 工具 1.IDA pro 2.Android Killer 0×01 环境 小黄人快跑 下载地址http://download.csdn.net/download/qq_36869808/10179100 0×02 支付分析 和其他的支付不一样,没有使用原生的Toast,所以我们要进行进一步分析. 0×03 祭神器 之前隐藏了一个技能,当然不是我的哈,是神器的技能.这个技能厉害到不需要动态调试就可以查看LOG.也是感谢程序员的辛勤奉献,不知道是不是调试

cocos2dx实战篇——Demo《萝莉快跑》学习心得

[唠叨] 源码教程请移步:http://blog.csdn.net/iamlazybone/article/details/19612941 感谢懒骨头提供了这么多的Demo教程,对于初学者的我而言,帮助真的十分大. 注:本节仅仅记录博主自身学习<萝莉快跑>的心得体会. [游戏截图] [学习心得] 1.游戏主场景分层 如果将所有的元素全部都写在一个Game类里面,会很混乱,且改动也很麻烦. 所以应该对游戏的元素进行分层,如:背景层.人物层.怪物层.道具层等. 然后再将所有的层放入Game场景中

Nouveau源代码分析(三):NVIDIA设备初始化之nouveau_drm_probe

Nouveau源代码分析(三) 向DRM注冊了Nouveau驱动之后,内核中的PCI模块就会扫描全部没有相应驱动的设备,然后和nouveau_drm_pci_table对比. 对于匹配的设备,PCI模块就调用相应的probe函数,也就是nouveau_drm_probe. // /drivers/gpu/drm/nouveau/nouveau_drm.c 281 static int nouveau_drm_probe(struct pci_dev *pdev, 282 const struct

Android 中View的绘制机制源代码分析 三

到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编辑器.这里之所以使用"下定决心"这个词.是由于毕竟Html编辑器使用好几年了.非常多习惯都已经养成了,要改变多年的习惯确实不易.相信这也是还有非常多人坚持使用Html编辑器的原因. 这也反应了一个现象.当人对某一事物非常熟悉时,一旦出现了新的事物想代替老的事物时,人们都有一种抵触的情绪,做

【原创】Kakfa utils源代码分析(三)

Kafka utils包最后一篇~~~ 十五.ShutdownableThread.scala 可关闭的线程抽象类! 继承自Thread同时还接收一个boolean变量isInterruptible表明是否允许中断.既然是可关闭的,因此一定不是守护线程,而是一个用户线程(不会阻塞JVM关闭).提供的方法有: 1. doWork: 抽象方法.子类必须实现这个方法,从名字来说应该是指定线程要完成的操作. 2. initiateShutdown: 发起关闭请求.首先通过CAS的方式判断是否线程在运行中