0

何羽かの鳥が飛んでいるゲームのコードを書きました。の配列を作成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("$$$$$$$$$$$$$$$$$$$$$$$");
    }
}
4

1 に答える 1

1

1つの更新関数で処理することをお勧めします...たとえば、移動する鳥のコードを含むMoveBird関数の呼び出しで1つの更新を行います

次に、checkCollision関数を呼び出します

void update (float dt) {

MoveBird();     // Move how many birds you want to move 
CheckCollision();

}

今あなたの鳥の衝突をチェックする方法

衝突関数で鳥をループ配列チェック

if(mBirdsArray[i]->boundingBox().containsPoint(mBirdsArray[j]->getPosition()))

およびその逆 ...

私はこれがうまくいくことを願っています...おそらくこれよりも優れたロジックがあります..

于 2012-12-17T13:07:35.430 に答える