位置: 编程技术 - 正文

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开发学习教程(非常好的成语)

  • 小规模纳税人税率1%政策到什么时候
  • 一般纳税人的税点
  • 按份共同保证和连带共同保证
  • 未分配利润转增股本会计处理
  • 企业合并固定资产增值的规定
  • 作家以及作品
  • 报关单和进口增值税专用缴款书联系
  • 物流企业会计核算的主要内容
  • 申报财产租赁合同印花税是在什么情况下?
  • 如何查询当月社保和公积金是否缴纳
  • 房地产企业所得税清算条件
  • 职工福利费要交个税吗
  • 税控盘未上传发票查询
  • 组合销售带来的收益
  • 最新粮食购销企业税务规定
  • 出口退税免退税
  • 工资放在主营业务成本和放在管理费用一样吗
  • 利润表与所得税申报表不符
  • 监证方什么意思
  • 鸿蒙系统怎么自动填充密码
  • 苹果紧急提醒
  • 如何设置win10电脑开机不黑屏
  • linux sed 行尾
  • os x yosemite dp6下载 os x 10.10 dp6官方下载地址
  • php字符串操作函数
  • wordpress使用
  • 高新技术产业的税收优惠
  • 扣缴义务人需要办理税务登记吗
  • 受托方代扣代缴的消费税计入什么科目
  • 自产产品用于应税项目为什么不考虑偷税
  • 空调年折旧率
  • 定期存款应计入会计哪个科目
  • 活动补助会计分录
  • 什么样的资产可以执行
  • 生产销售库存的会计科目
  • 数据结构—python语言描述
  • 织梦cms要钱吗
  • MongoDB aggregate 运用篇个人总结
  • 出租营改增之前取得的有形动产
  • 固定资产净残值账务处理
  • 法人转钱入公户要交税吗
  • 票据背书转让会计处理
  • 开房租发票交的税因优惠政策退税如何账务处理?
  • 报废的机器设备属于什么会计要素
  • ibm.data.db2
  • 劳务公司给包工头转账交税吗
  • 销售折让怎么写分录
  • 收到返款计入什么科目
  • 用友t3软件的系统内没有利润表模块
  • 进项税额转出分录怎么写,附加的原始凭证是什么
  • 进口卷烟消费税定额税率
  • 购买商标权需要缴税吗
  • 收到实务返利的发票
  • 以物易物方式销售货物的增值税处理
  • 中标服务费计入合同取得成本
  • 会展费会计分录
  • 个人如何成立公司
  • 私营企业会计退休年龄50还是55
  • 房地产企业会计分录
  • winscope是什么意思
  • windows预体验版本遇到问题
  • mac的替换在哪里
  • win7主要有哪些内容
  • 索尼笔记本安装软件顺序
  • Linux下使用httpry来嗅探HTTP流量教程
  • win1020h2版好不好
  • 系统右键菜单
  • linuxat命令的用法
  • 写出javascript的数据类型
  • 环境篇-幸福家庭是孩子心灵健康的关键.mp3
  • HTML5 WebStorage(HTML5本地存储技术)
  • nodejs cgi
  • 序列化和反序列化是什么意思
  • python3m
  • python3.9爬取网页教程
  • jquery表单事件实例
  • vue瀑布流实现
  • python设计二叉树结构
  • 国家税务局发票验证查询系统
  • 公司有房屋租赁许可证吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设