在skview中创buildbutton指向不同的场景
我正在尝试第一次使用sprite套件。 只是为了陈述明显的,我已经通过了文件,但我不能安静地掌握创build多个button的想法呢。 我可以创buildbutton好吧。 他们出现在我看来,但我不能附加function,以指出不同的意见。 这里是我用来创buildbutton的代码等等:
#import "Levels.h" #import "MyScene.h" #import "MyScene1.h" @interface Levels () @property BOOL contentCreated; @end @implementation Levels - (void)didMoveToView: (SKView *) view { if (!self.contentCreated) { [self createSceneContents]; self.contentCreated = YES; } } - (void)createSceneContents { self.backgroundColor = [SKColor blueColor]; self.scaleMode = SKSceneScaleModeAspectFit; [self addChild: [self button]]; [self addChild: [self button1]]; [self addChild: [self button2]]; [self addChild: [self button3]]; } - (SKLabelNode *)button { SKLabelNode *button = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; button.text = @"Level 1"; button.fontSize = 42; button.position = CGPointMake(CGRectGetMidX(self.frame),250); button.name = @"button"; return button; } - (SKLabelNode *)button1 { SKLabelNode *button1 = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; button1.text = @"Level 2"; button1.fontSize = 42; button1.position = CGPointMake(CGRectGetMidX(self.frame),200); button1.name = @"button1"; return button1; } - (SKLabelNode *)button2 { SKLabelNode *button2 = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; button2.text = @"Level 3"; button2.fontSize = 42; button2.position = CGPointMake(CGRectGetMidX(self.frame),150); button2.name = @"button2"; return button2; } - (SKLabelNode *)button3 { SKLabelNode *button3 = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; button3.text = @"Level 4"; button3.fontSize = 42; button3.position = CGPointMake(CGRectGetMidX(self.frame),100); button3.name = @"button3"; return button3; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location = [touch locationInNode:self]; SKNode *button = [self nodeAtPoint:location]; SKNode *button1 = [self nodeAtPoint:location]; SKNode *button2 = [self nodeAtPoint:location]; SKNode *button3 = [self nodeAtPoint:location]; //if fire button touched, bring the rain if (button != nil) { button.name = nil; SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5]; SKAction *zoom = [SKAction scaleTo: 2.0 duration: 0.25]; SKAction *pause = [SKAction waitForDuration: 0.5]; SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25]; SKAction *remove = [SKAction removeFromParent]; SKAction *moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]]; [button runAction: moveSequence completion:^{ SKScene *spaceshipScene = [[MyScene alloc] initWithSize:self.size]; SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:0.5]; [self.view presentScene:spaceshipScene transition:doors]; }]; } if (button1 != nil) { button1.name = nil; SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5]; SKAction *zoom = [SKAction scaleTo: 2.0 duration: 0.25]; SKAction *pause = [SKAction waitForDuration: 0.5]; SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25]; SKAction *remove = [SKAction removeFromParent]; SKAction *moveSequence1 = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]]; [button runAction: moveSequence1 completion:^{ SKScene *spaceshipScene1 = [[MyScene1 alloc] initWithSize:self.size]; SKTransition *doors1 = [SKTransition doorsOpenVerticalWithDuration:0.5]; [self.view presentScene:spaceshipScene1 transition:doors1]; }]; } if (button2 != nil) { button2.name = nil; SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5]; SKAction *zoom = [SKAction scaleTo: 2.0 duration: 0.25]; SKAction *pause = [SKAction waitForDuration: 0.5]; SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25]; SKAction *remove = [SKAction removeFromParent]; SKAction *moveSequence2 = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]]; [button runAction: moveSequence2 completion:^{ SKScene *spaceshipScene2 = [[MyScene alloc] initWithSize:self.size]; SKTransition *doors2 = [SKTransition doorsOpenVerticalWithDuration:0.5]; [self.view presentScene:spaceshipScene2 transition:doors2]; }]; } if (button3 != nil) { button3.name = nil; SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5]; SKAction *zoom = [SKAction scaleTo: 2.0 duration: 0.25]; SKAction *pause = [SKAction waitForDuration: 0.5]; SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25]; SKAction *remove = [SKAction removeFromParent]; SKAction *moveSequence3 = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]]; [button runAction: moveSequence3 completion:^{ SKScene *spaceshipScene3 = [[MyScene alloc] initWithSize:self.size]; SKTransition *doors3 = [SKTransition doorsOpenVerticalWithDuration:0.5]; [self.view presentScene:spaceshipScene3 transition:doors3]; }]; } } @end
使用上面的代码,当我按下第一个button的场景显示,然后冻结,哦,项目会崩溃。 我想这与我宣布button的方式有关。 如果我注释掉button1到3在- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
方法,它确实加载场景罚款和没有崩溃。 有人可以帮我解决这个问题。 我知道我不是正确的方式宣布其他三个button,但我似乎无法正确实施它们。
您应该只在- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
指定节点一次- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
,然后根据名称检查它是哪个节点:
- (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:@"button"]) { [self runButtonActionOnNode:node]; } else if ([node.name isEqualToString:@"button1"]) { [self runButtonActionOnNode:node]; } else if ([node.name isEqualToString:@"button2"]) { [self runButtonActionOnNode:node]; } else if ([node.name isEqualToString:@"button3"]) { [self runButtonActionOnNode:node]; } }
我也把你的行动代码放在一个方法中,以减less重复:
-(void)runButtonActionOnNode:(SKNode*) node{ node.name = nil; SKAction *moveUp = [SKAction moveByX: 0 y: 100.0 duration: 0.5]; SKAction *zoom = [SKAction scaleTo: 2.0 duration: 0.25]; SKAction *pause = [SKAction waitForDuration: 0.5]; SKAction *fadeAway = [SKAction fadeOutWithDuration: 0.25]; SKAction *remove = [SKAction removeFromParent]; SKAction *moveSequence = [SKAction sequence:@[moveUp, zoom, pause, fadeAway, remove]]; [node runAction: moveSequence completion:^{ SKScene *spaceshipScene = [[MyScene alloc] initWithSize:self.size]; SKTransition *doors = [SKTransition doorsOpenVerticalWithDuration:0.5]; [self.view presentScene:spaceshipScene transition:doors]; }]; }
我写了一个答案,创build一个专门用作button的类。 它在这里可用,并允许您为button的选定状态和正常状态设置图像,以及调用内部事件和触发内部事件的方法。 我认为你可以更容易地创build你的button作为这个类的实例,然后把这个部分的动作分配到不同的方法中的一个新的场景中,并将它们设置为事件调用的动作。 当你使用这个课程的时候,你甚至不需要touchesBegan
。