当我点击模拟器时为什么没有调用touchsended?

时间:2013-08-13 06:34:45

标签: c++ cocos2d-x

HelloWorld::~HelloWorld()
    {
        if (_targets) {
            _targets->release();
            _targets = NULL;
        }

        if (_projectiles) {
            _projectiles->release();
            _projectiles = NULL;
        }
    }

    HelloWorld::HelloWorld():_targets(NULL),_projectiles(NULL),_projectilesDestroyed(0)
    {

    }

    CCScene* HelloWorld::scene()
    {
        CCScene *scene = NULL;
        do{
            scene = CCScene::create();
            CC_BREAK_IF(!scene);

            HelloWorld *layer = HelloWorld::create();
            CC_BREAK_IF(!layer);

            scene->addChild(layer);
        } while (0);

        return scene;
    }

    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        bool bRet = false;
        do {
            CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)));

            CCMenuItemImage *pCloseItem = CCMenuItemImage::create("CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback));
            CC_BREAK_IF(! pCloseItem);

            pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
            CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
            pMenu->setPosition(CCPointZero);

            this->addChild(pMenu, 1);

            CCSize winSize = CCDirector::sharedDirector()->getWinSize();
            CCSprite *player = CCSprite::create("Player.png", CCRectMake(0, 0, 27, 40));
            player->setPosition(ccp(player->getContentSize().width/2, winSize.height/2));
            this->addChild(player);
            this->schedule(schedule_selector(HelloWorld::gameLogic), 1.0);
            this->setTouchEnabled(true);
            this->registerWithTouchDispatcher();
            _targets = new CCArray;
            _projectiles = new CCArray;

            this->schedule(schedule_selector(HelloWorld::updateGame) );
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("background-music-aac.wav", true);


            bRet = true;
        } while (0);

        return bRet;
    }

    void HelloWorld::menuCloseCallback(CCObject* pSender)
    {
        CCDirector::sharedDirector()->end();
    }

    void HelloWorld::addTarget()
    {
        CCSprite *target = CCSprite::create("Target.png", CCRectMake(0, 0, 27, 40));
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        float minY = target->getContentSize().height/2;
        float maxY = winSize.height - target->getContentSize().height/2;
        int reangeY = (int)(maxY - minY);
        int actualY = (rand() % reangeY) + (int)minY;

        target->setPosition(ccp(winSize.width + (target->getContentSize().width/2), actualY));
        this->addChild(target);

        int minDuration = (int)2.0;
        int maxDuration = (int)4.0;
        int rangeDuration = maxDuration - minDuration;

        int actualDuration = ( rand() % rangeDuration) + minDuration;

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

        target->setTag(1);
        _targets->addObject(target);
    }

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

        if (sprite ->getTag() == 1) {
            _targets->removeObject(sprite);

            GameOVerScene *gameOverScene = GameOVerScene::create();
            gameOverScene->getLayer()->getLabel()->setString("You Lose :[");
            CCDirector::sharedDirector()->replaceScene(gameOverScene);
        }
        else if (sprite->getTag() == 2)
        {
            _projectiles->removeObject(sprite);
        }
    }

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

    void HelloWorld::ccTouchesEnedd(CCSet* touches, CCEvent* event)
    {
        CCTouch *touch = (CCTouch*)(touches->anyObject());
        CCPoint location = touch->getLocationInView();

        location = CCDirector::sharedDirector()->convertToGL(location);
        CCSize winSize = CCDirector::sharedDirector()->getWinSize();
        CCSprite *projectile = CCSprite::create("Target.png", CCRectMake(0, 0, 20, 20));

        projectile->setPosition(ccp(20, winSize.height/2));

        float offX = location.x - projectile->getPosition().x;
        float offY = location.y - projectile->getPosition().y;

        if (offX <= 0) {
            return;
        }

        this->addChild(projectile);
        float realX = winSize.width + (projectile->getContentSize().width/2);
        float ratio = offY / offX;
        float realY = (realX *ratio) + projectile->getPosition().y;

        CCPoint realDest = ccp(realX, realY);

        float offRealX = realX - projectile->getPosition().x;
        float offRealY = realY - projectile->getPosition().y;
        float length = sqrtf((offRealX * offRealX) + (offRealY * offRealY));
        float velocity = 480/1;
        float realMoveDuration = length/velocity;

        projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, realDest), CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished))));
        projectile->setTag(2);
        _projectiles->addObject(projectile);

        CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("pew-pew-lei.wav");
    }

    void HelloWorld::updateGame(float dt)
    {
        CCArray *projectilesToDelegate = new CCArray;
        CCObject *arrayItem;
        CCARRAY_FOREACH(_projectiles, arrayItem){
            CCSprite *projectile = (CCSprite *)arrayItem;
            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 = new CCArray;

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

                CCRect rect = CCRect(projectileRect);
                if (rect.intersectsRect(targetRect)) {
                    targetsToDelete->addObject(target);
                }
            }

            CCARRAY_FOREACH(targetsToDelete, arrayItem){
                CCSprite *target = (CCSprite *)arrayItem;
                this->removeChild(target, true);

                _projectilesDestroyed++;
                if (_projectilesDestroyed >= 5) {
                    GameOVerScene *gameOverScene = GameOVerScene::create();
                    gameOverScene->getLayer()->getLabel()->setString("You Win!");
                    CCDirector::sharedDirector()->replaceScene(gameOverScene);
                }
            }

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

        CCARRAY_FOREACH(projectilesToDelegate, arrayItem){
            CCSprite* projectile = (CCSprite *)arrayItem;
            _projectiles->removeObject(projectile);
            this->removeChild(projectile, true);
        }

        projectilesToDelegate->release();
    }

    void HelloWorld::registerWithTouchDispatcher()
    {
        CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
    }

这些代码是射击游戏。我发现当我点击模拟器的屏幕时,touchesEnded没有调用?为什么?我是cocos2d-x的初学者。请给我一个帮助。谢谢。

0 个答案:

没有答案