生成其他节点后停止旋转

时间:2014-08-22 08:54:38

标签: ios objective-c sprite-kit

猫正在产生并跟随僵尸。他们也面对着僵尸,但当其他猫产生时,旋转停止。

-(void)spawningCat
{
    [self enumerateChildNodesWithName:@"cat" usingBlock:^(SKNode *node, BOOL *stop){
        float actionDuration = 0.1;
        CGPoint offset = CGPointSubtract(_zombie.position, node.position);
        CGPoint direction = CGPointNormalize(offset);
        CGPoint amountToMovePerSec = CGPointMultiplyScalar(direction, CAT_MOVE_POINTS_PER_SEC);
        CGPoint amountToMove = CGPointMultiplyScalar(amountToMovePerSec, actionDuration);
        node.position = CGPointMake(node.position.x+amountToMove.x, node.position.y+amountToMove.y);
        _velocityCat = CGPointMultiplyScalar(direction, CAT_MOVE_POINTS_PER_SEC);

        [self rotateNode:_cat toFace:_velocityCat rotateRadiansPerSec:CAT_ROTATE_RADIANS_PER_SEC];
    }];
}

-(void)spawnCat
{
    _cat = [SKSpriteNode spriteNodeWithImageNamed:@"cat"];
    _cat.position = CGPointMake(ScalarRandomRange(0, self.size.width), ScalarRandomRange(0, self.size.height));
    _cat.name = @"cat";
    [self addChild:_cat];

- (void)update:(NSTimeInterval)currentTime
{
    [self spawningCat];
}

1 个答案:

答案 0 :(得分:1)

使用SKSpriteNode对节点进行类型转换并旋转它。如下所示:

[self enumerateChildNodesWithName:@"cat" usingBlock:^(SKNode *node, BOOL *stop){
    SKSpriteNode *tempCat = (SKSpriteNode *) node; // typecast node
    [tempCat runAction:[SKAction rotateByAngle:1 duration:1]]; // eg: do action with 'tempCat' SKSpriteNode
}];

保持编码..............:)