SpriteKit Objective-C:编程方向键盘控件

时间:2015-03-23 14:08:49

标签: ios objective-c sprite-kit

所以让我直截了当: 我想要实现的是以方向键的形式控制我的SpriteKit游戏。我希望它的工作方式,例如,你可以按住一个按钮,然后将手指滑动到另一个按钮,这样第一个按钮就会被释放,而另一个按钮会被按下(就像在实体游戏中一样)控制器,例如)。我看到这是在许多现有游戏(Carmageddon,Minecraft PE)上实现的,所以有可能确定。

我一直试图使用的是简单的touchesBegan和touchesEnded,但问题是一旦你将手指从第一个按钮移动到第二个按钮,第二个按钮就不会被按下,而第一个保持“按下”状态,直到您停止触摸屏幕。我还尝试使用touchesMoved来检测触摸位置是否已经改变,但是它引起了很多不同的错误。

请把我推向正确的方向。我希望我所要求的是可能的(我使用的是Objective-c)。

1 个答案:

答案 0 :(得分:0)

它真的只是为你的控件设置一个真/假状态。就像你使用真正的控制器一样。您首先要检查touchesBegan方法中的接触触摸并相应地设置4个控制器BOOL变量。在did touchesMoved方法中,再次检查用户的触摸是否与任何控制器节点进行联系,并相应地设置BOOL变量。

在您的更新方法中,您可以检查4个BOOL变量的状态并相应地执行您的代码。

(我在下面的代码中使用横向方向)

#import "GameScene.h"

@implementation GameScene {

    SKSpriteNode *node0;
    SKSpriteNode *node1;
    SKSpriteNode *node2;
    SKSpriteNode *node3;

    BOOL node0touch;
    BOOL node1touch;
    BOOL node2touch;
    BOOL node3touch;
}

-(void)didMoveToView:(SKView *)view {
    self.backgroundColor = [SKColor blackColor];

    node0 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 50)];
    node0.name = @"node0";
    node0.position = CGPointMake(200, 200);
    [self addChild:node0];

    node1 = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
    node1.name = @"node1";
    node1.position = CGPointMake(200, 400);
    [self addChild:node1];

    node2 = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(50, 50)];
    node2.name = @"node2";
    node2.position = CGPointMake(100, 300);
    [self addChild:node2];

    node3 = [SKSpriteNode spriteNodeWithColor:[SKColor grayColor] size:CGSizeMake(50, 50)];
    node3.name = @"node3";
    node3.position = CGPointMake(300, 300);
    [self addChild:node3]; 
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];

        SKNode *node = [self nodeAtPoint:touchLocation];
        if(node.name != nil) {
            if([node.name isEqualToString:@"node0"]) {
                node0touch = true;
                node1touch = false;
                node2touch = false;
                node3touch = false;
            }
            if([node.name isEqualToString:@"node1"]) {
                node0touch = false;
                node1touch = true;
                node2touch = false;
                node3touch = false;
            }
            if([node.name isEqualToString:@"node2"]) {
                node0touch = false;
                node1touch = false;
                node2touch = true;
                node3touch = false;
            }
            if([node.name isEqualToString:@"node3"]) {
                node0touch = false;
                node1touch = false;
                node2touch = false;
                node3touch = true;
            }
        } else {
            node0touch = false;
            node1touch = false;
            node2touch = false;
            node3touch = false;
        }
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    for (UITouch *touch in touches) {
        CGPoint touchLocation = [touch locationInNode:self];

        SKNode *node = [self nodeAtPoint:touchLocation];
        if(node.name != nil) {
            if([node.name isEqualToString:@"node0"]) {
                node0touch = true;
                node1touch = false;
                node2touch = false;
                node3touch = false;
            }
            if([node.name isEqualToString:@"node1"]) {
                node0touch = false;
                node1touch = true;
                node2touch = false;
                node3touch = false;
            }
            if([node.name isEqualToString:@"node2"]) {
                node0touch = false;
                node1touch = false;
                node2touch = true;
                node3touch = false;
            }
            if([node.name isEqualToString:@"node3"]) {
                node0touch = false;
                node1touch = false;
                node2touch = false;
                node3touch = true;
            }
        } else {
            node0touch = false;
            node1touch = false;
            node2touch = false;
            node3touch = false;
        }
    }
}