2014-05-18 56 views
0

我想设置此游戏,以便当用户点击屏幕上的特定点时,它会从该位置分配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]; 
} 

但它没有工作,告诉我,我需要一个括号对于一些原因。

如何设置它,使其只在用户触摸屏幕中间时才会产生?

+0

locationInNode是一种方法。你需要在它周围使用方括号。然而,这里还有许多其他问题。你知道for循环是如何工作的吗? – CrimsonChris

+0

并非完全,for循环是我从SpriteKit项目的模板中偷取的一段代码(当用户点击它时会产生一个飞船)@CrimsonChris –

+0

我已经检查过相同的示例项目。当我告诉你时,请相信我,如果这是你第一次看到for循环,你不会走得太远。我建议阅读初学者面向对象编程。 – CrimsonChris

回答

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 


    } 

希望这将有助于

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]; 
     } 
    } 
相关问题