Question

Edit: Hi at all! (forgot to say hello...)

I got a pretty annoying problem.

I've created a small program which works quite good so far. However, I'm trying to change an SKSpriteNode texture whenever it's hovering over another SKSpriteNode. To test the implementation I added an NSLog() to print the hovered nodes origin. THAT works. But the texture change only works when hovering over the last itereated node. Complicated. Here is some code:

-(void)checkAimingHover{

for (SKSpriteNode *childSprite in [parentNode children]) {
        CGRect boundingBox =
        CGRectMake(childSprite.position.x-childSprite.size.width/2,
                   childSprite.position.y-childSprite.size.height/2,
                   childSprite.frame.size.width,
                   childSprite.frame.size.height);
        if (CGRectContainsPoint(boundingBox, aiming.position)) {
            // THIS works like a charm
            NSLog(@"%f/%f",boundingBox.origin.x, boundingBox.origin.y); 
            // THIS only works on the last created boundingBox
            [aiming setTexture:[SKTexture textureWithImageNamed:@"aim_hover"]]; 
        } else [aiming setTexture:aimDefault];


    }
}

aming: SKSpriteNode *aiming;

And the small update():

-(void)update:(NSTimeInterval)currentTime {

    if (lastUpdateTime) {
        deltaTime = currentTime - lastUpdateTime;
    } else {
        deltaTime = 0;
    }
    lastUpdateTime = currentTime;

    [self checkAimingHover];
}
Was it helpful?

Solution

You are resetting your texture on each loop. Try this instead:

   BOOL hitTestDidHit = NO;
   for (SKSpriteNode *childSprite in [parentNode children]) {
    CGRect boundingBox =
    CGRectMake(childSprite.position.x-childSprite.size.width/2,
               childSprite.position.y-childSprite.size.height/2,
               childSprite.frame.size.width,
               childSprite.frame.size.height);
     if (CGRectContainsPoint(boundingBox, aiming.position)) {
        // THIS works like a charm
        NSLog(@"%f/%f",boundingBox.origin.x, boundingBox.origin.y); 
        // THIS only works on the last created boundingBox
        hitTestDidHit = YES;
     }
    }


    if(hitTestDidHit) {
        [aiming setTexture:[SKTexture textureWithImageNamed:@"aim_hover"]]; 
    } else {
        [aiming setTexture:aimDefault];
    }

Also I should tell you about the super cool .calculateAccumulatedFrame property which pretty much does what your bounding box does but with children as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top