gpt4 book ai didi

ios - 用两根手指移动两个不同的对象 SpriteKit

转载 作者:行者123 更新时间:2023-12-01 16:42:49 25 4
gpt4 key购买 nike

我想用两个手指移动两个不同的对象。问题是我无法使用 touchBegan 方法,因为我无法识别我当前操作的对象。最好的方法是使用手势识别器,因为我可以识别我当前拖动的 View ,我可以移动两个或多个不同的对象并且我总是引用它们。但是我不能在 SpriteKit 中使用手势识别器。我不能向我的对象添加手势,但我可以向主 SKView 添加手势,但它只能与一个触摸对象一起使用。那么,问题是如何使用 SpriteKit 移动两个不同的对象?

最佳答案

触摸...方法肯定可以用于此。例如,要平移多个 Sprite ,首先迭代触摸。对于每一个,向场景询问该点的所有节点。 (这是节点,您可以获得对对象的引用)

然后迭代该点的所有节点。如果它们通过了某种可移动性测试(让我们说名称),则使用算术根据 UITouch 对象的当前和以前的触摸位置确定它们的新位置。话虽如此,下面的代码将移动场景中名为“nameOfMovableObject”的任何 Sprite 以及该位置的触摸。

CG_INLINE CGPoint CGPointFromPreviousCoords(CGPoint affectedPoint, CGPoint currentPoint, CGPoint previousPoint) {
return CGPointMake(affectedPoint.x + currentPoint.x - previousPoint.x, affectedPoint.y + currentPoint.y - previousPoint.y);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];

for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
CGPoint previousLocation = [touch previousLocationInNode:self];

for (SKNode *node in [self nodesAtPoint:location]) {
if ([node.name isEqualToString:@"nameOfMovableObject"]) {
[node setPosition:CGPointFromPreviousCoords(node.position, location, previousLocation)];
}
}
}
}

关于ios - 用两根手指移动两个不同的对象 SpriteKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com