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,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...
随机推荐
- 嵌入式Linux USB WIFI驱动的移植
硬件平台:飞思卡尔MX258开发板 操作系统:Linux2.6.31 WIFI: RT2860 USB WIFI模组 交叉编译环境:gcc version 4.1.2 调试步骤: 第一步:测试U ...
- C# 中的装箱与拆箱
转角撞倒猪 原文 C# 中的装箱与拆箱 装箱:将一个数据项(副本)从栈中自动复制到堆中的行为. int i = 8; object o = i; // 装箱 // 首先在堆中开辟出一片区域,再将 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.6
If $\sen{A}<1$, then $I-A$ is invertible, and $$\bex (I-A)^{-1}=I+A+A^2+\cdots, \eex$$ aa converg ...
- 基于kryonet的RPC,使用kryo进行序列化
Kryo是一个序列化框架. Kryonet是一个基于kryo的RPC框架,它实现了一套高效简洁的API,它通过NIO实现了TCP和UDP通讯,目前还不支持Http. 自己写了一个测试代码,运行了下,感 ...
- 那些年一起踩过的坑 — java 自动装箱拆箱问题
坑在哪里? 我们都知道Java的八种基本数据类型:int, short, long, double, byte, char, float, boolean 分别有各自对应的包装类型:Integ ...
- RAM, SDRAM ,ROM, NAND FLASH, NOR FLASH
在看上面2440的内存映射的时候,对其中的有些名字,不是完全太懂,所以到网上找了相关的信息. 对于mini2440来说,SDRAM,即内存,程序运行时的地方.选择连接SDRAM的为bank6. 1)S ...
- NOIP2014 寻找道路
2.寻找道路 (road.cpp/c/pas) [问题描述] 在有向图G中,每条边的长度均为1,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1.路径上的所有点的出边所指 ...
- 【暑假】[深入动态规划]UVAlive 4794 Sharing Chocolate
UVAlive 4794 Sharing Chocolate 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=12055 ...
- Android Studio工程导入另一个工程作为lib
简单视频应用开发时,使用Vitamio作为视频解码库,官方建议直接以工程作为lib方便升级,将该工程导入到项目时不知道该怎么做,参考了下面的博客,这里存档标记一下. 参考:导入一个Android St ...
- Tcp/Ip协议族简单解读及网络数据包/报/帧数据格式及封装及解包;
http://www.creseek.cn/products-install/install_on_bsd_linux/ 中文检索 离线cloudera ecosystem components: h ...