Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 创建了许多CCSPRIT,但当触发CCTouchStart时,总是给出最后一个CCSPRIT_C++_Cocos2d X - Fatal编程技术网

C++ 创建了许多CCSPRIT,但当触发CCTouchStart时,总是给出最后一个CCSPRIT

C++ 创建了许多CCSPRIT,但当触发CCTouchStart时,总是给出最后一个CCSPRIT,c++,cocos2d-x,C++,Cocos2d X,更新我改为更简单的示例 好的,现在我真的很困惑,我简化了这个类,因为当我在网上阅读时,建议将CCNode扩展得比CCSprite更好 并将其保留为CCNode的成员 下面是一个基于Hello cpp的非常简单的示例。 问题是同样的,当触摸任何一个Gem实例时,我会打印最后添加的Gem,为什么?? 我希望每个touch都会给我一个正确的实例(我打印id和name) 使用COCOS2D2.2.1RC0X-2.1.3C++,我有一些奇怪的东西,我创建了10个CcSpRITE。 我有一个类扩展了CCSp

更新我改为更简单的示例

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

使用COCOS2D2.2.1RC0X-2.1.3C++,我有一些奇怪的东西,我创建了10个CcSpRITE。 我有一个类扩展了CCSprite和CCTargetedTouchDelegate,如下所示:

Gem.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)
{

}
Gem.h

helloWorldScene.cpp init()方法

除了触摸屏幕并触发Gem::cctouchBegined方法外,一切正常。它总是给我最后一个CCSprite(我在屏幕上有50个)
这是为什么?我这里缺少什么?

因为每个
Gem
实例扩展
CCTargetedTouchDelegate
和register touch dispatcher,只会触发最高或最新添加的

因此,正确的方法是在
HelloWorld
类中实现
CCTargetedTouchDelegate
,当触摸发生时,检查触摸点触摸了哪个
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);
}
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;
}
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);
}