我为一些鸟儿飞翔的游戏编写了代码。我创建了数组CCSprite
并将它们添加到场景中。现在它为每个鸟对象分配一个调度程序,以便在屏幕中随机方向移动。所有人都在工作。现在我在鸟类碰撞中遇到了麻烦。
我正在尝试CCARRAY_FOREACH
在数组上运行一个循环,但它给出了错误。
0xC0000005: Access violation reading location 0xfeeefeee.
请帮助我如何在数组元素移动时获得碰撞检测。updateCollison
方法未执行。
我的代码是:
#include "HelloWorldScene.h"
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//srand(time(NULL));
CCSize size = CCDirector::sharedDirector()->getWinSize();
birds = CCArray::create();
for(int j = 0; j<5; j++){
CCSprite *bird = CCSprite::create("twitter_square.png");
bird->setTag(j);
int max = rand() % 480;
int min = rand() % 320;
bird->setPosition(ccp(max, min));
this->addChild(bird);
birds->addObject(bird);
bird->schedule( schedule_selector(HelloWorld::moveBird), 5.0 );
bird->schedule( schedule_selector(HelloWorld::updateCollison), 5.0 );
}
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCDirector::sharedDirector()->end();
}
void HelloWorld::moveBird(float dt)
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
int max = rand() % (int)size.width;
int min = rand() % (int)size.height;
CCActionInterval* actionTo = CCMoveTo::actionWithDuration(5, ccp(max,min));
this->runAction(actionTo);
}
void HelloWorld::updateCollison(float dt)
{
CCObject *obj;
for(int i=0; i < birds->count(); i++)
{
CCLOG("$$$$$$$$$$$$$$$$$$$$$$$");
}
}