My ccTouchesMoved sometime not respond (get called) as fast as it should be (let say it’s lack). but sometime it worked ok.
I created a sprite and set its position in every ccTouchesMoved (to move it according to touch’s position)
Result: sometime the sprite moved smoothly, but sometime it’s very lackky.
“sometime” = different time that I build and run with the same code.
I have no idea of how is this happen, I also did try a very basic multiTouchTest in samples. but the same problem is there too.
my test device was ipod touch latest gen (Iong screen).
using cocos2dx-2.1.5
Does anyone had the same problem with me, and how did you cope it?
I need your help please.
Here is my code:
header
class CCPhatTouches: public cocos2d::CCLayer {
cocos2d::CCSprite* _sprite = NULL;
public:
virtual bool init();
virtual ~CCPhatTouches();
CREATE_FUNC(CCPhatTouches);
virtual void registerWithTouchDispatcher(void);
virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesCancelled(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
};
cpp
#include "CCPhatTouches.h"
USING_NS_CC;
static ccColor3B s_TouchColors[CC_MAX_TOUCHES] = {
ccYELLOW,
ccBLUE,
ccGREEN,
ccRED,
ccMAGENTA
};
class TouchPoint : public CCNode
{
public:
TouchPoint()
{
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
}
virtual void draw()
{
ccDrawColor4B(m_TouchColor.r, m_TouchColor.g, m_TouchColor.b, 255);
glLineWidth(10);
ccDrawLine( ccp(0, m_pTouchPoint.y), ccp(getContentSize().width, m_pTouchPoint.y) );
ccDrawLine( ccp(m_pTouchPoint.x, 0), ccp(m_pTouchPoint.x, getContentSize().height) );
glLineWidth(1);
ccPointSize(30);
ccDrawPoint(m_pTouchPoint);
}
void setTouchPos(const CCPoint& pt)
{
m_pTouchPoint = pt;
}
CCPoint getTouchPos() {
return m_pTouchPoint;
}
void setTouchColor(ccColor3B color)
{
m_TouchColor = color;
}
static TouchPoint* touchPointWithParent(CCNode* pParent)
{
TouchPoint* pRet = new TouchPoint();
pRet->setContentSize(pParent->getContentSize());
pRet->setAnchorPoint(ccp(0.0f, 0.0f));
pRet->autorelease();
return pRet;
}
private:
CCPoint m_pTouchPoint;
ccColor3B m_TouchColor;
};
static CCDictionary s_dic;
bool CCPhatTouches::init() {
if (!CCLayer::init()) {
return false;
}
setTouchEnabled(true);
_sprite = CCSprite::create("HelloWorld.png");
addChild(_sprite);
return true;
}
CCPhatTouches::~CCPhatTouches() {
}
void CCPhatTouches::registerWithTouchDispatcher(void) {
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
}
void CCPhatTouches::ccTouchesBegan(CCSet *touches, CCEvent *event) {
CCSetIterator it = touches->begin();
for (; it!=touches->end(); it++) {
CCTouch* touch = (CCTouch*)(*it);
CCPoint location = touch->getLocation();
TouchPoint* pTouchPoint = TouchPoint::touchPointWithParent(this);
pTouchPoint->setTouchPos(location);
pTouchPoint->setTouchColor(s_TouchColors[touch->getID()]);
addChild(pTouchPoint);
s_dic.setObject(pTouchPoint, touch->getID());
printf("began:%d %f,%f\n", touch->getID(), location.x, location.y);
}
}
void CCPhatTouches::ccTouchesMoved(CCSet *touches, CCEvent *event) {
CCSetIterator it = touches->begin();
for (; it!=touches->end(); it++) {
CCTouch* touch = (CCTouch*)(*it);
CCPoint location = touch->getLocation();
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(touch->getID());
pTP->setTouchPos(location);
printf("moved:%d %f,%f\n", touch->getID(), location.x, location.y);
}
int n = s_dic.count();
printf("---> %d\n", n);
switch (n) {
default:
case 1: {
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(0);
if (pTP) {
_sprite->setPosition(pTP->getTouchPos());
}
}break;
case 2: {
TouchPoint* pTP1 = (TouchPoint*)s_dic.objectForKey(0);
TouchPoint* pTP2 = (TouchPoint*)s_dic.objectForKey(1);
if (pTP1 && pTP2) {
CCPoint location = (pTP1->getTouchPos()+pTP2->getTouchPos())/2;
_sprite->setPosition(location);
}
}break;
}
}
void CCPhatTouches::ccTouchesEnded(CCSet *touches, CCEvent *event) {
CCSetIterator it = touches->begin();
for (; it!=touches->end(); it++) {
CCTouch* touch = (CCTouch*)(*it);
CCPoint location = touch->getLocation();
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(touch->getID());
removeChild(pTP, true);
s_dic.removeObjectForKey(touch->getID());
printf("ended:%d %f,%f\n", touch->getID(), location.x, location.y);
}
}
void CCPhatTouches::ccTouchesCancelled(CCSet *touches, CCEvent *event) {
ccTouchesEnded(touches, event);
}
Cheers,