【发布时间】: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