(cocos2dx)使用__Array容器检测碰撞时间

前端之家收集整理的这篇文章主要介绍了(cocos2dx)使用__Array容器检测碰撞时间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

同样是学了别人教学视屏上的代码,但是在执行的时候发现在使用CCArray的时候,运行会报错。

然后我就翻了一下书,CCArray是2.x版本的,3.x是用__Array。

两者在创建时是有所不同的。

后来我就把里面用CCArray定义的都改成__Array来定义,就能正常运行。


#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto 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 ( !LayerColor::initWithColor(ccc4(255,255,255)) )//红 绿 蓝 alpha(透明度)
    {
        return false;
    }
#if 0
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image,which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
    
	closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2));

    // create menu,it's an autorelease object
    auto menu = Menu::create(closeItem,NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu,1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    
    auto label = Label::createWithTTF("Hello World","fonts/Marker Felt.ttf",24);
    
    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label,1);

    // add "HelloWorld" splash screen"
    auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
    this->addChild(sprite,0);
 #endif  

	CCSize screenSize=CCDirector::sharedDirector()->getVisibleSize();//在导演类中获取屏幕高度
    CCSprite* player=CCSprite::create("Player.png");
	player->setPosition(ccp(0+20,screenSize.height/2)); 
	this->addChild(player);
	
#if 0
	CCMenuItemImage* item=CCMenuItemImage::create("button2.png","button2.png",this,menu_selector(HelloWorld::responseFunc));

	item->setPosition(ccp(30,30));
	CCMenu* menu=CCMenu::create(item,NULL);
	this->addChild(menu);
#endif

	this->schedule(schedule_selector(HelloWorld::gameLogic),2);
	this->schedule(schedule_selector(HelloWorld::update));

	this->setTouchEnabled(true);
	setTouchMode(Touch::DispatchMode::ONE_BY_ONE);

	//_projs=new CCArray;
	//_targets=new CCArray;
	_projs=__Array::createWithCapacity(100);
	_targets=__Array::createWithCapacity(100);
	//在cocos2d.3x中创建Array需要用__Array,不应该用CCArrray不然会报错
	_projs->retain();//需要保持内存
	_targets->retain();


	return true;

}

void HelloWorld::update(float delta)
{
	__Array* targetToDelete=__Array::createWithCapacity(100);
	__Array* projToDelete=__Array::createWithCapacity(100);
	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);
		}

	}
	}
	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();
	targetToDelete->removeAllObjects();
	projToDelete->removeAllObjects();

}	


HelloWorld::~HelloWorld()
{
	/*
	if(_projs!=NULL)
		_projs->release();

	if(_targets!=NULL)
		_targets->release();
		*/
	_projs->removeAllObjects();
	CC_SAFE_RELEASE_NULL(_projs);//先释放在赋予nullptr
	_targets->removeAllObjects();
	CC_SAFE_RELEASE_NULL(_targets);
}


bool HelloWorld::onTouchBegan(Touch *pTouches,CCEvent *pEvent)
{
	//Touch* touch=(Touch*)pTouches->getLocation();	
	Vec2 location=pTouches->getLocation();
	//Point locInView=touch->getLocationInView();
	//Point loc=Director::sharedDirector()->convertToGL(locInView);

	if(location.x<=20)
	{
		return 1;
	}


	CCSize screenSize=CCDirector::sharedDirector()->getVisibleSize();
	
	CCSprite* proj=CCSprite::create("Projectile.png");
	proj->setPosition(ccp(20,screenSize.height/2.0));
	this->addChild(proj);
	proj->setTag(2);
	_projs->addObject(proj);
	
	
	double dx=location.x-20;
	double dy=location.y-screenSize.height/2.0;
	double d=sqrt(dx*dx+dy*dy);

	double D=sqrt(screenSize.width*screenSize.width+screenSize.height*screenSize.height);
	double ratio=D/d;
	double endx=ratio*dx+20;
	double endy=ratio*dy+screenSize.height/2.0;

	CCMoveTo* move=CCMoveTo::create(D/520,ccp(endx,endy));
	CCCallFuncN* moveFinish=CCCallFuncN::create(this,callfuncN_selector(HelloWorld::myDefine));
	CCSequence* actions=CCSequence::create(move,moveFinish,NULL);
	proj->runAction(actions);
}


void HelloWorld::gameLogic(float dt)
{
	this->createTarget();
}


void HelloWorld::createTarget()
{
	CCSize screenSize=CCDirector::sharedDirector()->getVisibleSize();

	CCSprite* target=CCSprite::create("Target.png");
	int y=rand()%(int)screenSize.height;
	target->setPosition(ccp(screenSize.width-20,y));
	this->addChild(target);
	target->setTag(1);
	_targets->addObject(target);
	

	CCMoveTo* move=CCMoveTo::create(2,ccp(0,y));
	CCCallFuncN* disappear=CCCallFuncN::create(this,disappear,NULL);
	target->runAction(actions);
}


void HelloWorld::responseFunc(Ref* obj)
{
	//CCLOG("menu item clicked");
	CCMoveTo* move=CCMoveTo::create(2,40));
	CCCallFuncN* disappear=CCCallFuncN::create(this,NULL);
	target->runAction(actions);
}


void HelloWorld::myDefine(Node* who)
{
	//who->setPosition(ccp(10,10));
	//who->setScale(2);
	who->removeFromParentAndCleanup(true);
    
	int tag=who->getTag();
	if(tag==1)
	{
		_targets->removeObject(who);
	}
	else if(tag==2)
	{
		_projs->removeObject(who);
	}

}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

猜你在找的Cocos2d-x相关文章