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,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...
随机推荐
- 解决IIS6.0不能下载EXE文件之妙方!
2003系统 IIS6建站 有EXE文件下载 但老是不让下载.当我把站删除再建后可以下载,但过一会又不让下载了.另,MIME里有EXE这项!请问这是为什么?应如何处理? 答案: win2003服务器允 ...
- WPF学习笔记 - 在XAML里绑定
Binding除了默认构造函数外,还有一个可以传入Path的构造函数,下面两种方式实现的功能是一样的. <TextBlock x:Name="currentFolder" D ...
- HUST 1017 Exact cover dance links
学习:请看 www.cnblogs.com/jh818012/p/3252154.html 模板题,上代码 #include<cstdio> #include<cstring> ...
- [King.yue]Ext中Grid得到选择行数据的方法总结
(1)grid.getStore().getRange(0,store.getCount()); //得到grid所有的行 (2)grid.getSelectionModel().getSele ...
- java语言实现简单接口工具--粗简版
2016注定是变化的一年,忙碌.网红.项目融资失败,现在有点时间整整帖子~~ 目标: 提高工作效率与质量,能支持平台全量接口回归测试与迭代测试也要满足单一接口联调测试. 使用人员: 测试,开发 工具包 ...
- 软件测试模型汇总-V模型,W模型,X模型,H模型
V模型 在软件测试方面,V模型是最广为人知的模型,尽管很多富有实际经验的测试人员还是不太熟悉V模型,或者其它的模型.V模型已存在了很长时间,和瀑布开发模型有着一些共同的特性,由此也和瀑布模型一样地受到 ...
- Java HashMap存储问题
public static boolean isIsomorphic(String s, String t) { Map map1 = new HashMap<>(); Map map2 ...
- js基础第八天
返回前面起第一个字符位置 indexOf("sdfsdf");它是从左边索引为0开始数,而且只找第一个,然后返回该字符的位置.返回是个数值.如果找不到该字符,那么就会返回-1. 返 ...
- 【译】 AWK教程指南 5AWK中的数组
awk程序中允许使用字符串当做数组的下标(index).利用这个特色十分有助于资料统计工作.(使用字符串当下标的数组称为Associative Array) 首先建立一个数据文件,并取名为 reg.d ...
- Python的数据类型的一些知识
int 类型表示(正负)整数,str 类型表示字符串(Unicode字符序列): 所表示的整数大小只受限于机器内存,而非固定的字节数. 字符串可以用双引号或是单引号封装,需要成对的出现. 传统上,py ...