CCMoveTo problem

Hello everyone.

I have some sprites (pieces) on my scene and want to move them to the initial position after dragging. But when the touch ended sprite sometimes returns to wrong position or goes even out of the scene. I can’t investigate any reasons or regularity for such behavior.

bool GameScene::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	for(int i = 0; i < _parts.size(); i++) // _parts is a vector of pieces
	{
		CCSize size = _parts[i]->getContentSize();
		CCRect rect = CCRectMake( - size.width / 2, 0, size.width, size.height);
		if(rect.containsPoint(_parts[i]->convertTouchToNodeSpaceAR(touch)))
		{
			_partNumber = i; // remember index of current sprite
			_partScale = _parts[i]->getScale(); // remember the initial scale
			_parts[i]->setScale(_squareScale);
			return true;
		}
	}
	return false;
}

void GameScene::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
        // move the sprite        
	_parts[_partNumber]->setPosition(_parts[_partNumber]->getPosition() + touch->getDelta());
}
void GameScene::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
 	_parts[_partNumber]->setScale(_partScale);
        // here I want to return sprite to its starting position
        // _partsPos is a vector with initial sprites positions
	_parts[_partNumber]->runAction(CCMoveTo::create(0.5, _partsPos[_partNumber]));
        cout << "Starting position is x = " _partsPos[_partNumner].x 
             << " y = " << _partsPos[_partNumber].y << endl;
}

cout always returns right coordinates! :;angry

I use cocos2d-x v.2.2.2

You should research Cocos2d-x coordinate systems.

I’m guessing you are missing some code like this:

// Get current and previous positions of the touches
		CCPoint curPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getLocationInView());
		CCPoint curPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getLocationInView());

		CCPoint prevPosTouch1 = CCDirector::sharedDirector()->convertToGL(touch1->getPreviousLocationInView());
		CCPoint prevPosTouch2 = CCDirector::sharedDirector()->convertToGL(touch2->getPreviousLocationInView());

I can confirm that CCMoveTo is not working reliably in Cocos2d-x version 2.2.3 on Android and Windows Phone 8 (and probably Windows RT). iOS and Win32 work fine. Like Den I also use the CCMoveTo-action to restore a sprite’s position after it’s been dragged and especially when dragging wildly the position is off in about 50% of the cases. I use absolute coordinates, so that should not be the issue. When the position is off, it’s about 20-30 pixel from the place where it should be.

I have created a workaround for this by creating a CCSequence where after the CCMoveTo-action a CCCallFunc is called to a function that executes a setPosition to ensure that the sprite is set to the exact position. In case the CCMoveTo is off, the setPosition corrects this and “snaps” to the correct coordinates.

Hi @froxul

I have solved my problem. The thing is that if you pick the already returning sprite its returning actions kind of accumulates therefore the destination position differs from the desired. Sometimes it happens so that I even can’t notice that.

If you do not need to be able to catch returning sprite, add something like this into ccTouchBegan event:

// the sprite is moving to its initial place
if(sprite->numberOfRunningActions() > 0) 
    return false;

If you do want to be able to catch already returning sprite then try something like this:

sprite->stopAllActions();
sprite->runAction(desiredAction);

Hope it helps :wink:

Hi Den. In my case, the incorrect positioning by CCMoveTo was not caused by new touch events. I’m using a status for the object (i.e. is_being_touched) and will not take new touch-events until the touchevent is ended (is_not_being_touched). Only when dragging objects wildly I got the incorrect repositioning and only on Android and Windows (WP8/Win8).

Anyway, Good luck!

:;ok