我为一些飞行的游戏编写了代码。我创建了CCSprite
数组并在Scene上添加了它们。现在它为每个鸟对象分配一个调度程序,以便在屏幕中随机移动。一切正常。现在我在遇到鸟类碰撞方面遇到了麻烦。
我正在尝试在数组上运行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("$$$$$$$$$$$$$$$$$$$$$$$");
}
}
答案 0 :(得分:1)
最好在一个更新函数中执行操作...例如,在该调用中进行一次更新MoveBird函数将包含您的移动鸟代码
其次现在调用检查碰撞功能
void update (float dt) {
MoveBird(); // Move how many birds you want to move
CheckCollision();
}
现在如何检查鸟类的碰撞
在你的碰撞函数forLoop你的鸟类阵列检查
if(mBirdsArray[i]->boundingBox().containsPoint(mBirdsArray[j]->getPosition()))
反之亦然......
我希望这会有效......也许比这更好的逻辑......