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,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...
随机推荐
- 【转】 Homebrew – OSX下简单的包管理系统
很多linux用户很喜欢 (Debian/Ubuntu)系列的apt包管理系统和(Redhat/Fedora)系列的yum包管理系统. 包括Windows用户都有多种方便的软件管理工具,如:360软件 ...
- @Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction区别 .(转)
mvc renderaction renderpartial 杂谈 Html.RenderPartial与Html.RenderAction这两个方法都是用来在界面上嵌入用户控件的. ...
- erlang判断语法结构:if/case/guard
erlang 有好几种常用的判断结构语句,如 if.case.guard 等.文章将分别对 if / case /guard 的特点做介绍,以及用例说明 1.if 结构 if Condition 1 ...
- 实用Python 语句集(入门者入)
1. Python IDLE中切换当前路径 在Python自带的编辑器IDLE中或者Python shell中不能使用cd命令,那么跳到目标路径呢. 方法是使用os包下的相关函数实现路径切换功能. i ...
- java jvm学习笔记八(实现jar包的代码签名)
欢迎装载请说明出处:http://blog.csdn.net/yfqnihao/article/details/8267669 课程源码:http://download.csdn.net/detai ...
- 【Jenkins】linux下Jenkins集成ant进行编译并发送结果
三个文章吧: 1 如何使用ant编译执行jmeter测试用例,并生成html报告 2 如何在Linux下搭建jenkins环境. 3 如何在Linux下搭建的jenkins中执行ant构建运行,并发送 ...
- 【JMeter】JMeter完成一个java请求的压测
先定义一下我说的remoteService:即远程调用服务,没有http的url.不对外提供或者对外提供有限的服务.具体视各公司的代码架构所定,比如有些公司为web工程,scf服务,db.scf即为服 ...
- uva11992-Fast Matrix Operations(区间增值、改值)
题意: r行c列的全0矩阵 有三种操作 1 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素增加v 2 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素设为v ...
- Python脚本控制的WebDriver 常用操作 <五> 访问链接
下面将使用webdriver来访问一个web链接 测试用例场景 测试中,经常会点击几个链接来进行操作,所以访问链接是基本的常见操作 Python脚本 from selenium import webd ...
- C#给文件重命名
使用的主要方法是: File.Move(oldFileDir,newFileDir);//这个是移动文件的方法 Directory.GetFiles(dir);//获取dir路径下的所有文件的全路径 ...