位置: 编程技术 - 正文

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

  • 年报纳税总额能查到吗
  • 增值税发票网上勾选平台
  • 个人在境外取得收入纳税办法
  • 加油站的成品油是石油公司配送吗
  • 个税手续费发给个人怎么做账
  • 资产报废需要税务登记吗
  • 存款利息缴纳个税
  • 金税盘显示已到锁死期,未到汇总期是什么原因
  • 退税抵税申请表
  • 企业城建税教育费附加和地教费的税率是多少
  • 拖欠供应商货款
  • 待认证进项分录
  • 单品毛利计算公式怎么算
  • 河道工程维护管理费征收
  • 全国增值税发票查询平台 手机版
  • 库存占销售的多少合适
  • 个人所得税怎么交的,怎么算的
  • 技术服务费收入属于提供劳务收入吗
  • 海关进口增值税可以抵扣吗
  • 失控发票的账务处理
  • 出口发票上的汇率按哪个开?
  • 刚成立的工业企业如何具体设计和考虑成本项目处理?
  • 合同成本如何设一级科目
  • 税务局返还的个税手续费需要缴纳增值税吗
  • 在window中
  • 债权债务重组公司
  • 其他债权投资期末公允价值的变动应当计入
  • 事业单位收受礼品怎么处理
  • 兼职劳务费税率是多少
  • 病毒变种太多
  • windows 11任务栏没有网络图标
  • php判断ua
  • 生产型企业出口退税计算案例
  • 企业所得税征收方式有哪些?
  • 前端网页设计的三大技术
  • 腾讯云验证码服务
  • es6特性及使用场景
  • 搭建小技巧
  • php红包源码
  • 贸易公司结转销售成本凭证怎么做
  • 企业应采用
  • 商业折扣,现金折扣,销售折让的核算特点
  • 税盘显示已反写
  • 购买股权溢价部分怎么做账
  • 织梦百科
  • 借款合同按什么缴纳印花税
  • 什么叫动量交易
  • 如何查询以前申请的新西兰签证记录
  • 个税系统更新在哪里
  • 工资是当月计提当月发放还是当月计提下月发放
  • 商誉的会计核算怎么核算
  • 投资收益属于资产嘛
  • sqlserver、mysql获取连接字符串步骤
  • 农机免税发票能抵扣吗
  • 路桥费税率是几个点
  • 给离退休人员发放以前年度在岗奖金需要计提工会经费吗
  • 新收入准则 2021
  • 收到预收款并已付款
  • 核销贷款收回账务怎么做
  • 盈余公积分配现金股利会影响留存收益吗
  • 支付给其他公司的借款属于什么现金流
  • 提取坏账会计分录怎么写
  • 去年的会计凭证做错了,今年发现要怎么修改
  • 仓管需要会计证吗
  • sqlserver高可用集群搭建
  • win8 开机
  • win10创建家庭
  • 电脑键盘上f1到f12快捷键的功能分别是
  • win7系统如何删除隐藏文件
  • 怎么给文件夹设置密码保护
  • cocos2d-x 3.2 在window平台vs2012下解决中文乱码问题
  • perl编程
  • android网络通信http
  • android aidl binder
  • python读取grib
  • javascript命名规范
  • 怎么理解python
  • 开票资料?
  • 陕西电子税务局新版
  • 广东省东莞电子税局
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设