touchesBegan方法的问题

时间:2014-11-21 04:34:09

标签: ios objective-c touch sprite-kit alpha

所以问题是,当我启动它时,soundOff按钮位于soundOn上方,但它是不可见的。所以我看到的是soundOn按钮,当我尝试点击soundOn按钮时,它真的只是按下soundOff按钮而没有给它机会正确运行touchesBegan方法。

@implementation GameScene
{
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.alpha = 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

[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;
    soundLogo.alpha = 0;

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

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

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

1 个答案:

答案 0 :(得分:1)

而不是将alpha更改为0/1你可以做的是从父级删除精灵,你可以添加到子级。

请勿在按钮上同时添加声音和声音,先添加声音打开的默认按钮。

现在,当点击按钮上的声音时,从父母移除按钮上的声音并关闭声音。

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

//-------------Remove below line-----------------//
//[self addChild:soundOff]; 

//Dont add sound off button..... 
}

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

[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"]) {
    [soundLogo removeFromParent];
    [self addChild:soundOff];

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

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

    [soundOff removeFromParent];
    [self addChild:soundLogo];
}
}