2014-09-02 80 views
0

我做一個遊戲,我只希望用戶觸摸屏幕的邊緣?有什麼辦法可以做到這一點?雪碧 - 如何只觸摸屏幕的某些部分?

下面的代碼讓用戶觸摸在屏幕任意位置,但我想他們只是觸摸屏幕的某些部分。

謝謝你的時間和答案

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

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

     SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"wall"]; 

     sprite.position = location; 
     sprite.scale = 0.5; 
     sprite.name = @"wall"; // Needed For Release Thing 
     sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size]; 
     sprite.physicsBody.dynamic = NO; 
     sprite.physicsBody.categoryBitMask = wallCategory; 


     SKAction *wait = [SKAction waitForDuration:0.4]; 
     SKAction *delete = [SKAction removeFromParent]; 


     SKAction *sequence = [SKAction sequence:@[wait, delete, ]]; 
     [sprite runAction: sequence]; 

     [self addChild:sprite]; 

    } 
} 

回答

1

我沒有測試這個,所以我不知道這是否會工作

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

    if (((location.x < leftTouchArea) || (location.x > rightTouchArea)) && ((location.y < upperTouchArea) || (location.y > lowerTouchArea))) 
    { 

     SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"wall"]; 

     sprite.position = location; 
     sprite.scale = 0.5; 
     sprite.name = @"wall"; // Needed For Release Thing 
     sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size]; 
     sprite.physicsBody.dynamic = NO; 
     sprite.physicsBody.categoryBitMask = wallCategory; 


     SKAction *wait = [SKAction waitForDuration:0.4]; 
     SKAction *delete = [SKAction removeFromParent]; 


     SKAction *sequence = [SKAction sequence:@[wait, delete, ]]; 
     [sprite runAction: sequence]; 

     [self addChild:sprite]; 
    } 

} 
+0

即使沒有你的方法工作,以解決這一問題是完全正確的。 – meisenman 2014-09-02 15:29:47