2014-11-21 53 views
0

所以問題是,當我啓動它時,soundOff按鈕位於soundOn的上方,但它是不可見的。所以我看到的只是soundOn按鈕,當我嘗試點擊soundOn按鈕時,它實際上只是點擊soundOff按鈕,並沒有給它一個正確運行touchesBegan方法的機會。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

然後改變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]; 
} 
} 
+0

我知道removeFromParent和addChild,但touchesBegan方法中的代碼是什麼? – 2014-11-21 05:50:36

+0

看到上面的代碼,如果它適合你 – 2014-11-21 06:04:38

+0

我有removeFromParent和addChild的確切代碼,我擺脫了soundOff方法調用中的addChild。現在,當我運行並單擊它時,我的遊戲終止於類型爲NSException的未捕獲異常。不知道發生了什麼事。 – 2014-11-21 06:57:19