SpriteKit物理体在击中/敲击后返回原始垂直位置

时间:2014-08-22 07:26:58

标签: ios7 sprite-kit physics game-physics skphysicsbody

我正在尝试制作一个物理机制,其中垂直站立的物体可以被撞击或击倒,然后将枢转回到它的原点位置。可以把它想象成一个落地式打孔袋。因此,对象将具有较低的枢轴/锚点。

我只想在如何使用SpriteKit物理学方法中找到一点理论指导。

任何帮助都会非常感激。

由于

1 个答案:

答案 0 :(得分:1)

以下通过连接两个实体创建一个复合对象:圆和重量。重量相对于圆的中心偏移并且更密集。当添加到场景时,重力旋转组合对象,因此具有重量的一侧在底部。要使用它1)创建一个新的sprite kit游戏,2)用这个代码替换默认的initWithSize和touchesBegan方法,3)运行并点击场景中的不同位置。

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

    }
    return self;
}

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

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        SKShapeNode *circle = [SKShapeNode node];
        circle.path =[UIBezierPath bezierPathWithOvalInRect: CGRectMake(-32, -32, 64, 64)].CGPath;
        circle.position = location;
        circle.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:32];

        SKSpriteNode *weight = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(8, 8)];
        // Adjust this to get the desire effect
        weight.position = CGPointMake(location.x+1, location.y+28);
        weight.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:4];
        // Adjust this to get the desired effect
        weight.physicsBody.density = 100.0;

        // The physics bodies must be in the scene before adding the joint
        [self addChild:circle];
        [self addChild:weight];

        // Join the circle and the weight with a physics joint
        SKPhysicsJoint *joint = [SKPhysicsJointFixed jointWithBodyA:circle.physicsBody bodyB:weight.physicsBody anchor:weight.position];
        [self.physicsWorld addJoint:joint];
    }
}