检测双击SKNode

时间:2014-03-12 02:48:19

标签: ios iphone touch sprite-kit

好的,所以我有一些代码可以选择一个介于1和2之间的随机数,并根据它生成一个metalCrate:或者只是一个crate:。我想,金属箱应该用2个水龙头消失。不幸的是,使用我的代码,这不起作用。

@interface MyScene () <SKPhysicsContactDelegate>

@property (nonatomic) NSTimeInterval lastSpawnTimeInterval;
@property (nonatomic) NSTimeInterval lastUpdateTimeInterval;
@property (nonatomic) SKSpriteNode *crate;
@property (nonatomic) SKSpriteNode *metalCrate;
@property (nonatomic) SKLabelNode *scoreLabel;
@property (nonatomic) int score;

@end

@implementation MyScene

-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {

        self.backgroundColor = [SKColor whiteColor];
        _score = 0;

        _scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Helvetica Neue"];
        _scoreLabel.text = [NSString stringWithFormat:@"%d", self.score];
        _scoreLabel.fontSize = 25;
        _scoreLabel.fontColor = [SKColor blackColor];
        _scoreLabel.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
        _scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
        _scoreLabel.position = CGPointMake(20, 27);
        [self addChild:_scoreLabel];
    }

    return self;
}

- (void)addCrate:(SKColor*)color withDuration:(int)duration withSize:(CGSize)size withMinX:(int)minX withMaxX:(int)maxX {
    self.crate = [SKSpriteNode spriteNodeWithColor:color size:size];
    self.crate.color = color;

    int rangeX = maxX - minX;
    int actualX = (arc4random_uniform(rangeX)) + minX;

    self.crate.position = CGPointMake(actualX, self.frame.size.height + self.crate.size.height/2);
    [self addChild:self.crate];
    self.crate.size = size;

    SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, -self.crate.size.height/2) duration:duration];
    SKAction * actionMoveDone = [SKAction removeFromParent];
    [self.crate runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}

- (void)addMetalCrate:(SKColor*)color withDuration:(int)duration withSize:(CGSize)size withMinX:(int)minX withMaxX:(int)maxX {
    self.metalCrate = [SKSpriteNode spriteNodeWithColor:color size:size];
    self.metalCrate.color = color;

    int rangeX = maxX - minX;
    int actualX = (arc4random_uniform(rangeX)) + minX;

    self.metalCrate.position = CGPointMake(actualX, self.frame.size.height + self.metalCrate.size.height/2);
    [self addChild:self.metalCrate];
    self.metalCrate.size = size;

    SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, -self.metalCrate.size.height/2) duration:duration];
    SKAction * actionMoveDone = [SKAction removeFromParent];
    [self.metalCrate runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
}

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

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    SKNode *touchedNode = [self nodeAtPoint:touchLocation];

    if (touchedNode != self && touchedNode != self.scoreLabel && [self nodeAtPoint:touchLocation] == self.crate) {
        NSLog(@"Node has been touched. Single tap.");
        [touchedNode removeFromParent];
        [self updateScore:1];
    } else if (touchedNode != self && touchedNode != self.scoreLabel && [self nodeAtPoint:touchLocation] == self.metalCrate && [touch tapCount] == 2) {
        NSLog(@"Node has been touched.  Double tap.");
        [touchedNode removeFromParent];
        [self updateScore:2];
    }
}

- (void)updateScore:(int)amount {

    _score += amount;
    _scoreLabel.text = [NSString stringWithFormat:@"%d", self.score];
}

- (void)update:(NSTimeInterval)currentTime {
    // Handle time delta.
    // If we drop below 60fps, we still want everything to movbe the same distance.
    CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
    self.lastUpdateTimeInterval = currentTime;
    if (timeSinceLast > 1) { // more than a second since last update
        timeSinceLast = 1.0 / 60.0;
        self.lastUpdateTimeInterval = currentTime;
    }

    [self updateWithTimeSinceLastUpdate:timeSinceLast];
}

- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {

    self.lastSpawnTimeInterval += timeSinceLast;
    if (self.lastSpawnTimeInterval > 0.5) {

        self.lastSpawnTimeInterval = 0;

        //Modify instance properties here

        int random = (arc4random() % 2) + 1;
        //NSLog(@"%d", random);

        if (random == 1) {

            [self addCrate:[SKColor grayColor] withDuration:3.75 withSize:CGSizeMake(45, 45)
                  withMinX:self.crate.size.width / 2 withMaxX:self.frame.size.width - self.crate.size.width / 2];

        } else {

            [self addMetalCrate:[SKColor redColor] withDuration:3 withSize:CGSizeMake(50, 50)
                  withMinX:self.crate.size.width / 2 withMaxX:self.frame.size.width - self.crate.size.width / 2];
        }

        //Modify instance properties here
    }
}

@end

为了解释,我认为问题在于检测触摸是在金属板上还是仅仅是板条箱,我将git与触摸位置进行比较。

提前谢谢!

2 个答案:

答案 0 :(得分:2)

您可以为您的箱子和金属箱子节点命名,只需将其用于比较。

[self.crate setName:@"crate"];
[self.metalCrate setName:@"metalCrate"];

编辑你的-touchesBegan:方法如下:

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

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInNode:self];
    SKNode *touchedNode = [self nodeAtPoint:touchLocation];

    if ([touchedNode.name isEqualToString:@"crate"] && [touch tapCount] == 1) {
        NSLog(@"Crate node has been touched. Single tap.");
        [touchedNode removeFromParent];
        [self updateScore:1];
    } else if ([touchedNode.name isEqualToString:@"metalCrate"] && [touch tapCount] == 2) {
        NSLog(@"Metal crate node has been touched.  Double tap.");
        [touchedNode removeFromParent];
        [self updateScore:2];
    }
}

答案 1 :(得分:0)

想出来。

我所做的就是使用比较字符串的想法。在这种情况下,我比较了节点的大小以确定它是什么。

感谢您的帮助!