【问题标题】:Sprite Kit: How to program a one directional rotating leverSprite Kit:如何对单向旋转杆进行编程
【发布时间】:2026-01-30 10:40:02
【问题描述】:

我可以使用精灵套件来制作跷跷板。当球击中一侧时,水平仪将旋转。但是,我只需要使杠杆顺时针转动。当它被球击中逆时针旋转时,它会变硬。

如何做到这一点?

谢谢!

  -(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

【问题讨论】:

  • 你能提供任何代码吗?

标签: ios objective-c collision-detection sprite-kit


【解决方案1】:

你可以试试这样的:

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

    [super didSimulatePhysics];

    ...

【讨论】:

  • 它仍然会移动一点然后停止。我尝试在 DidBeginContact 搬家,但没有帮助。