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

标签 ios objective-c sprite-kit

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

-(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;

}

最佳答案

你是说

[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];

关于ios - 暂停按钮在更改时添加新节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23349842/

相关文章:

ios - 当点击 NSSetUncaughtExceptionHandler 时,使用 CoreGraphics 在屏幕上绘制

swift - Swift 和 SpriteKit 如何将用户的分数转移到另一个场景?

swift - 使用 Swift 在 SpriteKit 中导入外部节点

ios - 停止 tableView.reloadData() 抛出错误?

ios - 有没有办法以编程方式删除我自己的 iOS 应用程序?

ios - Swift 上的自定义 UITableViewCell

ios - 从 Objective-c 快速进行 block 转换

iphone - UITableView 一次添加带有动画的行,但延迟显着

ios - 自动布局 UIScrollView 约束奇怪的行为

ios - SKPhysics : Made a rope, 为什么会坏?