位置: 编程技术 - 正文

Cocos2d-x碰撞检查与消灭的实现(cocos creator 碰撞检测)

编辑:rootadmin
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" using namespace cocos2d; class HelloWorld : public cocos2d::CCLayerColor { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); void addTarget(); void spriteMoveFinished(CCNode *sender); void gameLogic(cocos2d::CCTime dt); void ccTouchesEnded(CCSet *touches, CCEvent *event); CCArray *aarayTarget;//敌人 CCArray *arrayProjectile;//飞镖 void update(CCTime dt); }; #endif // __HELLOWORLD_SCENE_H__ HelloWorldScence.cpp[objc] view plaincopyprint?#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( CCLayerColor::initWithColor(ccc4(, , , )) ) { CCSize winSize = CCDirector::sharedDirector()->getWinSize();//获取屏幕大小 arrayProjectile = CCArray::create(); aarayTarget = CCArray::create(); float sprite_scale = 2.0; CCSprite *Player = CCSprite::create("Player.png"); Player->setScale(sprite_scale); Player->setPosition(ccp(Player->getContentSize().width*sprite_scale/2.0, winSize.height/2.0)); this->addChild(Player); aarayTarget->retain(); arrayProjectile->retain(); this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0); this->schedule(schedule_selector(HelloWorld::update)); this->setTouchEnabled(true); return true; } else{ return false; } } void HelloWorld::gameLogic(cocos2d::CCTime dt){ this->addTarget(); } void HelloWorld::addTarget(){ CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *target = CCSprite::create("Target.png"); //随机位置 int minY = target->getContentSize().height/2.0; int maxY = winSize.height - target->getContentSize().height/2.0; int rangeY = maxY - minY; int actualY = rand()%rangeY &#; minY; target->setPosition(ccp(winSize.width - target->getContentSize().width/2.0, actualY)); target->setTag(1); this->addChild(target); aarayTarget->addObject(target); //随机速度 float minDuration = 2.0; float maxDuration = 4.0; int rangeDuration = maxDuration - minDuration; float actualDuration = rand()%rangeDuration &#; minDuration; CCFiniteTimeAction *actionMove = CCMoveTo::create(actualDuration, ccp(0 - target->getContentSize().width/2.0, actualY));//0代表屏幕外,这句表示在3秒内从初始位置移动到屏幕外 //增加一个回调函数,回收移动到屏幕外的精灵 CCFiniteTimeAction *actionMoveDone = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)); target->runAction(CCSequence::create(actionMove,actionMoveDone,NULL)); } void HelloWorld::spriteMoveFinished(cocos2d::CCNode *sender){ CCSprite *sprite = (CCSprite *)sender; // this->removeChild(sprite, true); if (sprite->getTag() == 1) { aarayTarget->removeObject(sprite);//清除敌人 }else if(sprite->getTag() == 2){ arrayProjectile->removeObject(sprite);//清除飞镖 } } //发射飞镖 void HelloWorld::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *event){ CCTouch *touch = (CCTouch *)touches->anyObject(); CCPoint location = touch->getLocationInView(); location = this->convertTouchToNodeSpace(touch); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSprite *projectile = CCSprite::create("Projectile.png"); projectile->setPosition(ccp(, winSize.height/2)); float offX = location.x - projectile->getPositionX(); float offY = location.y - projectile->getPositionY(); if (offX <= 0) { return; } projectile->setTag(2); this->addChild(projectile); arrayProjectile->addObject(projectile); float angle = offY/offX; float realX= winSize.width &#; projectile->getContentSize().width/2; float realY = realX * angle &#; projectile->getPositionY(); CCPoint finalPosition = ccp(realX, realY); //获取飞镖飞行时间 float length = sqrtf(realX *realX &#; realY*realY); float velocity = ; float realMoveDuration = length/velocity; projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, finalPosition),CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL)); } //碰撞检测,消除飞镖和敌人 void HelloWorld::update(cocos2d::CCTime dt){ for (int i = 0; i < aarayTarget->count(); i&#;&#;) { CCSprite *target = (CCSprite *)aarayTarget->objectAtIndex(i); for (int j = 0; j < arrayProjectile->count(); j&#;&#;) { CCSprite *projectile = (CCSprite *)arrayProjectile->objectAtIndex(j); if (target->boundingBox().intersectsRect(projectile->boundingBox())) { aarayTarget->removeObjectAtIndex(i); arrayProjectile->removeObjectAtIndex(j); this->removeChild(target); this->removeChild(projectile); break; } } } } void HelloWorld::menuCloseCallback(CCObject* pSender) { CCDirector::sharedDirector()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }

推荐整理分享Cocos2d-x碰撞检查与消灭的实现(cocos creator 碰撞检测),希望有所帮助,仅作参考,欢迎阅读内容。

Cocos2d-x碰撞检查与消灭的实现(cocos creator 碰撞检测)

文章相关热门搜索词:cocoscreator碰撞,碰撞检测unity,cocos 碰撞,cocoscreator碰撞,cocos2dx碰撞检测,cocos 碰撞,cocos creator 碰撞检测,cocos2dx碰撞检测,内容如对您有帮助,希望把文章链接给更多的朋友!

非常好的cocos2d-x开发学习教程 COCOS2D开发者文档cocos2d,开发,IT,程序,教程,动画,游戏,文档,cocos2dxCOCOS2D-X开发者文档,一次下载不需要联网,所有资源单机,教程资源非常详细,包括:coc

在win7上python2.7环境下安装cocos-d-0.6.0 1.下载安装pyglet-1.2alpha1。由于我的系统已经安装python2.7,需自行安装pyglet下载地址:

OSX下cocos-2d的安装注意事项 1.同上篇win7中安装的基本步骤一样。先下载并安装pyglet1.2alpha版本。不过请使用root权限来安装。如果不清楚root密码,请按如下进行密码更改。MacBook-Air:~

标签: cocos creator 碰撞检测

本文链接地址:https://www.jiuchutong.com/biancheng/369144.html 转载请保留说明!

上一篇:Cocos2d-js入门学习笔记(cocos2dx4.0入门)

下一篇:非常好的cocos2d-x开发学习教程(非常好的成语)

  • 企业纳税人是什么
  • 白酒赠品赠什么方案
  • 工业企业取得土地收益
  • 制造费用科目一定无余额
  • 加油票怎么记账凭证
  • 计提应付票据利息的会计分录
  • 住房公积金证书插上为什么登不进去
  • 退货应入会计什么科目
  • 公司多久不做账会被注销?
  • 工业混合销售行为如何纳税?
  • 交强险和车船税必须一起交吗
  • 用友t3财务报表模块打不开
  • 报税提示您可能缺少相关组件怎么处理
  • 房地产开发企业预收款预缴增值税
  • 某产品今年进价是去年的
  • 出口企业进项税额转出怎么理解
  • 工资本月计提下月发放,个税如何计算做账
  • 带销货清单的专票咋红冲
  • 现金折扣通过财务费用核算
  • 企业所得税法的规定可在税前扣除的
  • 成本计算错误如何应对
  • 任务栏图标重叠一起
  • 跨年度暂估成本入账冲回如何会计分录
  • 上年度的费用今年怎么算
  • 生育津贴报销需要准生证吗
  • 完美解决usb电涌15秒后关机
  • Linux系统中怎么定位到java代码的方法级
  • 固定资产折旧表述不正确的是
  • 摇篮山圣克莱尔湖国家公园
  • 资产减值损失的计算公式
  • 有限责任公司股东对公司债务承担
  • 取得土地使用权的方式
  • 收取职工交来伙食费如何入账
  • thinkphp技巧
  • yii框架手册
  • Yii2中hasOne、hasMany及多对多关联查询的用法详解
  • 投资者投资企业项目的主要目的是
  • 税金及附加和营业收入有关系吗
  • 猿厂猿作设计机构招聘
  • anaconda卸载干净
  • javaweb界面设计
  • docker的常用命令汇总
  • gpt3 transformer
  • 集团对子公司拨款的规定
  • 抵扣联丢失如何抵扣
  • 个人劳务费需交什么税种
  • 金融企业往来支出属于费用吗
  • 待摊费用的明细
  • 增值税发票认证在哪里
  • 商业汇票利息账务处理如何做?
  • 一般纳税人年收入500万交多少税
  • 模具费收入计入什么科目
  • 采购成本和销售成本谁影响利润
  • mysql sqlyog
  • 个税返还手续费政策
  • 核价人员要对哪些方面的价格进行核定,怎么核?
  • 怎样申请开发票
  • 没有报关单可以出口吗
  • 企业年报修改后没有公示怎么办
  • 借别人的承兑后期还现钱可以吗?
  • pc版直通车
  • xp操作系统入门
  • ubuntu 14.04安装
  • linux 压缩包zip
  • win8右下角
  • win8如何更改用户名字?
  • linux删错文件
  • win8怎么把网速调到最快
  • c调用java后又调用回
  • unity sliced
  • jquery配合.NET实现点击指定绑定数据并且能够一键下载
  • Shell中使用scp命令实现文件上传代码
  • 置顶语句子
  • HTTP状态代码以及定义(解释)
  • javascript create
  • jQuery+ajax+asp.net获取Json值的方法
  • python 正则 \s
  • ca锁使用
  • 青岛怎样网上交医保
  • 登录上海电子税务局显示获取公告失败
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设