如何设置水龙头的位置

时间:2014-05-18 22:53:53

标签: sprite-kit touchesbegan

我想设置这个游戏,以便当用户点击屏幕上的特定点时,它会从该位置分配 SKSpriteNode 。所以我设置的内容就是触摸开始的方法:

for (UITouch *touch in touches) {
    score = score + 1;

    cat = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"cat.png"] size:CGSizeMake(35, 35)];
    cat.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)+120);

    [self addChild:cat];
}

工作正常,并将节点添加到触摸发生的任何位置。

我希望只在用户触摸特定位置时添加它,所以我尝试设置它:

for (touch locationInNode:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)) {
    score = score + 1;

    cat = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"cat.png"] size:CGSizeMake(35, 35)];
    cat.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)+120);

    [self addChild:cat];
}

但由于某种原因,它没有工作并且告诉我需要一个方括号。

如何进行设置以便只有在用户触摸屏幕中间时才会生成?

2 个答案:

答案 0 :(得分:0)

想出来:

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];
        if (CGRectContainsPoint(crate.frame, location)) {
            score = score + 1;

   cat = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"cat.png"] size:CGSizeMake(35, 35)];
    cat.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)+120);

    [self addChild:cat];
        }
    }

答案 1 :(得分:0)

我认为你可以使用

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
        UITouch* touch = [touches anyObject];
        CGPoint positionInScene = [touch locationInNode:self];
        // Add your logic to check the specific location 
        // if( CGPointEqualToPoint (positionInScene, yourSpecificPosition)

        // Render Cat


  }

希望它会有所帮助