暂停按钮在更改时添加新节点

时间:2014-04-28 19:44:24

标签: ios objective-c sprite-kit

我在sprite kit游戏中实现了一个暂停和播放按钮。问题是每次按下按钮似乎都会添加一个新节点。这将是非常无效的。我想要的是取代现有的。我怎样才能做到这一点?

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

    pauseButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [pauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [pauseButton addTarget:self action:@selector(pausedMenu:) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:pauseButton];

}




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

    menuBackground = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:0.0 alpha:0.2] size:CGSizeMake(self.frame.size.width*2, self.frame.size.height*2)];

    [self addChild:menuBackground];


    [pauseButton setImage:[UIImage imageNamed:@"start"] forState:UIControlStateNormal];
    [pauseButton addTarget:self action:@selector(startMenu:) forControlEvents:UIControlEventTouchUpInside];

    self.scene.paused = YES;

}

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

    [menuBackground removeAllChildren];
    [pauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [pauseButton addTarget:self action:@selector(pausedMenu:) forControlEvents:UIControlEventTouchUpInside];
    self.scene.paused = NO;

}

1 个答案:

答案 0 :(得分:1)

你的意思是

[menuBackground removeFromParent];//add this
menuBackground = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:0.0 alpha:0.2] size:CGSizeMake(self.frame.size.width*2, self.frame.size.height*2)];
[self addChild:menuBackground];

[pauseButton removeFromParent];//add this
pauseButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [pauseButton setImage:[UIImage imageNamed:@"pause"] forState:UIControlStateNormal];
    [pauseButton addTarget:self action:@selector(pausedMenu:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:pauseButton];