精灵套件,按下按钮延迟

时间:2014-05-21 05:41:15

标签: sprite-kit

我正在编写一个使用按钮的精灵套件游戏,它们是游戏中非常重要的一部分。问题是我需要他们有延迟,所以说你只能每5分钟按一次。在此先感谢您的帮助。

.H文件

    typedef NS_ENUM(NSInteger, ButtonState)
    {
        On,
        Off
    };

    @interface Button2 : SKLabelNode

    - (instancetype)initWithState:(ButtonState) setUpState;
    - (void) buttonPressed;

    @end

.M文件

@implementation Button2 
{
    ButtonState _currentState;
}

- (id)initWithState:(ButtonState) setUpState
{
    if (self = [super init]) {
        _currentState = setUpState;
        self = [Button2 labelNodeWithFontNamed:@"Chalkduster"];
        self.text = [self updateLabelForCurrentState];
        self.fontSize = 30;

    }
    return self;
}

- (NSString *) updateLabelForCurrentState
{
    NSString *label;

    if (_currentState == On) {
        label = @"Sell";

    }
    else if (_currentState == Off) {

    }

    return label;
}

- (void) buttonPressed
{
    if (_currentState == Off) {
        _currentState = On;

    }
    else {
        _currentState = Off;
    }

    self.text = [self updateLabelForCurrentState];
}

@end

1 个答案:

答案 0 :(得分:0)

我猜测触摸代表正在场景中实现,您可以使用它在touchPoint上找到节点并在其上调用buttonPressed方法。

您可以设置一个bool标志,可以在按下按钮时设置。

在你的.h文件中:

@property BOOL interactionAllowed;

您可以在场景的触摸代理中访问此属性,如:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInNode:self];

    SKNode *node = [self nodeAtPoint:touchPoint];

    if ([node isKindOfClass:[Button2 class]])
    {
        Button2 *button = (Button2*)node;
        if (button.interactionAllowed)
        {
            [button buttonPressed];
        }
    }
}

关于设置(和重置)interactionAllowed属性:

- (void) buttonPressed
{
    if (_currentState == Off) {
        _currentState = On;
        self.interactionAllowed = NO;

        [self runAction:[SKAction sequence:@[[SKAction waitForDuration:3000], [SKAction runBlock:^{
            self.interactionAllowed = YES;
        }]]]];

    }
    else {
        _currentState = Off;
    }

    self.text = [self updateLabelForCurrentState];
}