我已经成功地使用以下代码使CCNode可拖放:
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInNode:self];
// start catapult dragging when a touch inside of the catapult arm occurs
if (CGRectContainsPoint([_heroContainer boundingBox], touchLocation))
{
NSLog(@"YUM YUM");
}
_foodNode.position = touchLocation;
}但是,如果我触摸并拖动屏幕上的任何位置,它就会移动CCNode。只有当它被触碰时,我才能使它被拖走?
发布于 2014-06-16 18:34:27
我假设您的touchMoved:方法就在场景本身中,这就是为什么您是独立于触摸屏幕的位置获得通知的(这是正确的)。
若要仅在触摸节点时移动节点,请尝试在touchBegan:中签入触摸您的sprite,并设置一个标志变量,以便稍后在touchMoved:中签入
在touchBegan:中是这样的
if (CGRectContainsPoint(_foodNode.boundingBox, touchLocation))
_shouldDragMyNode = YES;
else
_shouldDragMyNode = NO;然后在touchMoved中:只有当_shouldDragMyNode是YES时才更改位置
if (_shouldDragMyNode)
_foodNode.position = touchLocation;抱歉,现在不能测试代码。但是像这样的东西应该能起作用。
https://stackoverflow.com/questions/24248155
复制相似问题