我想知道如何在八个方向中的一个方向上滑动一个节点,并在该方向上移动节点。我只希望它能够按以下方向滑动:向上,向右,向右,向右,向下,向左,向左和向左上方滑动。我是sprite kit和swift的新手,所以我不确定如何做到这一点。任何帮助,将不胜感激!
所以我想拥有它,所以可以在列出的任何方向上滑动sigle精灵,然后滑动方向滑动射击距离,然后返回中心。
答案 0 :(得分:0)
这实际上是几个问题。我现在在手机上,所以我现在无法添加示例代码,但是一旦您设置了要移动的节点,就需要做一些事情。
设置节点的physicsBody属性。
添加第一个节点所在的另一个节点,并为其添加物理主体。
将它们与SKPhysicsJoint或类似的类连接起来。
就移动它们而言,您可以通过更改physicsBody.velocity属性直接设置速度。
如果您只需要在评论中向我解释的内容,那么如果正确设置关节节点,您将无需检测其滑动的方向。
编辑:以下是一些示例代码,可帮助您入门。它可能不完全是你想要的,但你应该可以从这里开始。这花了一点时间,所以我确信它应该有所帮助。
首先,使用yourSKView.showsPhysics = YES;
{
// active node will be the one you are touching now. you will see it is set in the touches methods
SKSpriteNode *_activeNode;
// this is the middle node
SKSpriteNode *_fixedNode;
}
-(void)didMoveToView:(SKView *)view {
// set up the physics world
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsWorld.gravity = CGVectorMake(0, 0);
// this node is made so you can pin something to it
_fixedNode = [SKSpriteNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(1, 1)];
_fixedNode.position = CGPointMake(self.frame.size.width /2, self.frame.size.height / 2);
_fixedNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1];
_fixedNode.physicsBody.dynamic = NO;
[self addChild:_fixedNode];
// this is the only node i made, you can make several and pin them to the same fixedNode
SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(50, 50)];
node.position = CGPointMake(self.frame.size.width /2, self.frame.size.height / 2);
[self addChild:node];
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
_activeNode = node;
//this is the skjoint i was telling you about
SKPhysicsJointLimit *rope = [SKPhysicsJointLimit jointWithBodyA:_fixedNode.physicsBody bodyB:node.physicsBody anchorA:_fixedNode.position anchorB:node.position];
// set how far you want it to go at max
rope.maxLength = 150;
[self.physicsWorld addJoint:rope];}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// make the node touched the active node
_activeNode = (SKSpriteNode*)[self nodeAtPoint:[[touches anyObject] locationInNode:self]];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
//when touches end there is no active node, clear it
_activeNode = nil;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// the first like makes it so that if there is no active node nothing will be done, and if you are clicking on the scene nothing will be done as well. just a safeguard
if (!_activeNode || [_activeNode isKindOfClass:[SKScene class]]) return;
// where is the last touch?
CGPoint touchPoint = [[touches anyObject] locationInNode:self];
// from this point on in the method, a basic trig understanding is required
// find the offset between fixedNode and moved position
CGPoint offset = CGPointMake(touchPoint.x - _activeNode.position.x, touchPoint.y - _activeNode.position.y);
// set the velocity directly (20 is an arbitrary number)
_activeNode.physicsBody.velocity = CGVectorMake(offset.x * 20, offset.y * 20);
}