有touchesBegan方法的问题

时间:2014-11-21 02:34:12

标签: ios objective-c button sprite-kit touchesbegan

我的屏幕上出现徽标声音,测试时会出现声音徽标。我想要它做的是当我点击它时从声音变为声音。按钮确实确认了水龙头但没有任何反应。我们在点击更改节点图像时没有太多经验,所以我真的不知道我是否使用了最有效的代码。有人可以查看我的代码,看看有什么问题吗?提前谢谢!

@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");
}
}

1 个答案:

答案 0 :(得分:0)

您的addSoundOffaddSoundOn函数似乎没有错。作为改变alpha的{​​{1}}和zPosition属性的解决方案之一,您可以做些什么。并在实现下创建SKSpriteNodesoundOff作为实例变量。所以这是所有代码:

soundLogo