2

更新 我改为更简单的例子

好的,现在我真的很困惑我简化了这个类,因为我在网上阅读的建议是扩展 CCNode 比 CCSprite 更好,并将其保留为 CCNode 的成员,所以这里是基于 Hello cpp 的非常简单的示例。
问题是一样的,当触摸任何 Gem 实例时,我会打印最后添加的 Gem,为什么?
我希望每个 Touch 都会给我正确的 bean 触摸实例(我打印 id 和 name)

我使用 cocos2d-2.1rc0-x-2.1.3 c++ ,我有一些奇怪的东西我创建了 10 个 CCSprites。我有像这样扩展 CCSprite 和 CCTargetedTouchDelegate 的类:

宝石.cpp

#include "Gem.h"
Gem::Gem()
{

   ;

}
Gem::~Gem()
{
    ;
}

void Gem::onEnter()
{

    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    CCNode::onEnter();

}
void Gem::onExit()
{  
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->removeDelegate(this);
    CCNode::onExit();    
} 
bool Gem::ccTouchBegan(CCTouch* touch, CCEvent* event)
{       
    CCPoint touchPoint = touch->getLocation();   
    CCLOG("Gem Touched! ImageName:%s |GemId:%s  x:%f ,y:%f myspriteX:%f, myspriteY:%f nodePosX:%f nodePosY:%f",this->getImageName().c_str(),this->getGemId().c_str(),touchPoint.x,touchPoint.y,getGemSprite()->getPositionX(),getGemSprite()->getPositionY(),this->getPositionX(),this->getPositionY());
    return true;
}
void Gem::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
    CCPoint touchPoint = touch->getLocation();  
}
void Gem::ccTouchEnded(CCTouch* touch, CCEvent* event)
{

}

宝石.h

class Gem :public CCNode , public CCTargetedTouchDelegate 
{
public:
    Gem();

    virtual ~Gem();

    CC_SYNTHESIZE(std::string,imageName,ImageName)
    CC_SYNTHESIZE(std::string,gemId,GemId)
    CC_SYNTHESIZE(CCPoint,gemPos,GemPos)
    CC_SYNTHESIZE(CCSprite*,gemSprite,GemSprite)

    virtual void onEnter();
    virtual void onExit();
    virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
    virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
    virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);


};

helloWorldScene.cpp init() 方法

bool HelloWorld::init()
{
       bool bRet = false;
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

       if(!CCLayer::init())
        return false;

        CCSize m_winSize;
        CCSize visibleSize;
        CCPoint origin;
        m_winSize = CCDirector::sharedDirector()->getWinSize();
        visibleSize = CCDirector::sharedDirector()->getVisibleSize();
        origin = CCDirector::sharedDirector()->getVisibleOrigin();

        CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprites.plist","sprites.png");
        CCSpriteBatchNode* gameBatchNode  = CCSpriteBatchNode::create("sprites.png"/*,200*/);
        CCSprite *bg= CCSprite::create("grideFinal.png");
        //bg->setAnchorPoint(ccp(0,0));
        bg->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
        this->addChild(bg,1);

        Gem* gem1 = new Gem();
        gem1->retain();
        gem1->setGemId("gem1");
        gem1->setImageName("img_1");
        gem1->setGemSprite(CCSprite::createWithSpriteFrameName("gem1.png"));
        Gem* gem2 = new Gem();
        gem2->retain();
        gem2->setGemId("gem2");
        gem2->setImageName("img_2");
        gem2->setGemSprite(CCSprite::createWithSpriteFrameName("gem2.png"));

        gem1->setAnchorPoint(ccp(0,0));
        gem2->setAnchorPoint(ccp(0,0));

        gem1->setPosition(ccp(0,0));
        gem2->setPosition(ccp(gem1->getGemSprite()->getContentSize().width,40));

        gem1->getGemSprite()->setAnchorPoint(ccp(0,0));
        gem2->getGemSprite()->setAnchorPoint(ccp(0,0));
        gem1->getGemSprite()->setPosition(ccp(0,0));
        gem2->getGemSprite()->setPosition(ccp(gem1->getGemSprite()->getContentSize().width,40));
        //gameBatchNode->addChild(gem1->getGemSprite(),4,44);
        //gameBatchNode->addChild(gem2->getGemSprite(),4,45);
        this->addChild(gameBatchNode);

        bg->addChild(gem1->getGemSprite(),50);
        bg->addChild(gem2->getGemSprite(),50);
        bg->addChild(gem1,50);
        bg->addChild(gem2,50);

        bRet = true;


    return bRet;
}

除了我触摸屏幕并触发 Gem::ccTouchBegan 方法外,一切都很好。它总是给我最后一个 CCSprite(我在屏幕上有 50 个,为什么会这样?我在这里缺少什么?

4

1 回答 1

2

因为每个Gem实例都扩展CCTargetedTouchDelegate并注册了触摸调度器,所以只会触发最高或最新添加的实例。

那么正确的方法是CCTargetedTouchDelegateHelloWorld课堂上实现,当触摸发生时,检查Gem触摸点触摸的是哪个。

这是用于检查触摸是否在某个节点中的方法:

bool Gem::isTouchInside(CCTouch* pTouch)
{
    CCPoint touchLocation = pTouch->getLocation();
    CCRect rect =
    CCRectApplyAffineTransform(CCRectMake(0 ,
                                          0 ,
                                          this->getContentSize().width,
                                          this->getContentSize().height),
                               this->nodeToWorldTransform());
    return rect.containsPoint(touchLocation);
}
于 2013-08-16T08:51:00.507 回答