【发布时间】:2015-03-25 15:03:38
【问题描述】:
我正在查看this drag and drop tutorial by Ray Wenderleich,它允许在屏幕上拖动精灵,但是没有重力,一旦用户抬起手指,精灵就会被固定在原位。
同时我是looking at this SpriteKit collision demo by Apple,对象之间有碰撞。
然后是this tutorial which shows how to apply impulse to sprites.
我已经把demo组合在一起了,所以我的场景有重力、边缘碰撞和弹跳,但是拖放代码仍然没有惯性。
如何将精灵的拖放行为修改为“拖放”?我正在寻找类似于滚动视图中的视图的行为 - 加速、减速和惯性。 p>
下面的代码有两种模式 - 将脉冲施加到精灵上的轻弹和改变精灵位置的拖动。我不确定如何将两个代码路径组合到
- (void)handlePanFrom:(UIPanGestureRecognizer *)recognizer {
CGPoint touchLocation = [recognizer locationInView:recognizer.view];
touchLocation = [self convertPointFromView:touchLocation];
if (recognizer.state == UIGestureRecognizerStateBegan) {
[self selectNodeForTouch:touchLocation];
} else if (recognizer.state == UIGestureRecognizerStateChanged) {
SKNode* node = [self selectNodeForTouch:touchLocation];
//this is the flick gesture - pings the
double angle = atan2(touchLocation.y-node.position.y,touchLocation.x-node.position.x);
[node.physicsBody applyImpulse:CGVectorMake(20*cos(angle), 20*sin(angle))];
//CGPoint translation = [recognizer translationInView:recognizer.view];
// translation = CGPointMake(translation.x, -translation.y);
// [self panForTranslation:translation];
// [recognizer setTranslation:CGPointZero inView:recognizer.view];
} else if (recognizer.state == UIGestureRecognizerStateEnded) {
}
}
这是拖放代码:
- (void)panForTranslation:(CGPoint)translation {
CGPoint position = [_selectedNode position];
if([[_selectedNode name] isEqualToString:kAnimalNodeName]) {
[_selectedNode setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)];
} else {
CGPoint newPos = CGPointMake(position.x + translation.x, position.y + translation.y);
[_background setPosition:[self boundLayerPos:newPos]];
}
}
【问题讨论】:
-
一种选择是存储拖动节点的当前位置和最后位置。根据 x 和 y 的变化,您将知道节点被拖动的速度,并且可以在 touchesEnded 后相应地应用脉冲强度。
-
您好,不确定这是否是您要查找的内容,但请在此处查看我的答案stackoverflow.com/q/28245653/2158465
标签: ios sprite-kit drag-and-drop physics