Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly remove node when out of screen bounds?

I'm working on a sprite-kit game where nodes spawn below the lowest point on the screen and gravity is set to have them float to the top of the screen. Everything works perfectly, but it quickly starts to slow down the FPS, and eventually lags and glitches becoming very slowly. I thought the way to solve this would have been to remove the nodes from the parent after they'd past a point, this was the code I used in the update:

-(void)update:(CFTimeInterval)currentTime {

    if (_bubble1.position.y > CGRectGetMaxX(self.frame)+40) {
        [self removeFromParent];
    }

}

And in case it is needed, this is how I spawned said bubble below the initWithSize method:

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

    [self performSelector:@selector(spawnBubbles) withObject:nil afterDelay:1.0];
    [self performSelector:@selector(spawnBubbles1) withObject:nil afterDelay:1.5];

}

-(void)spawnBubbles {
    randomPosition = arc4random() %260*DoubleIfIpad;
    randomPosition = randomPosition + 20*DoubleIfIpad;

    randomNumber = arc4random() %7;
    randomNumber = randomNumber + 1;

    myColorArray = [[NSArray alloc] initWithObjects:colorCombo1, colorCombo2, colorCombo3, colorCombo4, colorCombo5, colorCombo6, colorCombo7, colorCombo8, nil];
    myRandomColor = [myColorArray objectAtIndex:randomNumber];

    _bubble1 = [SKShapeNode node];
    [_bubble1 setPath:CGPathCreateWithEllipseInRect(CGRectMake(-25*DoubleIfIpad, -25*DoubleIfIpad, 50*DoubleIfIpad, 50*DoubleIfIpad), nil)];
    _bubble1.strokeColor = _bubble1.fillColor = myRandomColor;
    _bubble1.position = CGPointMake(randomPosition, CGRectGetMinY(self.frame)-60);
    _bubble1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:20];
    _bubble1.physicsBody.categoryBitMask = CollisionBubble;

    [self addChild:_bubble1];

    [self runAction:[SKAction sequence:@[
                                         [SKAction waitForDuration:1.0],
                                         [SKAction performSelector:@selector(spawnBubbles) onTarget:self],
                                         ]]];

}

How can I make it so that the nodes are properly disposed of when they leave the screen? And how can I keep the FPS at a constant rate of 60 FPS?

Thanks in advance!!

like image 275
user3576196 Avatar asked Jun 11 '14 18:06

user3576196


1 Answers

I would recommend using the built in contact detection in spritekit. Create a skspritenode mimicking a roof that has a physics body set to detect contact with bubbles. Create an event on contact between the roof node and bubble node that will simply remove the bubbles. This will ensure that bubbles are removed and you maintain a constant FPS.

Example of event called on contact:

- (void)bubble:(SKSpritenode*)bubble didCollideWithRoof:(SKSpriteNode*)roof{
[bubble removeFromParent];}

Contact detection example:

- (void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
    firstBody = contact.bodyA;
    secondBody = contact.bodyB;
}
else
{
    firstBody = contact.bodyB;
    secondBody = contact.bodyA;
}

if (firstBody.categoryBitMask==bubbleCategory && secondBody.categoryBitMask == roofCategory)
{
    [self bubble:(SKSpriteNode*)firstBody.node didCollideWithRoof:(SKSpriteNode*)secondBody.node];
}}

Bubble needs:

 _bubble.physicsBody.contactTestBitMask = roofCategory;

Roof:

SKSpriteNode *roof = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(self.scene.size.width, 1)];   
roof.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(self.scene.size.width, 1)];

    roof.position = CGPointMake(self.scene.size.width/2,self.scene.size.height)
    roof.physicsBody.dynamic = NO;
    roof.physicsBody.categoryBitMask = floorCategory;
    roof.physicsBody.contactTestBitMask = bubbleCategory;
    [self addChild:roof];
like image 193
meisenman Avatar answered Oct 20 '22 10:10

meisenman