Frage

please help me with abstract touches began method. I have this method at scene, VZChurch it's SKSpriteNode. Is there way to transfer touches began method inside Subclass of SKSpriteNode, because i think that 5-6 if else statement not shine decision, so i try to found way to migrate touches began method in every subclass of SKSpriteNode

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self.mapStaticGeometry];
    SKNode *node = [self.mapStaticGeometry nodeAtPoint:location];

    if ([node.name isEqualToString:@"church"]) {

        VZChurch *obj = (VZChurch *)node;
        obj.requestedAnimation = VZAnimationStateTouch;
        [obj runAnimation];
    } else if ...n iterations
}
War es hilfreich?

Lösung

The best way to avoid what you're asking is to subclass an SKNode (or SKSpriteNode) for each node. This then allows you handle the touchesBegan inside the subclassed node.

So just subclass a SKNode (or SKSpriteNode since VZChurch is a sprite) and handling the touchesBegan in VZChurch's it it's own methods file. Then if you have other SKSprites, do the same thing, subclass each sprite and handle the touchesBegan inside their respective subclassed methods file.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top