Cocos2d-x和处理触摸事件
Cocos2d-x and handling touch events
我在屏幕上有我的精灵,我有一个存储每个精灵的向量。
CCSprite*能处理触摸事件吗?还是仅仅是CCLayer*?
决定哪个精灵被触摸的最佳方法是什么?我应该存储精灵所在位置的坐标(在精灵类中)吗?当我获得事件时,通过查看向量并获得每个精灵的当前坐标,看看用户触摸的位置是否就是精灵所在的位置?UPDATE:I子类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时,这个精灵是打开的,尽管我确实击中了实现Layer的类中的那些精灵,并将这些精灵放在层上。
更新:我一直在尝试的代码:
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();
}
我以前已经回答过这个问题。
请转到cocos2d子类化精灵来处理触摸?以查找详细信息。
相关文章:
- Android NDK传感器向事件队列报告奇怪的间隔
- 从文本文件中读取时钟时间和事件时间并进行处理
- WMI检测进程创建事件-c++
- EvtExportLogneneneba API正在将远程计算机的事件日志保存到远程PC本身.如何将其保存到主机
- 在 Android 上触摸事件 SDL2
- (如何)我可以在Windows上模拟触摸事件吗?
- QT5未注册触摸事件
- 如何重新校准Qt应用程序的触摸事件
- 如何用SDL1.2.15实现触摸事件
- cocos2d-x-3.0alpha1触摸事件
- cocos2d-x添加触摸事件回调错误
- 如何在 Windows 8 上捕获触摸键盘加载事件
- 如何在blackberry10中处理自定义控件的触摸事件
- 在QT中捕捉"Tap and hold" QPushButton上的触摸事件时出现问题
- 是否可以在Qt3.0中发出跨进程信号或发布触摸/鼠标点击事件
- Cocos2d-x和处理触摸事件
- 注入触摸事件
- Cocos2d-x触摸事件坐标系
- 如何使Qwt接受触摸和多点触摸事件
- 如何在 qt 4.6.3 中取消触摸事件