0

Hey so I have created two buttons that when pressed transition the current scene to either the next scene or the main menu and they work but neither button disappears after it has been pressed. They both carry over onto the next scene which I don't want. I've tried hiding the buttons and removing them from the superView but neither worked. Heres what the code looks like for what happens when it is pressed.

-(IBAction)buttonPressed:(UIButton *)sender
{
    if ([sender isEqual:self.continueButton]) {
        SKTransition* reveal = [SKTransition doorsCloseVerticalWithDuration:0.5];

        SKScene* transition2Scene = [[Transition2Scene alloc] initWithSize:self.size];
        [self.view presentScene:transition2Scene transition: reveal];
        [self.continueButton removeFromSuperview];
        self.continueButton.hidden = YES;
        self.continueButton.enabled = NO;

    }
    else {
        SKTransition* doors = [SKTransition doorsCloseVerticalWithDuration:0.5];

        SKScene* spriteMyScene = [[SpriteMyScene alloc] initWithSize:self.size];

        [self.view presentScene:spriteMyScene transition: doors];
        self.menuButton.hidden = YES;
        self.menuButton.enabled = NO;
        [self.menuButton removeFromSuperview];
    }
}

And heres what the coding of the buttons themselves looks like.

self.continueButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.continueButton.frame = CGRectMake(self.frame.size.width/3, self.frame.size.height * 0.5, 120, 70);
[self.continueButton setTitle:@"Continue" forState:UIControlStateNormal];
[self.continueButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.continueButton];

self.menuButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.menuButton.frame = CGRectMake(self.frame.size.width/3, self.frame.size.height * 0.6, 120, 70);
[self.menuButton setTitle:@"Main Menu" forState:UIControlStateNormal];
[self.menuButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.menuButton];

I've looked around for answers and tried some things but they didn't work cause the issue I'm having isn't quite the same so any help would be greatly appreciated thanks!

modusCell
  • 13,151
  • 9
  • 53
  • 80
alec6134
  • 3
  • 2

1 Answers1

0

The SKScene and its contents are remove from the view when transitioning to a new scene. Views, such as your buttons, are not removed. You will need to manually remove them. Here's an example...

- (IBAction) buttonPressed:(UIButton *)sender
{
    [self.continueButton removeFromSuperview];
    [self.menuButton removeFromSuperview];

    // Find and remove all UIButtons in your view
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            NSLog(@"%@", view);
            [view removeFromSuperview];
        }
    }

    // Verify that the UIButtons were removed
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            NSLog(@"this shouldn't happen: %@", view);
        }
    }

    // The rest of your code

Alternatively, you can create buttons using SKSpriteNode and add them to your scene. Here's an example...

Setting up buttons in SKScene

Community
  • 1
  • 1
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • Okay well my IBAction method already has the removeFromSuperView as a part of it for each button when they are pressed for the current scene so are you saying I need to add that method a second time to the next scene? Cause if so I just tried that but when I clicked them a second time I got EXC Bad Acess error... – alec6134 Jul 28 '14 at 20:03
  • Are the removeFromSuperViews before the scene transition code? – 0x141E Jul 28 '14 at 20:09
  • Yeah I changed them following your suggestion but I still get the same problem... – alec6134 Jul 29 '14 at 03:37
  • Set a breakpoint on one of the removeFromSuperview to see if instance variables are ok. – 0x141E Jul 29 '14 at 17:35
  • I've add code to my answer. Try copying the two for loops into your buttonPressed method. The first loop looks for UIButtons currently in the view and removes them. The second loop makes sure there aren't any UIButtons in the view. – 0x141E Jul 30 '14 at 08:52