SpriteKit BoundingBox检测纹理变化

时间:2014-03-07 18:20:45

标签: objective-c ios7 sprite-kit detection bounding-box

编辑:嗨! (忘了问好......)

我有一个非常讨厌的问题。

我创建了一个小程序,到目前为止效果很好。但是,我试图在它悬停在另一个SKSpriteNode上时更改SKSpriteNode纹理。 为了测试实现,我添加了一个NSLog()来打印悬停节点的原点。 这样可行。但纹理更改仅在将鼠标悬停在最后一个迭代节点上时才有效。 复杂。这是一些代码:

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

小更新():

-(void)update:(NSTimeInterval)currentTime {

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

    [self checkAimingHover];
}

1 个答案:

答案 0 :(得分:1)

您正在重置每个循环上的纹理。试试这个:

   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];
    }

另外我应该告诉你一个超酷的.calculateAccumulatedFrame属性,这个属性几乎与你的边界框有关,但也有孩子。