0

我有一个 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;

然而,当它们连在一起时,我移动鱼钩,鱼却不来。我犯了什么明显的错误吗?

非常感谢任何帮助。

4

1 回答 1

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]];

此外,我看到您正在使用大量的碰撞自定义(例如碰撞组、旋转限制等)。我建议您使用最低限度(或非最低限度)开始并使其正常工作,然后一一添加自定义以确保它一切正常。

于 2013-11-12T22:14:50.053 回答