cocos2d 小游戏
今天写了一个小游戏,发现看过的代码自己来写还是会经常出错,还是要多自己动手写写哈。
先上几张游戏界面图
void HelloWorld::addTarget()
{
//首先初始化精灵
CCSprite *pTarget = CCSprite::create("Target.png");
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
//计算可绘制的范围
int minY = pTarget->getContentSize().height/;
int maxY = winSize.height - minY;
//计算可随机基数
int rangeY = maxY - minY;
//随机出的数*随机基数+半个身位 = 最后的坐标点
int actualY = (rand() % rangeY) + minY;
pTarget->setPosition(ccp(winSize.width + pTarget->getContentSize().width/ ,actualY));
this->addChild(pTarget);
pTarget->setTag();
_targets->addObject(pTarget); //计算移动速度 最慢4秒移动横屏 最快2秒
int minDuration = (int) 2.0;
int maxDuration = (int) 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (rand() % rangeDuration) +minDuration; //初始化耗时动作
CCFiniteTimeAction* actionMove =
CCMoveTo::create((float)actualDuration,
ccp( - pTarget->getContentSize().width/,actualY));
CCFiniteTimeAction* actionMoveDone =
CCCallFuncN::create(this,
callfuncN_selector(HelloWorld::spriteMoveFinished)); //绑定动作
pTarget->runAction(CCSequence::create(actionMove,actionMoveDone,NULL));
}
这是怪物随机从右边向左边移动的代码,到左边后回调方法spriteMoveFinished。
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
CCTouch *touch = (CCTouch *)touches->anyObject();
CCPoint location = touch->getLocationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCSprite *pProjectile = CCSprite::create("Projectile.png");
pProjectile->setPosition(ccp(pHero->getContentSize().width/,size.height/)); int offX = location.x - pProjectile->getPosition().x;
int offY = location.y - pProjectile->getPosition().y; if(offX <= ) return;
//添加发子弹声音
CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(
"pew-pew-lei.wav"); this->addChild(pProjectile);
pProjectile->setTag();
_projectiles->addObject(pProjectile); int realX = size.width +
(pProjectile->getContentSize().width/);
float ratio = (float)offY / (float)offX;
int realY = (realX * ratio) + pProjectile->getPosition().y;
CCPoint realDest = ccp(realX,realY); int offRealX = realX - pProjectile->getPosition().x;
int offRealY = realY - pProjectile->getPosition().y;
float length = sqrtf((offRealX * offRealX) + (offRealY*offRealY)); float velocity = /;
float realMoveDuration = length/velocity; pProjectile->runAction(CCSequence::create(
CCMoveTo::create(realMoveDuration,realDest)
,CCCallFuncN::create(this,callfuncN_selector(HelloWorld::spriteMoveFinished))
,NULL));
}
这是触摸后发射子弹,子弹发射后到右边也是回调方法spriteMoveFinished。
void HelloWorld::spriteMoveFinished(CCNode* sender)
{
CCSprite *pSprite = (CCSprite *)sender;
this->removeChild(pSprite,true); //当怪物走屏幕左边,显示失败界面
if (pSprite->getTag() == )
{
_targets->removeObject(pSprite);
GameOverScene *gameOverScene =(GameOverScene*) GameOverScene::create();
if(gameOverScene){
gameOverScene->getLabel()->setString("You Lose!");
CCScene *pScene = CCScene::create();
pScene->addChild(gameOverScene);
CCDirector::sharedDirector()->replaceScene(pScene);
}
}else if (pSprite->getTag() == )
{
_projectiles->removeObject(pSprite);
}
}
这是怪物和子弹的回调方法,怪物走到左边,remove怪物、从数组中删除并显示失败界面;子弹飞到右边,也是remove掉子弹并从数组中删除。
void HelloWorld::update(float dt)
{
CCLOG("%f",dt);
CCArray *projectilesToDelete =
CCArray::create();
projectilesToDelete->retain();
//碰撞算法 //循环遍历 怪物的数字内的所有对象
for(int i = ;i < _projectiles->count();i++)
{
//取出第i个对象 转化为精灵对象指针
CCSprite *projectile = (CCSprite *)_projectiles->objectAtIndex(i);
//获取这个对象的区域
CCRect projectileRect = CCRectMake(projectile->getPosition().x -(projectile->getContentSize().width/),
projectile->getPosition().y - (projectile->getContentSize().height/),
projectile->getContentSize().width,
projectile->getContentSize().height
);
//创建存放子弹的数组
CCArray *targetsToDelete = CCArray::create();
targetsToDelete->retain();
//循环遍历 怪物数组对象
for(int j = ;j < _targets->count();j++)
{
//取出第j个对象的指针
CCSprite *target = (CCSprite *)_targets->objectAtIndex(j);
CCRect targetRect = CCRectMake(target->getPosition().x -(target->getContentSize().width/),
target->getPosition().y - (target->getContentSize().height/),
target->getContentSize().width,
target->getContentSize().height);
//如果两个范围有交集
if(projectileRect.intersectsRect(targetRect))
{
targetsToDelete->addObject(target);
} }
for(int k = ;k < targetsToDelete->count();k++)
{
CCSprite *target =(CCSprite *)targetsToDelete->objectAtIndex(k);
_targets->removeObject(target);
this->removeChild(target,true);
_projectilesDestroyed++;
//打中超过4个怪物,跳转到获胜界面
if (_projectilesDestroyed > )
{
GameOverScene *gameOverScene =(GameOverScene*) GameOverScene::create();
if(gameOverScene){
gameOverScene->getLabel()->setString("You Win!");
CCScene *pScene = CCScene::create();
pScene->addChild(gameOverScene);
CCDirector::sharedDirector()->replaceScene(pScene);
}
} }
if(targetsToDelete->count() > )
{
projectilesToDelete->addObject(projectile);
}
//循环遍历 移除对象
for ( int i=;i< projectilesToDelete->count();i++)
{
CCSprite* projectile =(CCSprite *)projectilesToDelete->objectAtIndex(i);
_projectiles->removeObject(projectile);
this->removeChild(projectile, true);
}
}
}
上面这个是检测子弹跟怪物是否碰撞,有碰撞分别把它们都remove掉。
源码及资源下载链接:http://pan.baidu.com/s/1kTHr5DP
cocos2d 小游戏的更多相关文章
- Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)
Cocos2d—X游戏开发之CCToggle(菜单标签切换) 首先继承子CCMenu,是菜单标签中的一种.‘ class CC_DLL CCMenuItemToggle : public CCMenu ...
- [SpriteKit] 系统框架中Cocos2d-x制作小游戏ZombieConga
概述 使用SpriteKit实现一个简单的游戏, 通过一个游戏来进行SpriteKit的入门, 熟练2D游戏的API, 也可以更好的结合在iOS应用中. 详细 代码下载:http://www.demo ...
- iOS cocos2d 2游戏开发实战(第3版)书评
2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...
- jQuery实践-网页版2048小游戏
▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...
- 拼图小游戏之计算后样式与CSS动画的冲突
先说结论: 前几天写了几个非常简单的移动端小游戏,其中一个拼图游戏让我郁闷了一段时间.因为要获取每张图片的位置,用`<style>`标签写的样式,直接获取计算后样式再用来交换位置,结果就悲 ...
- 推荐10款超级有趣的HTML5小游戏
HTML5的发展速度比任何人的都想像都要更快.更加强大有效的和专业的解决方案已经被开发......甚至在游戏世界中!这里跟大家分享有10款超级趣味的HTML5游戏,希望大家能够喜欢! Kern Typ ...
- 如何开发一个简单的HTML5 Canvas 小游戏
原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...
- JavaScript版拼图小游戏
慕课网上准备开个新的jQuery教程,花了3天空闲时间写了一个Javascript版的拼图小游戏,作为新教程配套的分析案例 拼图游戏网上有不少的实现案例了,但是此源码是我自己的实现,所以不做太多的比较 ...
- C语言-纸牌计算24点小游戏
C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...
随机推荐
- apache开源项目--TIKA
Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...
- c语言中较常见的由内存分配引起的错误_内存越界_内存未初始化_内存太小_结构体隐含指针
1.指针没有指向一块合法的内存 定义了指针变量,但是没有为指针分配内存,即指针没有指向一块合法的内浅显的例子就不举了,这里举几个比较隐蔽的例子. 1.1结构体成员指针未初始化 struct stude ...
- 【转】Android异步消息处理机制完全解析,带你从源码的角度彻底理解
原文网址:http://blog.csdn.net/guolin_blog/article/details/9991569 转载请注明出处:http://blog.csdn.net/guolin_bl ...
- Tomcat配置JNDI数据源
经过3个多小时的努力,配置JNDI数据源(主要是通过DBCP连接池)终于搞定-还是Tomcat官方的说明好,不过全是英文的,大概还看得懂.百度上那么花花绿绿的太多了,一个也没成功!...本例使用的数据 ...
- 一个简单的MDI示范程序(Delphi)
http://www.cnblogs.com/pchmonster/archive/2012/01/07/2316012.html 最为一个巩固之前有关窗体和对象的有关知识,下面就建立一个简单的MDI ...
- MSP430主系统时钟以及430的低功耗设置
如何将系统时钟设置到外部高频晶体振荡器,430的MCLK默认的是DCO的,如何安全的从DCO切换到外部晶体振荡器,这是一个很重要的步骤,因为经过此步骤,可以极大地提高430的处理能力,DCO在内部,可 ...
- openCV学习笔记
学习新玩意儿的事情,必须要懂人家的规矩,openCV就有自己的规范和数据结构的,要用人家的那些库函数,必须要把传入参数的基础结构搞搞明白,比如RGBA,人家opencv已经说了,用CvScalar,就 ...
- Windows内核编程之:分页内存与非分页内存 #define PAGEDCODE code_seg("PAGE") (转)
原文链接:http://blog.chinaunix.net/uid-24504987-id-161192.html Windows规定有些虚拟内存可以交换到文件中,这类内存被称为分页内存 有些虚拟内 ...
- Android Socket 相关
http://www.imooc.com/learn/223 http://www.epubit.com.cn/book/onlinechapter/12093
- Harris 角点检测
一 .Motivation 对于做图像处理的人来说,Harris角点检测肯定听过,1988年发表的文章"A combined corner and edge detector"描述 ...