我有一个SKSpriteNode鱼和一个扩展的SKSpriteNode类钩子;当他们联系时,我正试图将它们加在一起,然后让它们一起移动。我正在使用此代码:
[fish removeAllActions];
NSLog(@"Fish anchor is is (%f,%f)", fish.anchorPoint.x, fish.anchorPoint.y);
CGPoint hp = CGPointMake(self.hook.frame.size.width/2, 0);
fish.anchorPoint = CGPointMake(1, .5);
CGPoint hookPoint = [self.scene convertPoint:hp fromNode:self.hook];
NSLog(@"Current fish position is (%f,%f)", fish.position.x, fish.position.y);
NSLog(@"Hook point is (%f,%f)", hookPoint.x, hookPoint.y);
SKAction *moveAction = [SKAction moveTo:hookPoint duration:.1];
SKAction * flip;
if (fish.direction) //if going to the right, then turn -90 degrees
flip = [SKAction rotateToAngle:M_PI_4 duration:.1 ];
else
flip = [SKAction rotateToAngle:(-M_PI_2) duration:.1 ];
//[fish runAction:moveAction];
[fish runAction:[SKAction sequence:@[moveAction, flip]]];
SKPhysicsJointFixed *joint =
[SKPhysicsJointFixed jointWithBodyA:self.hook.physicsBody
bodyB:fish.physicsBody
anchor:hookPoint];
[self.physicsWorld addJoint:joint];
NSLog(@"Joined");
我有这样设计的鱼:
self.anchorPoint = CGPointMake(1, .05);
[self setFishSize:[self getRandomSize]];
self.xScale *= .3;
self.yScale *= .3;
self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size]; // 1
self.physicsBody.collisionBitMask = 0;
self.physicsBody.contactTestBitMask = hookCategory;
self.physicsBody.categoryBitMask = fishCategory;
self.physicsBody.usesPreciseCollisionDetection = true;
和钩子这样:
_hook = [SKSpriteNode spriteNodeWithImageNamed:@"hook"];
_hook.anchorPoint = CGPointMake(.5, 0);
_hook.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:_hook.frame.size];
_hook.position = CGPointMake(100, self.frame.size.height - self.hook.size.height/2);
_hook.physicsBody.dynamic = YES;
_hook.physicsBody.categoryBitMask = hookCategory;
_hook.physicsBody.contactTestBitMask = fishCategory;
_hook.physicsBody.collisionBitMask = 0;
_hook.physicsBody.usesPreciseCollisionDetection = YES;
_hook.physicsBody.affectedByGravity = false;
_hook.physicsBody.allowsRotation = false;
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
然而当他们加入时,我移动钩子,鱼不会出现。 我正在制作任何明显的错误吗?
任何帮助非常感谢。
答案 0 :(得分:1)
你应该在一个动作块中进行联合编队, 我建议这样的事情,
SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:self.hook.physicsBody bodyB:fish.physicsBody anchor:hookPoint];
SKAction *createJoint = [SKAction runBlock:^{
[self.physicsWorld addJoint:joint];}]
// run this action when the condition is met
[fish runAction:[SKAction createJoint]];
此外,我发现您正在使用大量的碰撞自定义(例如碰撞组,旋转限制等)。我建议您使用最小(或非)启动并使其工作,然后逐个添加自定义确保一切正常。