0

I can't seem to get a unmovable fixed floor / wall in Sprite Kit.

I have a scene which has an SKNode as a "floor" and when the user presses the screen, a block drops from the touched position onto the "floor". That works well, but when the user adds another lets say 20 blocks or so, suddenly the floor rotates. This is the code for my Floor:

SKSpriteNode *floorNode = [[SKSpriteNode alloc] initWithColor:[SKColor clearColor] size:CGSizeMake(self.size.width*2, 10)];
floorNode.position = CGPointMake(CGRectGetMidX(self.frame), -5);
floorNode.name = @"floor";
floorNode.zPosition = 1;

floorNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:floorNode.size];
floorNode.physicsBody.affectedByGravity = NO;
floorNode.physicsBody.dynamic = NO;
floorNode.physicsBody.usesPreciseCollisionDetection = YES;

return floorNode;

Sorry i'm quite new to Sprite Kit. Does anyone know what's wrong with my code?

EDIT: I've added some images to make the problem a bit more clear. Since it happens in this screen also. Those black spheres you see are flying from left to right like a canonball. I increased the amount of spheres to show the problem occurring. This is the code:

- (SKNode *)newEditButton {
    SKLabelNode *editButtonNode = [SKLabelNode labelNodeWithFontNamed:@"chalkduster"];
    editButtonNode.text = @"EDIT CASTLE";
    editButtonNode.fontSize = 19;
    editButtonNode.fontColor = [SKColor darkGrayColor];
    editButtonNode.position = CGPointMake(CGRectGetMidX(self.frame) + 110, CGRectGetMidY(self.frame) + 30);
    editButtonNode.name = @"edit_button";
    editButtonNode.zPosition = 2;

    editButtonNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(140, 40)];
    editButtonNode.physicsBody.usesPreciseCollisionDetection = YES;
    editButtonNode.physicsBody.dynamic = NO;
    editButtonNode.physicsBody.affectedByGravity = NO;

    SKAction *hover = [SKAction sequence:@[[SKAction moveByX:0 y:6.0 duration:0.9],
                                           [SKAction moveByX:0 y:-6.0 duration:0.9]]];
    SKAction *hoverForever = [SKAction repeatActionForever:hover];
    [editButtonNode runAction:hoverForever];

    return editButtonNode;
}

- (SKNode *)newPlayButton {
    SKLabelNode *playButtonNode = [SKLabelNode labelNodeWithFontNamed:@"chalkduster"];
    playButtonNode.text = @"SLAUGHTER";
    playButtonNode.fontSize = 19;
    playButtonNode.fontColor = [SKColor darkGrayColor];
    playButtonNode.position = CGPointMake(CGRectGetMidX(self.frame) - 110, CGRectGetMidY(self.frame) + 30);
    playButtonNode.name = @"play_button";
    playButtonNode.zPosition = 2;

    playButtonNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(140, 40)];
    playButtonNode.physicsBody.usesPreciseCollisionDetection = YES;
    playButtonNode.physicsBody.dynamic = NO;
    playButtonNode.physicsBody.affectedByGravity = NO;

    SKAction *hover = [SKAction sequence:@[[SKAction moveByX:0 y:6.0 duration:0.9],
                                           [SKAction moveByX:0 y:-6.0 duration:0.9]]];
    SKAction *hoverForever = [SKAction repeatActionForever:hover];
    [playButtonNode runAction:hoverForever];

    return playButtonNode;
}

After a while 5-10 seconds, the "buttons" called "slaughter" and "edit castle" start to rotate everytime a sphere collides with it. But it doesn't seem like it has to do something with physics, because, if it rotates to the left, it's rotation direction will always be left. And if it starts rotating right, it's rotation direction will always be rightbefore rotation.after rotating started

It looks funny though! But not what i want ;-)

4

1 回答 1

0

你不知道,但你是在告诉 SpriteKit 应用从炮弹到标签、从块到地板的碰撞!

那是因为您将 SKPhysicsBody.collisionBitMask 保留为其默认值,即观察一切的碰撞。

从文档中

Discussion
When two physics bodies contact each other, a collision may occur. This body’s collision mask is compared to the other body’s category mask by performing a logical AND operation. If the result is a nonzero value, this body is affected by the collision. Each body independently chooses whether it wants to be affected by the other body. For example, you might use this to avoid collision calculations that would make negligible changes to a body’s velocity.

The default value is 0xFFFFFFFF (all bits set).

所以,做:

floorNode.physicsBody.collisionBitMask = 0
于 2015-06-05T18:02:32.690 回答