Cocos2d-x:从CCTime转换为SEL_SCEDULE错误

时间:2012-10-03 02:46:13

标签: c++ cocos2d-x

我是一名初学者,刚刚关注了cocos2d-x的原生教程,我面临着一堵巨大的墙!

这是我的错误:

>c:\cocos2d-2.0-x-2.0.3\cocos2dsimplegame\classes\helloworldscene.cpp(86): error C2440:      'type cast' : cannot convert from 'void (__thiscall HelloWorld::* )(cocos2d::CCTime)' to 'cocos2d::SEL_SCHEDULE' 
>Pointers to members have different representations; cannot cast between them

我的cpp文件:

    #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()
{
    bool bRet = false;

    _targets = CCArray::create();
    _projectiles = CCArray::create();

    do {
        ////////////////////
        // super init first
        ////////////////////

        if ( !CCLayerColor::initWithColor( ccc4(255, 255, 255, 255) ) )
        {
            return false;
        }

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

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemWithNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        //Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        //Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

        //////////////////////
        // 2. add your codes below...

        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        CCSprite *player = CCSprite::spriteWithFile("Player.png", CCRectMake(0, 0, 27, 40));
        player->setPosition(ccp(player->getContentSize().width/2, winSize.height/2));
        this->addChild(player);

        bRet = true;
    } while (0);

    // Call game logic about every second
    this->schedule( schedule_selector(HelloWorld::gameLogic), 1.0);
    // You can shoot the bullet
    this->setTouchEnabled(true);
    this->schedule( schedule_selector(HelloWorld::update) );

    return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    // "close" menu item clicked
    CCDirector::sharedDirector()->end();
}

void HelloWorld::addTarget()
{
    CCSprite *target = CCSprite::spriteWithFile("Target.png", CCRectMake(0, 0, 27, 40));

    // Determine where to spawn the target along the Y axis
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    int minY = target->getContentSize().height/2;
    int maxY = winSize.height - target->getContentSize().height/2;

    int rangeY = maxY - minY;
    // srand( TimGetTicks() );
    int actualY = ( rand() % rangeY ) + minY;

    // Create the target slightly off-screen along the right edge,
    // and along a random position along the Y axis as calculated
    target->setPosition( ccp(winSize.width + (target->getContentSize().width/2), actualY) );
    this->addChild(target);

    // Determine speed of the target
    int minDuration = (int)2.0;
    int maxDuration = (int)4.0;
    int rangeDuration = maxDuration - minDuration;
    // srand( TimGetTicks() );
    int actualDuration = ( rand() % rangeDuration ) + minDuration;

    // Create the actions
    CCFiniteTimeAction* actionMove = CCMoveTo::actionWithDuration( (float)actualDuration, ccp(0 - target->getContentSize().width/2, actualY) );
    CCFiniteTimeAction* actionMoveDone = CCCallFuncN::actionWithTarget( this, callfuncN_selector(HelloWorld::spriteMoveFinished) );
    target->runAction( CCSequence::actions( actionMove, actionMoveDone, NULL) );

    // Add to targets array
    target->setTag(1);
    _targets->addObject(target);
}

void HelloWorld::spriteMoveFinished(CCNode* sender) 
{
    CCSprite *sprite = (CCSprite *)sender;
    this->removeChild(sprite, true);

    if (sprite->getTag() == 1) // target
    {
        _targets->removeObject(sprite);
    }
    else if (sprite->getTag() == 2) // projectile
    {
        _projectiles->removeObject(sprite);
    }
}

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

void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
    // Choose one of the touches to work with
    CCTouch* touch = (CCTouch*)( touches->anyObject() );
    CCPoint location = touch->locationInView();
    location = CCDirector::sharedDirector()->convertToGL(location);

    // Set up initial location of projectile
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCSprite *projectile = CCSprite::spriteWithFile("Projectile.png", CCRectMake(0, 0, 20, 20));
    projectile->setPosition( ccp(20, winSize.height/2) );

    // Determine offset of location to projectile
    int offX = location.x - projectile->getPosition().x;
    int offY = location.y - projectile->getPosition().y;

    // Bail out if we are shooting down or backwards
    if (offX <= 0) return;

    // OK to add now - we've double checked position
    this->addChild(projectile);

    // Determine where we wish to shoot the projectile to
    int realX = winSize.width + (projectile->getContentSize().width/2);
    float ratio = (float)offY / (float)offX;
    int realY = (realX * ratio) + projectile->getPosition().y;
    CCPoint realDest = ccp(realX, realY);

    // Determine the length of how far we're shooting
    int offRealX = realX - projectile->getPosition().x;
    int offRealY = realY - projectile->getPosition().y;
    float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
    float velocity = 480/1; // 480pixels/1sec
    float realMoveDuration = length/velocity;

    // Move projectile to actual endpoint
    projectile->runAction( CCSequence::actions( CCMoveTo::actionWithDuration(realMoveDuration, realDest), CCCallFuncN::actionWithTarget(this, callfuncN_selector(HelloWorld::spriteMoveFinished)), NULL) );

    // Add to projectiles array
    projectile->setTag(2);
    _projectiles->addObject(projectile);
}

void HelloWorld::update(CCTime dt)
{
    CCArray *projectilesToDelete = CCArray::create();
    CCObject* arrayItem1;
    CCARRAY_FOREACH(_projectiles, arrayItem1)
    {
        CCSprite* projectile = (CCSprite*)arrayItem1;
        CCRect projectileRect = CCRectMake(
            projectile->getPosition().x - (projectile->getContentSize().width/2),
            projectile->getPosition().y - (projectile->getContentSize().height/2),
            projectile->getContentSize().width,
            projectile->getContentSize().height);

        CCArray* targetsToDelete = CCArray::create();

        CCObject* arrayItem2;
        CCARRAY_FOREACH(_targets, arrayItem2)
        {
            CCSprite* target = (CCSprite*) arrayItem2;
            CCRect targetRect = CCRectMake(
                target->getPosition().x - (target->getContentSize().width/2),
                target->getPosition().y - (target->getContentSize().height/2),
                target->getContentSize().width, 
                target->getContentSize().height);

            if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
            {
                targetsToDelete->addObject(target);
            }
        }

        CCARRAY_FOREACH(targetsToDelete, arrayItem2)
        {
            CCSprite* target = (CCSprite*) arrayItem2;
            _targets->removeObject(target);
            this->removeChild(target, true);
        }

        if (targetsToDelete->count() > 0)
        {
            projectilesToDelete->addObject(projectile);
        }
        targetsToDelete->release();
    }

    CCARRAY_FOREACH(projectilesToDelete, arrayItem1)
    {
        CCSprite* projectile = (CCSprite*) arrayItem1;
        _projectiles->removeObject(projectile);
        this->removeChild(projectile, true);
    }
    projectilesToDelete->release();
}

也许这部分是一个问题:

bool HelloWorld::init() 
{
....
this->schedule( schedule_selector(HelloWorld::update) );
....
}

但我不明白为什么这部分是个问题。

拜托,帮助我!

1 个答案:

答案 0 :(得分:2)

CCTime更改为float

在旧的cocos2d-x版本中,他们有ccTime而不是CCTime

但在2.0中它们将其删除。因为它与浮动重复。