【问题标题】:Cocos2d-x and handling touch events [duplicate]Cocos2d-x 和处理触摸事件[重复]
【发布时间】:2013-06-27 18:30:29
【问题描述】:

我在屏幕上有我的精灵,我有一个存储每个精灵的向量。

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();
}

【问题讨论】:

  • @m.ding,我已经尝试过您给出的链接中提到的解决方案,但在我的情况下它没有用。
  • @Rv15 - 你在分类什么?

标签: c++ cocos2d-iphone cocos2d-x multi-touch


【解决方案1】:

我以前回答过这个问题。

详情请到cocos2d subclassing sprite to handle touch?查询。

【讨论】:

  • 我想我已经在做这一切了。让我仔细看看你之前的回答。我确实发现,如果我什至在我的图层上捕捉到触摸并穿过我的矢量,那么我就可以让它工作。我只是想让每个精灵自己回答。
  • @you 在onEnter() 中错过了CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);。如果您没有在 TouchDispatcher 上注册精灵,您的精灵如何获得触摸?并且不要忘记在onExit()取消注册
  • @Jason 注册过程就像有人说“嗨,Jason,如果系统发生触摸事件,请传给我,不要传给其他人”。
  • 我的 onEnter() 中有你上面提到的 getTouchDispatcher() 行
  • @Jason 您能否为您的Field 类发布create() 函数的代码,以及您在哪里创建Field 对象并使用它?并确保您设置的addTargetedDelegate(this, INT_MIN, true) 优先级高于任何其他对象。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多