Using touch to move sprite OR background layer separately.

Hi,

I’m making a game using C*+ and I need to be able to touch a sprite and drag it to move as well as the layer. I’m more or less using this tutorial http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
What I need:

  1. Touch sprite and drag to move it. .
    OR
  2. Touch background and drag to move background layer. .
    Problems in my code:
  3. Sprite and Background can be moved separately, however, sometimes after touching and moving the background layer, the sprite can no longer be touched. When I try to click the sprite, the background layer is clicked instead.
  4. Sometimes, when moving the sprite, the background layer will also move along. .

I’ve read about Standard Touch Delegate and Targeted Touch Delegate. Targeted Touch Delegate would solve my problem easily, however, I need to use multi-touch for zooming later on so I need to use Standard Touch Delegate.
Here’s what I’ve tried doing so far. Naively, I tried setting a variable to prevent one touch from being handled if the other item is being touched.
<pre>
void Isometric::ccTouchesBegan
{
CCTouch* pTouch = );
CCPoint location = pTouch~~>getLocationInView;
location.y = ) + CCDirector::sharedDirector~~>getWinSize.height;
CCRect* pTextureRect = new CCRect, pBuildingTest~~>getPositionY, pBuildingTest~~>getContentSize.width, pBuildingTest~~>getContentSize.height);
for ; iter != pTouches~~>end;*iter)
{
if )
{
CCLog;
Isometric::setTouched;
}
else if )
{
CCLog;
Isometric::setTouched;
}
}
}
void Isometric::ccTouchesMoved
{
CCRect* pTextureRect = new CCRect, pBuildingTest~~>getPositionY, pBuildingTest~~>getContentSize.width, pBuildingTest~~>getContentSize.height);
CCTouch* pTouch = );
CCPoint location = pTouch~~>getLocationInView;
location.y = )
CCDirector::sharedDirector()>getWinSize.height;
for ; iter != pTouches
>end(); +*iter)
{
if && m_spriteTouched true)
{
CCPoint oldTouchLocation = pTouch->getPreviousLocationInView();
oldTouchLocation.y = (oldTouchLocation.y * (-1)) + CCDirector::sharedDirector()->getWinSize().height;
CCPoint translation = ccpSub(location, oldTouchLocation);
//translation.y = translation.y * (-1);
CCPoint newPosition = ccpAdd(pBuildingTest->getPosition(), translation);
pBuildingTest->setPosition(newPosition);
}

    else if (!pTextureRect-\>containsPoint(location) && m\_spriteTouched  false)

{
CCPoint oldTouchLocation = pTouch->getPreviousLocationInView;
oldTouchLocation.y = )* CCDirector::sharedDirector()>getWinSize.height;
CCPoint translation = ccpSub;
this
>setPosition(ccpAdd(this->getPosition(), translation));
}
}
}

void Isometric::ccTouchesEnded(CCSet* pTouches, CCEvent* event)
{
if (m_spriteTouched true)
{
Isometric::setTouched(m_spriteTouched, 1);
}

else if (m\_spriteTouched  false)

{
Isometric::setTouched(m_spriteTouched, 0);
}
}

void Isometric::setTouched(bool &spriteTouched, int condition)
{
if (condition 0)
{
spriteTouched = true;
CCLog(“Set to True”);
}

else if (condition  1)

{
spriteTouched = false;
CCLog(“Set to False”);
}
}

So does anyone have any insight as to what may be wrong? Also, is it possible to set Targeted Touch for the sprite and Standard Touch for the background?

Looks like you need to convert the touch location.

Something like touchPos = layer->convertTouchToNodeSpace(touch);

You cant touch the sprite because the layer movement has changed its position and has not been adjusted.

Oh wow thanks.

The tutorial didn’t cover the different coordinate systems. After reading your post, I read this one http://www.cocos2d-x.org/boards/6/topics/7991 and it really helped me understand my problem. Again, thanks for your help.