【问题标题】:Writing my own Methods - SpriteKit编写我自己的方法 - SpriteKit
【发布时间】:2014-02-22 14:17:40
【问题描述】:

我经常发现自己在重复代码,所以我想创建一个方法来处理它并减少大量重复。我很难理解方法,所以有人可以建议我如何创建一个自定义方法来处理 touchesBegan 方法中的重复代码。

谢谢

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

for (vLineName in vLineArray) {

    NSString *value = [vLineArray objectForKey:vLineName];
    SKAction *changeLineToDark = [SKAction setTexture:[SKTexture textureWithImageNamed:@"darkTexture.png"]];


    for (UITouch *touch in touches) {
        CGPoint touchPoint = [touch locationInNode:self];
        SKSpriteNode *box = (SKSpriteNode *)[self childNodeWithName:value];
        if (CGRectContainsPoint(box.frame, touchPoint)) {
            [box runAction:changeLineToDark];
            box.zPosition = 2;
            NSLog (@"Just Touched %@", vLineName);
        }
     }
}

for (hLineName in hLineArray) {

    NSString *value = [hLineArray objectForKey:hLineName];
    SKAction *changeLineToDark = [SKAction setTexture:[SKTexture textureWithImageNamed:@"darkTexture.png"]];


    for (UITouch *touch in touches) {
        CGPoint touchPoint = [touch locationInNode:self];
        SKSpriteNode *box = (SKSpriteNode *)[self childNodeWithName:value];
        if (CGRectContainsPoint(box.frame, touchPoint)) {
            [box runAction:changeLineToDark];
            box.zPosition = 2;
            NSLog (@"Just Touched %@", hLineName);
        }
    }
}
}

【问题讨论】:

    标签: objective-c methods sprite-kit


    【解决方案1】:

    您可以从 touchesBegan:withEvent: 方法中提取代码,例如:

    -(void)handleTouch:(NSSet *)touches
    {
        for (vLineName in vLineArray) {
    
        NSString *value = [vLineArray objectForKey:vLineName];
        SKAction *changeLineToDark = [SKAction setTexture:[SKTexture textureWithImageNamed:@"darkTexture.png"]];
    
    
        for (UITouch *touch in touches) {
            CGPoint touchPoint = [touch locationInNode:self];
            SKSpriteNode *box = (SKSpriteNode *)[self childNodeWithName:value];
            if (CGRectContainsPoint(box.frame, touchPoint)) {
                [box runAction:changeLineToDark];
                box.zPosition = 2;
                NSLog (@"Just Touched %@", vLineName);
            }
         }
    }
    
    for (hLineName in hLineArray) {
    
        NSString *value = [hLineArray objectForKey:hLineName];
        SKAction *changeLineToDark = [SKAction setTexture:[SKTexture textureWithImageNamed:@"darkTexture.png"]];
    
    
        for (UITouch *touch in touches) {
            CGPoint touchPoint = [touch locationInNode:self];
            SKSpriteNode *box = (SKSpriteNode *)[self childNodeWithName:value];
            if (CGRectContainsPoint(box.frame, touchPoint)) {
                [box runAction:changeLineToDark];
                box.zPosition = 2;
                NSLog (@"Just Touched %@", hLineName);
            }
        }
    }
    }
    

    并且每次触摸屏幕时都可以调用它:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self handleTouch:touches];
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-17
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多