游戏大幅增加节点数并冻结

时间:2014-11-25 02:43:48

标签: ios objective-c sprite-kit

我创建了两个节点,一个射弹节点和一个目标节点,每个节点都有各自的类别,当它们彼此接触时,两者都被移除,并且根据射弹的不同,会出现更多的射弹。

当弹丸接触到屏幕边缘时,它们也会被移除。

一切正常,直到" wave"目标(我创建了一种偶尔在设计中产生5个目标的方法)出现并在屏幕上滚动,并且射弹遍布整个地方接触越来越多的目标,现在我会理解节点数增加,但数量是否定的在接近金额的地方,游戏然后落到FPS为2 FPS的点,游戏最终冻结,节点数量达到700.

didBeginContact Method

static inline Enemies *nodeFromBody(SKPhysicsBody *body1, SKPhysicsBody *body2, uint32_t category) {
Enemies *node = nil;
if (body1.categoryBitMask & category) {
    node = (Enemies *)body1.node;
}
else if (body2.categoryBitMask & category) {
    node = (Enemies *)body2.node;
}
return node;
}

-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;

SKSpriteNode *projectile = nil;
SKSpriteNode *offScreen = nil;
Enemies *target = nil;

firstBody = contact.bodyA;
secondBody = contact.bodyB;

projectile = nodeFromBody(firstBody, secondBody, projectileCategory);
offScreen = nodeFromBody(firstBody, secondBody, offScreenCategory);
target = nodeFromBody(firstBody, secondBody, targetsCategory);

if (projectile && target) {

    if ([projectile.name isEqualToString:@"firstShot"]) {
        NSLog(@"firstShot");
        [projectile removeAllActions];
        [projectile removeFromParent];
        [ProjectilePatterns pattern1:self spawnPoint:target.position];
        [target removeAllActions];
        [target removeFromParent];
        [Enemies wave1:self spawnPoint:CGPointMake(CGRectGetMidX(self.frame) - 525, CGRectGetMidY(self.frame) - 50)];
    }
}

enemyClassMethod

// These are the regular targets, the other method that spawns the wave is essentially creating new enemies and returns them positioned in certain positions.

+(Enemies *)wave1:(SKScene *)scene spawnPoint:(CGPoint)location {

SKAction *delay = [SKAction waitForDuration:5];
SKAction *remove = [SKAction removeFromParent];
SKAction *delayAndRemove = [SKAction sequence:@[delay, remove]];

Enemies *enemy1 = [[Enemies alloc] init];
enemy1 = [Enemies spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(30, 30)];
enemy1.position = location;
enemy1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy1.size];
enemy1.physicsBody.categoryBitMask = targetsCategory;
enemy1.physicsBody.contactTestBitMask = projectileCategory;
enemy1.physicsBody.collisionBitMask = 0;

[scene addChild:enemy1];
[enemy1 runAction:delayAndRemove];
[enemy1.physicsBody applyImpulse:CGVectorMake(12, 0)];

return enemy1;
}

弹丸模式

+(ProjectilePatterns *)pattern1:(SKScene *)scene spawnPoint:(CGPoint)location {

SKAction *rotate = [SKAction rotateByAngle:1 duration:0.1];
SKAction *rotateForever = [SKAction repeatActionForever:rotate];

ProjectilePatterns *patternBomb = [[ProjectilePatterns alloc] init];
patternBomb = [ProjectilePatterns spriteNodeWithImageNamed:@"contactBomb"];
patternBomb.position = CGPointMake(location.x + 10, location.y);
patternBomb.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:patternBomb.size];
patternBomb.physicsBody.categoryBitMask = projectileCategory;
patternBomb.physicsBody.contactTestBitMask = targetsCategory | offScreenCategory;
patternBomb.physicsBody.collisionBitMask = 0;
patternBomb.physicsBody.friction = 0;
patternBomb.physicsBody.linearDamping = 0;
patternBomb.name = @"p1";

ProjectilePatterns *patternBomb2 = [[ProjectilePatterns alloc] init];
patternBomb2 = [ProjectilePatterns spriteNodeWithImageNamed:@"contactBomb"];
patternBomb2.position = CGPointMake(location.x - 10, location.y);
patternBomb2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:patternBomb2.size];
patternBomb2.physicsBody.categoryBitMask = projectileCategory;
patternBomb2.physicsBody.contactTestBitMask = targetsCategory | offScreenCategory;
patternBomb2.physicsBody.collisionBitMask = 0;
patternBomb2.physicsBody.friction = 0;
patternBomb2.physicsBody.linearDamping = 0;
patternBomb2.name = @"p1";

[scene addChild:patternBomb];
[patternBomb runAction:rotateForever];
[patternBomb.physicsBody applyImpulse:CGVectorMake(5, 0)];

[scene addChild:patternBomb2];
[patternBomb2 runAction:rotateForever];
[patternBomb2.physicsBody applyImpulse:CGVectorMake(-5, 0)];

return patternBomb;
return patternBomb2;
}

我还应该注意到场景的物理世界引力是(0,0)。我做到了这一点,因此冲动运动将是不变的。

0 个答案:

没有答案