在skview中创建按钮以指向不同的场景 [英] creating buttons in skview to point to different scenes
问题描述
我是第一次尝试精灵套件。只是为了陈述显而易见的,我已经通过了文档,但我无法安静地掌握创建多个按钮的想法。我可以创建按钮。它们出现在我的视图中,但我无法将这些功能附加到它们以指向不同的视图。这是我用来创建按钮等的代码:
i am trying the sprite kit for the first time. just to state the obvious, I've been through the doc's but i can't quiet grasp the idea of creating multiple buttons yet. i can create the buttons alright. and they appear in my view, but i can't attach the function to them in order to point to different views. here is the code I'm using for creating the buttons and so on:
#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
使用上面的代码时我按下场景出现的第一个按钮,然后冻结,哦项目会崩溃。我想这与我宣布按钮的方式有关。如果我在中注释掉按钮1到3 - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)事件
方法,它确实加载了场景并且没有崩溃。有人可以帮我解决这个问题。我知道我没有以正确的方式宣布其他三个按钮,但我似乎无法正确实现它们。
using the code above when i press the first button the scene shows up, then freezes and oh well the project would crash. i guess it has something to do with the way i declared the buttons. if i comment out the button 1 through 3 in - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
method, it does load the scene fine and no crash. can some one please help me out with this. i know i am not declaring the other three buttons the right way, but i just can't seem to be able to implement them properly.
推荐答案
你应该只在中分配一次节点 - (void)touchesBegan:(NSSet *)触及withEvent:(UIEvent *)event
然后检查它是哪个节点,根据其名称:
You should only assign the node once in - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
and then check which node it is, based on its name:
- (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];
}
}
我还将您的操作代码放入方法中,减少重复:
I've also put your action code into a method, to cut down on duplication:
-(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];
}];
}
这篇关于在skview中创建按钮以指向不同的场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!