gpt4 book ai didi

ios - touchesBegan 方法的问题

转载 作者:行者123 更新时间:2023-11-29 02:29:43 25 4
gpt4 key购买 nike

所以问题是,当我启动它时,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");
}
}

最佳答案

然后将 alpha 更改为 0/1,您可以做的是从父项中删除 Sprite ,然后您可以将其添加到子项中。

不要同时添加声音关闭和声音打开按钮,首先添加声音打开的默认按钮。

现在,当单击按钮上的声音时,从父项中删除按钮上的声音并添加声音关闭。

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

关于ios - touchesBegan 方法的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27054186/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com