本文主要是介绍改变背景颜色+碰撞检测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.让类继承CCLayerColor比如
class HelloWorld:public cocos2d::CCLayerColor{
public :
在.cpp文件中
bool HelloWorld::init(){
if(!CCLayerColor::initWithColor(ccc4(255,255,255,255))){
return false;
}
使背景颜色变为白色。
2.ccTouchesEnded(CCSet*pTouches,CCEvent*pEvent){
先取得一个点。
CCTouch*onePoint=(CCTouch*)pTouches->anyObject(); //取得一个点,。强制转换一下
CCPoint location=onePoint->getLocationInview(); //取得UI坐标系下的坐标
CCPoint loc=CCDirector::sharedDirector()->convertToNodeSpace(location); //OPENGL
3.array删除添加难,遍历容易,
list删除添加容易,遍历难,
但是遍历fps时间就一次,但是增加删除2.0s才一次,所以存放在array中,
4.释放两个数组
HelloWorld::~HelloWorld(){
if(_projs!=NULL){
_projs->release(); //数组释放
}
5.关于tag
int tag=who->getByTag();
if(tag==1){
_projs->removeObject(who);
if(tag==2){
_taget->removeObject(who);
.
6.关于update,如果是碰撞检测的话是调用update方法, 即每一帧都检测
- CCARRAY_FOREACH(_targets, itarget){
- CCSprite* target = (CCSprite*)itarget;
- itarget是CCObject类型,这样转换一下才能得到相应的区域。
- 得到怪物的的矩形区域
- CCRect targetZone=CCRectMake(
- target->getPositionX(),
- target->getPositionY(),
- target->getContentSize().width,
- target->getContentSize().height);
-
- CCARRAY_FOREACH(_projs, iproj){
- CCSprite* proj = (CCSprite*)iproj;
- CCRect projZone = CCRectMake(proj->getPositionX(),
- proj->getPositionY(),
- proj->getContentSize().width,
- proj->getContentSize().height);
-
- CCArray* targetToDelete = new CCArray;
- CCArray* projToDelete = new CCArray;
- projToDelete->addObject(iproj);
- targetToDelete->addObject(itarget);
- CCARRAY_FOREACH(projToDelete, iproj){
- _projs->removeObject(iproj);
- CCSprite* proj = (CCSprite*)iproj;
- proj->removeFromParentAndCleanup(true);
- }
- CCARRAY_FOREACH(targetToDelete, itarget){
- _targets->removeObject(itarget);
- CCSprite* target = (CCSprite*)itarget;
- target->removeFromParentAndCleanup(true);
- }
targetToDelete->release();
projToDelete->release();
7.
- void HelloWorld::update(float delta) // delta = 1.0 / fps
- {
- CCArray* targetToDelete = new CCArray;
- CCArray* projToDelete = new CCArray;
- CCObject* itarget;
- CCObject* iproj;
- CCARRAY_FOREACH(_targets, itarget){
- CCSprite* target = (CCSprite*)itarget;
- CCRect targetZone = CCRectMake(target->getPositionX(),
- target->getPositionY(),
- target->getContentSize().width,
- target->getContentSize().height);
- CCARRAY_FOREACH(_projs, iproj){
- CCSprite* proj = (CCSprite*)iproj;
- CCRect projZone = CCRectMake(proj->getPositionX(),
- proj->getPositionY(),
- proj->getContentSize().width,
- proj->getContentSize().height);
- if (projZone.intersectsRect(targetZone)){
- projToDelete->addObject(iproj);
- targetToDelete->addObject(itarget);
- }
- } // end of iterate projectile
- } // end of iterate target
- CCARRAY_FOREACH(projToDelete, iproj){
- _projs->removeObject(iproj);
- CCSprite* proj = (CCSprite*)iproj;
- proj->removeFromParentAndCleanup(true);
- }
- CCARRAY_FOREACH(targetToDelete, itarget){
- _targets->removeObject(itarget);
- CCSprite* target = (CCSprite*)itarget;
- target->removeFromParentAndCleanup(true);
- }
- targetToDelete->release();
- projToDelete->release();}
这篇关于改变背景颜色+碰撞检测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!