Having issues with touchesBegan method

230 Views Asked by At

I have my sound on logo showing up on the screen and the sound off logo appears when i test it. what I want it to do is change from sound on to sound off when I tap on it. The buttons do acknowledge the tap but nothing happens. I don't have much experience in change node images when they're tapped so I don't really know if I used the most efficient code. Can someone look over my code and see what's wrong? Thanks in advance!

@implementation MyScene
{
SKSpriteNode *soundLogo;
SKSpriteNode *soundOff;
}

-(void) addSoundOff:(CGSize)size {
soundOff = [SKSpriteNode spriteNodeWithImageNamed:@"soundOff"];
//resize sprite
soundOff.size = CGSizeMake(soundOff.size.width/2.25, soundOff.size.height/2.25);
//position it
soundOff.position = CGPointMake(65, 25);
//name sound off
soundOff.name = @"soundOff";

soundOff.zPosition = 0;
soundOff.alpha = 0;
//[self addChild:soundOff];
}


-(void) addSoundOn:(CGSize)size {
soundLogo = [SKSpriteNode spriteNodeWithImageNamed:@"soundLogo"];
//resize sprite
soundLogo.size = CGSizeMake(soundLogo.size.width/2.25, soundLogo.size.height/2.25);
//position sprite
CGPoint myPoint = CGPointMake(65, 25);
soundLogo.position = myPoint;
//name sound logo
soundLogo.name = @"soundOn";
//add action

//soundLogo.alpha = 1;
soundLogo.zPosition = 100;

[self addChild:soundLogo];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];

if ([node.name isEqualToString:@"soundOn"]) {

    soundOff.zPosition = 100;
    soundOff.alpha = 1;
    soundLogo.alpha = 0;
    soundLogo.zPosition = 0;

//        [node removeFromParent];
//        [node addChild:soundOff];
    NSLog(@"sound on is pressed");
 }

if ([node.name isEqualToString:@"soundOff"]) {

    soundLogo.zPosition = 100;
    soundLogo.alpha = 1;
    soundOff.alpha = 0;
    soundOff.zPosition = 0;

//        [node removeFromParent];
//        [node addChild:soundLogo];
    NSLog(@"sound off is pressed");
}
}
1

There are 1 best solutions below

7
On BEST ANSWER

It seems nothing wrong in your addSoundOff and addSoundOn functions. What you can do as one of the solutions to change alpha and zPosition properties of SKSpriteNode . And also createsoundOff and soundLogo as instance variables under the implementation. So here is the all code:

#import "MyScene.h"

@implementation MyScene
{
    SKSpriteNode *soundLogo;
    SKSpriteNode *soundOff;
}

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        [self addSoundOn:self.size];
        [self addSoundOff:self.size];

    }
    return self;
}

-(void) addSoundOff:(CGSize)size {
    soundOff = [SKSpriteNode spriteNodeWithImageNamed:@"soundOff"];
    //resize sprite
    soundOff.size = CGSizeMake(soundOff.size.width/2.25, soundOff.size.height/2.25);
    //position it
    soundOff.position = CGPointMake(65, 25);
    //name sound off
    soundOff.name = @"soundOff";

    soundOff.alpha = 0;
    soundOff.zPosition = 0;
    [self addChild:soundOff];

}

-(void) addSoundOn:(CGSize)size {
    SKTexture *soundOn = [SKTexture textureWithImageNamed:@"soundLogo"];
    soundLogo = [SKSpriteNode spriteNodeWithTexture:soundOn];
    //resize sprite
    soundLogo.size = CGSizeMake(soundLogo.size.width/2.25, soundLogo.size.height/2.25);
    //position sprite
    CGPoint myPoint = CGPointMake(65, 25);
    soundLogo.position = myPoint;
    //name sound logo
    soundLogo.name = @"soundOn";
    //add action

    soundLogo.zPosition = 100;
    [self addChild:soundLogo];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    SKNode *node = [self nodeAtPoint:location];

    //sound logo pressed to turn sound on/off
    if ([node.name isEqualToString:@"soundOn"]) {

        soundOff.alpha = 1;
        soundOff.zPosition = 100;
        soundLogo.alpha = 0;
        soundLogo.zPosition = 0;

        NSLog(@"sound on is pressed");
    }

    if ([node.name isEqualToString:@"soundOff"]) {

        soundOff.alpha = 0;
        soundOff.zPosition = 0;
        soundLogo.alpha = 1;
        soundLogo.zPosition = 100;
        NSLog(@"sound off is pressed");
    }
}


@end