Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

V-Appel

macrumors newbie
Original poster
Oct 1, 2013
5
0
Hi!
I´ve tested in a number of apps to try too figure out what makes this happen but with no luck...

Here´s the problem,
I have a small simple game, it has two buttons
1. One to start adding sprites that move from one side if the screen to the other
2. the other to make all sprites disappear with
Code:
[node removeFromParent]

My problem is that when I start adding sprites the second time, it adds doubles on top of each other... And the third time it adds three sprites on top of each other...
So these pictures will show you how it looks:
1. http://gyazo.com/f03c12203c19e405092b269fe05a1327

Here we have a nice amount of sprites on the screen.
If I just stop and start 3-4 times it looks like this
2. http://gyazo.com/c121d88068185559806b2ab521d6c37b


This is the code that affects my sprites.
Code:
-(id)initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        randnumb = arc4random_uniform(5) + 4;
        randnumbTrain = arc4random_uniform(0) + 1;
   
      
        [self addChild:[self gameButton]];
    }
    return self;
}
-(SKSpriteNode*) gameButton {
    SKSpriteNode* gameButton = [SKSpriteNode spriteNodeWithImageNamed:@"pig0"];
    gameButton.name = @"gameButton";
    gameButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    return gameButton;
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    if ([node.name isEqualToString:@"gameButton"] && hideMainMenuThings == NO && hideScoreBoards == YES){
        [self performSelector:@selector(gameTime)];

        hideMainMenuThings = YES;
        
    }
    else if ([node.name isEqualToString:@"exitButton"] && hideMainMenuThings == YES){
      //  hideMainMenuThings = NO;
    }
}
-(void)gameTime {

    [self addChild:[self gameCharacters]];
    [self addChild:[self exitButton]];
    [self performSelector:@selector(bomb) withObject:nil afterDelay:0];
    [self performSelector:@selector(movingObject) withObject:nil afterDelay:0];

}
-(SKSpriteNode*) gameCharacters {
    SKSpriteNode* character = [SKSpriteNode spriteNodeWithImageNamed:@"pig0"];
    character.name = @"character";
    character.xScale = 0.5;
    character.yScale = 0.5;
    character.position = CGPointMake(70, 150);
    
    return character;
}
-(void) movingObject { // Not train, this you have to jump
    SKSpriteNode* object = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
    object.name = @"object";
    object.xScale = 0.5;
    object.yScale = 0.5;
    object.position = CGPointMake(300, 130);
    float randnumber = arc4random_uniform(3) + 2;
    [self performSelector:@selector(movingObject) withObject:nil afterDelay:randnumber];
    [self addChild:object];
    
}
-(SKSpriteNode*) exitButton {
    SKSpriteNode* exitButton = [SKSpriteNode spriteNodeWithImageNamed:@"rectangle"];
    exitButton.name = @"exitButton";
    exitButton.position = CGPointMake(200, 450);
    
    return exitButton;
}
-(void) bomb {
    SKSpriteNode *bomb = [SKSpriteNode spriteNodeWithImageNamed:@"ball"];
    bomb.name = @"bomb";
    bomb.position = CGPointMake(300, 400);
    bomb.xScale = 0.5;
    bomb.yScale = 0.5;
    [self addChild:bomb];

    SKSpriteNode* bombCarr = [SKSpriteNode spriteNodeWithImageNamed:@"rectangle"];
    bombCarr.name = @"bombCarr";
    bombCarr.position = CGPointMake(300, 380);
    bombCarr.xScale = 0.5;
    bombCarr.yScale = 0.5;
    [self addChild:bombCarr];
    [self performSelector:@selector(bomb) withObject:nil afterDelay:randnumb];

}
-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
  //  SKNode* train = [self childNodeWithName:@"train"];
    
    [self enumerateChildNodesWithName:@"gameButton" usingBlock:^(SKNode *node, BOOL *stop){
        if ( hideMainMenuThings == YES){
            node.hidden = YES;
        }
        else if (hideMainMenuThings == NO){
            node.hidden = NO;
        }
     }];
    [self enumerateChildNodesWithName:@"character" usingBlock:^(SKNode *node, BOOL *stop){
        if (hideMainMenuThings == NO || hideScoreBoards == NO){
            [node removeFromParent];
        }
        else if (hideMainMenuThings == YES || hideScoreBoards == YES) {

        }
    }];
    
    [self enumerateChildNodesWithName:@"object" usingBlock:^(SKNode *node, BOOL *stop){
        if (node.position.x <= -20 || hideMainMenuThings == NO || hideScoreBoards == NO){
            [node removeFromParent];
        }
       
        else {
            node.position = CGPointMake(node.position.x - 2, node.position.y);
        }
        
    }];
    [self enumerateChildNodesWithName:@"exitButton" usingBlock:^(SKNode *node, BOOL *stop){
        if (hideMainMenuThings == NO || hideScoreBoards == NO){
            [node removeFromParent];
        }
        else {
        
        }

    }];
[self enumerateChildNodesWithName:@"bomb" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.x < 40){
            node.position = CGPointMake(node.position.x, node.position.y - 4);
            
        }
        else if (node.position.y <-20 ||  hideMainMenuThings == NO || hideScoreBoards == NO){
            [node removeFromParent];
        }
        else {
            node.position = CGPointMake(node.position.x - 3, node.position.y);
        }
        
    }];
    [self enumerateChildNodesWithName:@"bombCarr" usingBlock:^(SKNode *node, BOOL *stop) {
        if (node.position.x < -10 || hideMainMenuThings == NO || hideScoreBoards == NO){
            [node removeFromParent];
        }
        else {
            node.position = CGPointMake(node.position.x - 3, node.position.y);
        }
    }];

@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.