2016-01-13 65 views
0

我試圖在接觸點碰撞時將球粘到微調器上。但是,好像在聯繫人啓動之前調用了didBeginContact。下面的圖片顯示他們都在一起旋轉,但有一個很大的空間。didBeginContact輸入得太早

enter image description here

代碼如下:

#import "GameScene.h" 

@implementation GameScene 
@synthesize _flowIsON; 

NSString *const kFlowTypeRed = @"RED_FLOW_PARTICLE"; 
const float kRED_DELAY_BETWEEN_PARTICLE_DROP = 0.01; //delay for particle drop in seconds 

static const uint32_t kRedParticleCategory   = 0x1 << 0; 
static const uint32_t kSpinnnerCategory   = 0x1 << 1; 

NSString *const kStartBtn = @"START_BTN"; 
NSString *const kLever = @"Lever"; 

NSString *const START_BTN_TEXT = @"Start Game"; 

CFTimeInterval lastTime; 


-(void)didMoveToView:(SKView *)view { 

    _bkgNode = (SKSpriteNode *)[self.scene childNodeWithName:@"Background"]; 

    [self initializeScene]; 

} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 


    for (UITouch *touch in touches) { 
     CGPoint location = [touch locationInNode: self]; 

     SKNode *node = [self nodeAtPoint:location]; 

     if ([node.name isEqualToString:kStartBtn]) { 
      [node removeFromParent]; 

      //initalize to ON 
      _flowIsON = YES; 

      //[self initializeScene]; 
     } 
    } 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

} 

-(void)update:(CFTimeInterval)currentTime { 

    float deltaTimeInSeconds = currentTime - lastTime; 

    //NSLog(@"Time is %f and flow is %d",deltaTimeInSeconds, _flowIsON); 

    if ((deltaTimeInSeconds > kRED_DELAY_BETWEEN_PARTICLE_DROP)) { 


     //TBD 
     SKAction *rotation = [SKAction rotateByAngle: M_PI/8.0 duration:0]; 
     [_spinner runAction:rotation]; 


     //only if its been past 1 second do we set the lasttime to the current time 
     lastTime = currentTime; 

    } 



} 


- (void) initializeScene { 

    self.physicsWorld.contactDelegate = self; 


    //create ball 
    SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:@"Ball"]; 
    ball.size = CGSizeMake(50, 50); 
    ball.position = CGPointMake(320, 1050); 
    ball.zPosition = 1; 
    ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25]; 
    ball.physicsBody.restitution = 0.0; 
    ball.physicsBody.categoryBitMask = kRedParticleCategory; 
    ball.physicsBody.contactTestBitMask = kSpinnnerCategory; 
    ball.physicsBody.collisionBitMask = kSpinnnerCategory; 
    ball.name = @"Ball"; 

    NSLog(@"Ball size is %f",ball.size.width); 

    [self addChild:ball]; 



    //Create spinner 
    _spinner = [SKSpriteNode spriteNodeWithImageNamed:@"Spinner"]; 
    _spinner.size = CGSizeMake(300, 300); 
    _spinner.position = CGPointMake(320, 500); 
    _spinner.zPosition = 1; 
    _spinner.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:150]; 
    _spinner.physicsBody.affectedByGravity = NO; 
    _spinner.physicsBody.allowsRotation = YES; 
    _spinner.physicsBody.dynamic = NO; 
    _spinner.physicsBody.restitution = 0.0; 
    _spinner.physicsBody.categoryBitMask = kSpinnnerCategory; 
    _spinner.physicsBody.contactTestBitMask = kRedParticleCategory; 
    _spinner.physicsBody.collisionBitMask = kRedParticleCategory; 
    _spinner.name = @"Spinner"; 

    [self addChild:_spinner]; 


    //create pipe 

// CGPoint center = CGPointMake(400, 600) ; 
//  
// UIBezierPath *bezierPath = [UIBezierPath bezierPath]; 
// [bezierPath addArcWithCenter:center radius:400 startAngle:1.825777 endAngle:2.011118 clockwise:YES]; 
// [bezierPath addLineToPoint:center]; 
// [bezierPath closePath]; 
//  
// SKShapeNode *shapeNode = [SKShapeNode shapeNodeWithPath:bezierPath.CGPath]; 
// shapeNode.strokeColor = [UIColor whiteColor]; 
// shapeNode.fillColor = [UIColor whiteColor]; 
// [self addChild:shapeNode]; 

} 






# pragma mark -- SKPhysicsContactDelegate Methods 

- (void)didBeginContact:(SKPhysicsContact *) contact { 

    if ([contact.bodyA.node.name isEqualToString:@"Ball"] && [contact.bodyB.node.name isEqualToString:@"Spinner"]) { 

     [self connectNode1:(SKSpriteNode *)contact.bodyA.node toNode2:(SKSpriteNode *)contact.bodyB.node withContact:contact]; 

    } 

} 


- (void)didEndContact:(SKPhysicsContact *) contact { 
    //NSLog(@"didEndContact called"); 

} 

- (void) connectNode1:(SKSpriteNode *)node1 toNode2:(SKSpriteNode *)node2 withContact: (SKPhysicsContact *)contact 
{ 

    SKPhysicsJointFixed *joint = [SKPhysicsJointFixed jointWithBodyA:node1.physicsBody 
                   bodyB:node2.physicsBody 
                   anchor:node2.position]; 
    [self.physicsWorld addJoint:joint]; 
} 


@end 

如果我註釋掉確實開始接觸方法,你可以看到圖像大小正確,當他們碰撞時,它們休息,相得益彰監守。

enter image description here

怎麼來的contact.contactPoint是不一樣的,當我註釋掉didEnterContact方法都被機體的碰撞點?任何想法如何解決?

+0

我有同樣的問題。 – ricardo

回答

0

'didBeginContact'不會調用得太早..問題在於操作在場景週期的早些時候進行了評估:先是'didEvaluateActions()'然後是'didSimulatePhysics()'。所以,即使你的SKPST可能看起來是正確的,之後會有一個物理評估。我建議在使用物理引擎時不要使用動作來進行旋轉校正。也許使用約束,那些會在物理更新之後出現。

+0

請參閱:https://developer.apple.com/reference/spritekit/skscene –