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:
- Touch sprite and drag to move it. .
OR - Touch background and drag to move background layer. .
Problems in my code: - 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.
- 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;>end(); +*iter)
for ; iter != pTouches
{
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;>setPosition(ccpAdd(this->getPosition(), translation));
CCPoint translation = ccpSub;
this
}
}
}
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”);
}
}