保持两个精灵之间的恒定距离
Maintaining a constant distance between two sprites
这里有一个距离公式我运行通过更新方法来跟踪2个精灵之间的距离:
-(void)distance
{
double dx = (_spriteA.position.x - _spriteB.position.x); //(x2 - x1);
double dy = (_spriteA.position.y - _spriteB.position.y); //(y2 - y1);
dist = sqrt(dx*dx + dy*dy);
}
-(void)update:(NSTimeInterval)currentTime
{
[self distance]; //Calculate A & B distance
if (dist > 50)
{
//What to write here to keep a constant distance between A & B?
[_spriteB runAction:[SKAction moveTo:(_spriteA.position) duration:1]];
}
}
距离跟踪很好,但 if 语句有问题。具体来说,实际的动作是在精灵 B 上 运行。最终发生的是精灵 B 像溜溜球一样不停地向精灵 A 移动 - 即使精灵 A 没有移动。我需要精灵 B 仅在精灵 A 移动时移动并保持 50 的距离。精灵 A 仅在人触摸屏幕时移动。请帮忙。
您可以使用SKConstraint
class来保持两个节点之间的距离。
例如:
let node1 = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(20, 10))
node1.position = CGPointMake(self.size.width/2, self.size.height/2)
self.addChild(node1)
let node2 = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(10, 20))
node2.position = CGPointMake(self.size.width/2, self.size.height/2 - 50)
self.addChild(node2)
// The upper and lower limit is set to 50 to maintain a constant distance.
let constraint = SKConstraint.distance(SKRange(lowerLimit: 50, upperLimit: 50), toNode : node1)
node2.constraints = [constraint]
node1.runAction(SKAction.moveToY(100, duration: 2.0))
在Objective C
SKSpriteNode *node1 = [[SKSpriteNode alloc] initWithColor:[UIColor redColor]
size:CGSizeMake(20, 10)];
node1.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:node1];
SKSpriteNode *node2 = [[SKSpriteNode alloc] initWithColor:[UIColor redColor]
size:CGSizeMake(10, 20)];
node2.position = CGPointMake(self.size.width/2, self.size.height/2 - 50);
[self addChild:node2];
// The upper and lower limit is set to 50 to maintain a constant distance.
SKConstraint *constraint = [SKConstraint distance:[SKRange rangeWithLowerLimit:50 upperLimit:50] toNode:node1];
node2.constraints = @[constraint];
[node1 runAction:[SKAction moveToY:100 duration:2.0]];
这里有一个距离公式我运行通过更新方法来跟踪2个精灵之间的距离:
-(void)distance
{
double dx = (_spriteA.position.x - _spriteB.position.x); //(x2 - x1);
double dy = (_spriteA.position.y - _spriteB.position.y); //(y2 - y1);
dist = sqrt(dx*dx + dy*dy);
}
-(void)update:(NSTimeInterval)currentTime
{
[self distance]; //Calculate A & B distance
if (dist > 50)
{
//What to write here to keep a constant distance between A & B?
[_spriteB runAction:[SKAction moveTo:(_spriteA.position) duration:1]];
}
}
距离跟踪很好,但 if 语句有问题。具体来说,实际的动作是在精灵 B 上 运行。最终发生的是精灵 B 像溜溜球一样不停地向精灵 A 移动 - 即使精灵 A 没有移动。我需要精灵 B 仅在精灵 A 移动时移动并保持 50 的距离。精灵 A 仅在人触摸屏幕时移动。请帮忙。
您可以使用SKConstraint
class来保持两个节点之间的距离。
例如:
let node1 = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake(20, 10))
node1.position = CGPointMake(self.size.width/2, self.size.height/2)
self.addChild(node1)
let node2 = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(10, 20))
node2.position = CGPointMake(self.size.width/2, self.size.height/2 - 50)
self.addChild(node2)
// The upper and lower limit is set to 50 to maintain a constant distance.
let constraint = SKConstraint.distance(SKRange(lowerLimit: 50, upperLimit: 50), toNode : node1)
node2.constraints = [constraint]
node1.runAction(SKAction.moveToY(100, duration: 2.0))
在Objective C
SKSpriteNode *node1 = [[SKSpriteNode alloc] initWithColor:[UIColor redColor]
size:CGSizeMake(20, 10)];
node1.position = CGPointMake(self.size.width/2, self.size.height/2);
[self addChild:node1];
SKSpriteNode *node2 = [[SKSpriteNode alloc] initWithColor:[UIColor redColor]
size:CGSizeMake(10, 20)];
node2.position = CGPointMake(self.size.width/2, self.size.height/2 - 50);
[self addChild:node2];
// The upper and lower limit is set to 50 to maintain a constant distance.
SKConstraint *constraint = [SKConstraint distance:[SKRange rangeWithLowerLimit:50 upperLimit:50] toNode:node1];
node2.constraints = @[constraint];
[node1 runAction:[SKAction moveToY:100 duration:2.0]];