2014-01-23 18 views
-2

我可以使用sprite套件製作鋸片。當球擊中一側時,水平線會旋轉。但是,我需要使槓桿只能順時針轉動。當它被球擊中逆時針旋轉時,它會變硬。Sprite套件:如何編程單向旋轉杆

這怎麼辦?

謝謝!

-(instancetype) initWithRect: (CGRect) rect : (float) percent : (float) tilt 
    { 
     if ( self = [super init]) 
     { 
     self.physicsBody.categoryBitMask = CNPhysicsCategoryLever; 
     self.physicsBody.collisionBitMask = CNPhysicsCategoryBall; 
      [self addPlank:rect :percent :tilt]; 
     pivot = [SeeSaw spriteNodeWithColor:[UIColor greenColor] size:CGSizeMake(50,50)]; 

     pivot.position = CGPointZero; 

     pivot.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: pivot.size]; 

     pivot.physicsBody.dynamic = NO; 
     [self addChild:pivot]; 

     [self attachDebugRectWithSize:pivot.size]; 

    } 

    return self; 
    } 

    - (void) setNewColor 
    { 

     self.color = [UIColor grayColor]; 

    } 

    - (void) addPlank: (CGRect) rect : (float) percent : (float) tilt 
    { 

     plank = [SKSpriteNode spriteNodeWithColor:[UIColor yellowColor]  
    size:CGSizeMake(rect.size.width, rect.size.height)]; 

    // self.position = CGPointMake(rect.origin.x, rect.origin.y); 
    plank.position = CGPointZero; 
    plank.size = CGSizeMake(rect.size.width, rect.size.height); 
    plank.name = @"plank"; 
    plank.physicsBody = 
    [SKPhysicsBody bodyWithRectangleOfSize:plank.size]; 
    plank.physicsBody.friction = 0.2; 
    plank.physicsBody.categoryBitMask = CNPhysicsCategoryLever; 
    plank.physicsBody.collisionBitMask = CNPhysicsCategoryBall; 
    [plank attachDebugRectWithSize:plank.size]; 
    [self addChild:plank]; 



    } 

    - (void) addJoint: (MyScene*) scene 
    { 
    pin = [SKPhysicsJointPin jointWithBodyA:pivot.physicsBody 
                 bodyB:plank.physicsBody 
                 anchor:pivot.position]; 

     // pin.frictionTorque = 1; 

    [scene.physicsWorld addJoint: pin]; 



    scene.physicsWorld.contactDelegate = self; 
} 

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


} 

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

} 

@end 
+0

能否請您提供任何代碼? –

回答

0

你可以嘗試這樣的事:

-(void)didSimulatePhysics 
{ 
    if (plank.physicsBody.angularVelocity > 0) { 
     plank.physicsBody.angularVelocity = 0; 
    } 

    [super didSimulatePhysics]; 

    ... 
+0

它仍然會移動一點,然後停下來。我嘗試移動到DidBeginContact並沒有幫助。 –