今天写了一个小游戏,发现看过的代码自己来写还是会经常出错,还是要多自己动手写写哈。

先上几张游戏界面图

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 小游戏的更多相关文章

  1. Cocos2d—X游戏开发之CCToggle(菜单标签切换)CCControlSwitch(开关切换)

    Cocos2d—X游戏开发之CCToggle(菜单标签切换) 首先继承子CCMenu,是菜单标签中的一种.‘ class CC_DLL CCMenuItemToggle : public CCMenu ...

  2. [SpriteKit] 系统框架中Cocos2d-x制作小游戏ZombieConga

    概述 使用SpriteKit实现一个简单的游戏, 通过一个游戏来进行SpriteKit的入门, 熟练2D游戏的API, 也可以更好的结合在iOS应用中. 详细 代码下载:http://www.demo ...

  3. iOS cocos2d 2游戏开发实战(第3版)书评

    2013是游戏爆发的一年,手游用户也是飞速暴增.虽然自己不做游戏,但也是时刻了解手机应用开发的新动向.看到CSDN的"写书评得技术图书赢下载分"活动,就申请了一本<iOS c ...

  4. jQuery实践-网页版2048小游戏

    ▓▓▓▓▓▓ 大致介绍 看了一个实现网页版2048小游戏的视频,觉得能做出自己以前喜欢玩的小游戏很有意思便自己动手试了试,真正的验证了这句话-不要以为你以为的就是你以为的,看视频时觉得看懂了,会写了, ...

  5. 拼图小游戏之计算后样式与CSS动画的冲突

    先说结论: 前几天写了几个非常简单的移动端小游戏,其中一个拼图游戏让我郁闷了一段时间.因为要获取每张图片的位置,用`<style>`标签写的样式,直接获取计算后样式再用来交换位置,结果就悲 ...

  6. 推荐10款超级有趣的HTML5小游戏

    HTML5的发展速度比任何人的都想像都要更快.更加强大有效的和专业的解决方案已经被开发......甚至在游戏世界中!这里跟大家分享有10款超级趣味的HTML5游戏,希望大家能够喜欢! Kern Typ ...

  7. 如何开发一个简单的HTML5 Canvas 小游戏

    原文:How to make a simple HTML5 Canvas game 想要快速上手HTML5 Canvas小游戏开发?下面通过一个例子来进行手把手教学.(如果你怀疑我的资历, A Wiz ...

  8. JavaScript版拼图小游戏

    慕课网上准备开个新的jQuery教程,花了3天空闲时间写了一个Javascript版的拼图小游戏,作为新教程配套的分析案例 拼图游戏网上有不少的实现案例了,但是此源码是我自己的实现,所以不做太多的比较 ...

  9. C语言-纸牌计算24点小游戏

    C语言实现纸牌计算24点小游戏 利用系统时间设定随机种子生成4个随机数,并对4个数字之间的运算次序以及运算符号进行枚举,从而计算判断是否能得出24,以达到程序目的.程序主要功能已完成,目前还有部分细节 ...

随机推荐

  1. Java [leetcode 28]Implement strStr()

    题目描述: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...

  2. RMAN备份与恢复实例

    1. 检查数据库模式:   sqlplus /nolog    conn /as sysdba   archive log list (查看数据库是否处于归档模式中) 若为非归档,则修改数据库归档模式 ...

  3. 【转】开发者教程:如何将Android应用发布到Google Play(Android Market)官方市场

    原文网址:http://www.chinaapp.org/game/5594.html 作为一个专业的App开发者网站,竟然没有一篇讲述如何将Android App发布到Google Play的教程, ...

  4. ubuntu下eclipse打开win下的代码中文出现乱码

    问题出现的原因:因为windows下默认的编码是GBK,在ubuntu下是UTF-8所以,所以在windows下的注释,在ubuntu下就变成了乱码. 解决的方案: 1)  eclipse->w ...

  5. 数列极限---和Gauss(取整)函数有关

  6. Apache log4cxx用法

    一个好的系统通常需要日志输出帮助定位问题 .Apache基金会的log4cxx提供的完善的Log分级和输出功能.所以准备把该Log模块加入的系统中. 使用log4cxx需要满足一下功能: 1.提供日志 ...

  7. 【译】 AWK教程指南 3计算并打印文件中指定的字段数据

    awk 处理数据时,它会自动从数据文件中一次读取一条记录,并会将该记录切分成一个个的字段:程序中可使用 $1, $2,... 直接取得各个字段的内容.这个特色让使用者易于用 awk 编写 reform ...

  8. android sdk api结构解析

    一.系统级:android.accounts android.app     1.OS 相关         android.os         android.os.storage         ...

  9. HIbernate学习笔记(八) hibernate缓存机制

    hibernate缓存 一. Session级缓存(一级缓存) 一级缓存很短和session的生命周期一致,因此也叫session级缓存或事务级缓存 hibernate一级缓存 那些方法支持一级缓存: ...

  10. HW5.36

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...