Objective c 编写我自己的方法-SpriteKit

Objective c 编写我自己的方法-SpriteKit,objective-c,methods,sprite-kit,Objective C,Methods,Sprite Kit,我经常发现自己在重复代码,所以我想创建一个方法来处理它,并减少大量重复。我很难理解这些方法,所以有人可以建议我如何创建一个自定义方法来处理TouchesBegind方法中的重复代码 谢谢 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (vLineName in vLineArray) { NSString *value = [vLineArray objectForKey:vLineNam

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

谢谢

- (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);
        }
    }
}
}

您可以从ToucheSBegind: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];
}