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");
}
}
我知道removeFromParent和addChild,但touchesBegan方法中的代碼是什麼? – 2014-11-21 05:50:36
看到上面的代碼,如果它適合你 – 2014-11-21 06:04:38
我有removeFromParent和addChild的確切代碼,我擺脫了soundOff方法調用中的addChild。現在,當我運行並單擊它時,我的遊戲終止於類型爲NSException的未捕獲異常。不知道發生了什麼事。 – 2014-11-21 06:57:19