创建了许多CCSprit,但当触发ccTouchBegan时,会给出最后一个
Created many CCSprits but when triggering ccTouchBegan gives the last one allways
UPDATE我改为更简单的示例
好吧,现在我真的很困惑,我简化了这个类,因为我在网上读到的建议是更好地扩展CCNode而不是CCSprite并将其保留为CCNode的成员这是一个基于Hello-cpp的非常简单的例子
问题仍然存在,当触摸我打印的任何宝石实例时,最后一个添加的宝石,为什么
我希望每个Touch都能给我一个正确的实例,让bean接触(我打印id和名称)
我使用的是cocos2d-2.1rc0-x-2.13C++,我创建了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)
{
}
宝石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个)为什么?我在这里缺了什么?
因为每个Gem
实例都扩展了CCTargetedTouchDelegate
并注册了触摸调度器,所以只会触发最高或最新添加的。
那么,正确的方法是在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);
}
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- lower_bound()返回最后一个元素
- 从控制台中删除最后打印的元素
- 获取向量C++中第一个值和最后一个值的和
- 获取用C/C++打印的最后一个字符串
- 读取最后一行代码算法 - c++ 时出现问题
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- 为什么这个程序返回最后一个单词而不是最长的单词?
- 为什么我的最后一个 ELSE 条件无法正确执行
- 如何在最后一步使用CryptDecrypt解决NTE_BAD_DATA
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- 仅显示链表的最后一个元素
- 这段代码的最后一行在做什么?
- 删除列表 c++ 中的最后 3 个元素
- 如何检测是否在缓冲绘画动画中绘制最后一帧?
- 测试迭代器是否位于列表中的最后一个
- push_back通过自行创建的对象获取最后一个元素的向量
- 斐波那契数列部分和的最后一位数字
- 创建了许多CCSprit,但当触发ccTouchBegan时,会给出最后一个