Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My spritekit game crashes when I am reloading the game?

In my spritekit game I am creating Stone randomly through update method. Here is my code for random creation of stone

//Create random island
-(void)createRandomStone:(NSMutableArray *)imageArray
{

    int getRandomNumberCoordinate = [self getRandomNumberBetween:(int)0 to:(int)768];
    int getRandomStoneImage = [self getRandomNumberBetween:0 to:(int)([imageArray count] - 1)];

    NSLog(@"Stone Image name = %d", getRandomStoneImage);

    SKSpriteNode *createStone = [SKSpriteNode spriteNodeWithTexture:[imageArray objectAtIndex:getRandomStoneImage]];

    if((getRandomNumberCoordinate + createStone.size.height / 2) > 768 )
        createIsland.position = CGPointMake(_myScreenSize.width + createStone.size.width, 768 - createStone.size.height / 2);
    else if((getRandomNumberCoordinate - createStone.size.height / 2) < 0 )
        createStone.position = CGPointMake(_myScreenSize.width + createStone.size.width, 0 + createIsland.size.height / 2);
    else
        createStone.position = CGPointMake(_myScreenSize.width + createStone.size.width, getRandomNumberCoordinate);
    createStone.name = @"Stone";
    createStone.zPosition = 3;
    [self addChild:createStone];

    //Apply physics on the Stone

    createStone.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: CGSizeMake(createStone.size.width - createStone.size.width / 4, createStone.size.height - createStone.size.height / 6)];
    createStone.physicsBody.categoryBitMask = CollisionTypeStone;
    createStone.physicsBody.contactTestBitMask = CollisionTypeMan;
    createStone.physicsBody.usesPreciseCollisionDetection = YES;
    createStone.physicsBody.collisionBitMask = 0;
}

Stones are moving from the coordinate 1024 to 0 and if any stone cross the 0 coordinate the the stone will remove by using the code

-(void)updateStonePosition:(NSString *)whichDirection andMoveAmount:(float)speed
{
    for (SKNode* node in self.children)
    {
    if([node.name isEqualToString:@"Stone"])
    {
        node.position = CGPointMake(node.position.x - speed, node.position.y);
        if(node.position.x < -node.frame.size.width / 2)
            [node removeFromParent];

    }  
   }
  }

Both of the above two methods are calling from the update method. And if the man hit by any 5 stone then the game will again reload. The reloading code is:

[self.view presentScene:[[MyScene alloc] initWithSize:self.size] transition:[SKTransition doorsCloseHorizontalWithDuration:0.5f]];

the game is reloaded but after few second an error message shows EXC_BAD_ACCESS(code=2, address = 0x0) on the line

SKSpriteNode *createStone = [SKSpriteNode spriteNodeWithTexture:[imageArray objectAtIndex:getRandomStoneImage]];

Please help. Thanks in advance.

like image 459
Banshi Avatar asked Nov 10 '22 15:11

Banshi


1 Answers

To me, and with the code/information you are providing, it seems that when your scene is reloaded the array you are passing to "createRandomStone:" is nil.

Try checking for nil before calling the method:

if (imageArray != nil) {
    // call createRandomStone
} else {
    NSLog(@"imageArray is nil")
}

see if that helps identifying the root cause of the error.

like image 189
Joaquin Llaneza Avatar answered Nov 15 '22 05:11

Joaquin Llaneza