2013-06-27 219 views
1

我有我的精灵在屏幕上,我有一个存储每个精灵的向量。Cocos2d-x和处理触摸事件

CCSprite *可以处理触摸事件吗?或者只是CCLayer *?

什么是决定哪个精灵被触动的最佳方法?我是否应该存储精灵所在的坐标(在精灵类中)以及何时得到事件,通过查看矢量并获取每个精灵当前坐标来查看用户触摸的位置是精灵的位置? 更新:我继承CCSprite:

class Field : public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate

,我实现的功能:

cocos2d::CCRect rect(); 

    virtual void onEnter(); 
    virtual void onExit(); 

    bool containsTouchLocation(cocos2d::CCTouch* touch); 
    virtual bool ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event); 
    virtual void ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event); 
    virtual void ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event); 

    virtual void touchDelegateRetain(); 
    virtual void touchDelegateRelease(); 

我把CCLOG声明中每一个和我不打他们!

当我触摸CCLayer时,虽然我在类中实现了图层并将这些图形放置在图层上,但这个图元仍然打开。

更新:我一直在试图代码:

Field* Field::createWithLocation(cocos2d::CCPoint p) 
{ 
    Field* f = new Field(); 
    f->autorelease(); 
    f->initWithLocation(p); 
    return f; 
} 

void Field::initWithLocation(cocos2d::CCPoint p) 
{ 
    setFieldCenterPoint(p); 
    setFieldGraphicName(FIELD::fieldIconFileName); 

    setFieldSprite(cocos2d::CCSprite::create(getFieldGraphicName().c_str())); 
    getFieldSprite()->setPosition(ccp(getFieldCenterPoint().x, getFieldCenterPoint().y)); 

    setFieldSize(getFieldSprite()->getContentSize()); 
} 

cocos2d::CCRect Field::rect() 
{ 
    cocos2d::CCSize s = getFieldSprite()->getTexture()->getContentSize(); 
    return cocos2d::CCRectMake(-s.width/2, -s.height/2, s.width, s.height); 
} 

void Field::onEnter() 
{ 
    CCLOG("In onEnter"); 
    cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector(); 
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 
    //_dir->Instance()->getDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 
    cocos2d::CCSprite::onEnter(); 
} 

void Field::onExit() 
{ 
    CCLOG("In onExit"); 
    cocos2d::CCDirector* pDirector = cocos2d::CCDirector::sharedDirector(); 
    pDirector->getTouchDispatcher()->removeDelegate(this); 
    //_dir->Instance()->getDirector()->getTouchDispatcher()->removeDelegate(this); 
    cocos2d::CCSprite::onExit(); 
} 

bool Field::containsTouchLocation(cocos2d::CCTouch* touch) 
{ 
    return rect().containsPoint(convertTouchToNodeSpaceAR(touch)); 
} 

bool Field::ccTouchBegan(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) 
{ 
    CCLOG("In ccTouchBegan"); 

    return true; 
} 

void Field::ccTouchMoved(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) 
{ 
    CCLOG("In ccTouchMoved"); 
} 

void Field::ccTouchEnded(cocos2d::CCTouch* touch, cocos2d::CCEvent* event) 
{ 
    CCLOG("In ccTouchEnded"); 
} 

void Field::touchDelegateRetain() 
{ 
    this->retain(); 
} 

void Field::touchDelegateRelease() 
{ 
    this->release(); 
} 
+0

@ m.ding,我已经尝试了您提供的链接中提到的解决方案,但在我的情况下它没有用。 –

+0

@ Rv15 - 你在分类什么? – Jason

回答

0

我以前回答过这个。

请前往cocos2d subclassing sprite to handle touch?查找详情。

+0

我想我已经在做所有这些了。让我仔细看看你之前的回答。我确实发现,如果我甚至在我的图层上捕捉到触摸并通过我的矢量运行,那么我可以使它工作。我只是想让每个精灵都自己回答。 – Jason

+1

@you missed'CCDirector :: sharedDirector() - > getTouchDispatcher() - > addTargetedDelegate(this,0,true);'in'onEnter()''。如果您没有在TouchDispatcher上注册精灵,那么您的精灵如何获得联系?并且不要忘记注销'onExit()' –

+0

@Jason注册过程就像有人说“你好,杰森,如果系统发生触摸事件,请将它传递给我,不要给任何人其他”。 –