1
我有我的聲音顯示在屏幕上的標誌和聲音關閉標誌出現時,我測試它。當我點擊它時,我想要做的是從聲音變爲聲音。按鈕確認敲擊,但沒有任何反應。我沒有太多經驗可以在更改節點圖像的時候點擊,所以我不知道我是否使用了最高效的代碼。有人可以看看我的代碼,看看有什麼不對嗎?提前致謝!觸摸有問題開始方法
@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");
}
}
好的思想,將工作,但是當我運行它,它顯示了soundOn按鈕,所以我點擊它,它說,我輕敲soundOff按鈕,沒有任何反應。 – 2014-11-21 04:17:59
所以問題是,當我啓動它時,soundOff按鈕位於soundOn的上方,但它不可見。所以我看到的只是soundOn按鈕,當我嘗試點擊soundOn按鈕時,它實際上只是點擊soundOff按鈕,並沒有給它一個正確運行touchesBegan方法的機會。 – 2014-11-21 04:28:20
你是完全正確的,我改變了答案,希望它現在可以按你所希望的方式工作。 @NicholasWhitley – 2014-11-21 06:20:16