使用它的子项调整SKSpriteNode的大小

时间:2013-12-12 07:51:34

标签: ios objective-c sprite-kit skspritenode skaction

我正在尝试在有一个孩子的SKSpriteNode上运行一系列SKActions, 所有动作都在2个形状上一起执行,除了:

[SKAction resizeToWidth:<#(CGFloat)#> height:<#(CGFloat)#> duration:<#(NSTimeInterval)#>]

这是我的代码:

-(void)CreateCards{
for (int i=0; i<3; i++) {
    SKSpriteNode *sprite=[SKSpriteNode spriteNodeWithImageNamed:@"default_circle.png"];
    SKSpriteNode *shape=[SKSpriteNode spriteNodeWithImageNamed:[self GetRandomImage]];
    [sprite addChild:shape];
    sprite.position=CGPointMake(-43, 345);
    sprite.size=CGSizeMake(43, 43);
    shape.size=CGSizeMake(43, 43);


    SKAction *zoom=[SKAction group:[NSArray arrayWithObjects:[SKAction moveTo:CGPointMake(60, 345) duration:0.75],[SKAction resizeToWidth:80 height:80 duration:0.75], nil]];
    zoom.timingMode=SKActionTimingEaseOut;

    SKAction *zoomOut=[SKAction group:[NSArray arrayWithObjects:[SKAction moveTo:[self GetLocationOfIndex:i NumberOfCircles:3] duration:0.5],[SKAction resizeToWidth:43 height:43 duration:0.5], nil]];

    zoomOut.timingMode=SKActionTimingEaseIn;


    SKAction *enter=[SKAction sequence:[NSArray arrayWithObjects:[SKAction waitForDuration:1*i],zoom,zoomOut, nil]];
    [sprite runAction:enter];


    [self addChild:sprite];
}

}

1 个答案:

答案 0 :(得分:9)

要达到预期的效果,您应该使用scaling而不是调整大小。

替换:

SKAction *zoom=[SKAction group:[NSArray arrayWithObjects:[SKAction moveTo:CGPointMake(60, 345) duration:0.75],[SKAction resizeToWidth:80 height:80 duration:0.75], nil]];

使用:

SKAction *zoom=[SKAction group:[NSArray arrayWithObjects:[SKAction moveTo:CGPointMake(60, 345) duration:0.75],[SKAction scaleXBy:80/43 y:80/43 duration:0.75], nil]];

以下代码适用于您的情况:

-(void)CreateCards{
for (int i=0; i<3; i++) {
    SKSpriteNode *sprite=[SKSpriteNode spriteNodeWithImageNamed:@"default_circle.png"];
    SKSpriteNode *shape=[SKSpriteNode spriteNodeWithImageNamed:[self GetRandomImage]];
    [sprite addChild:shape];
    sprite.position=CGPointMake(-43, 345);
    sprite.size=CGSizeMake(43, 43);
    shape.size=CGSizeMake(43, 43);

    CGFloat zoomScale = 80 / 43;
    SKAction *zoom=[SKAction group:[NSArray arrayWithObjects:[SKAction moveTo:CGPointMake(60, 345) duration:0.75],[SKAction scaleXBy:zoomScale y:zoomScale duration:0.75], nil]];
    zoom.timingMode=SKActionTimingEaseOut;

    SKAction *zoomOut=[SKAction group:[NSArray arrayWithObjects:[SKAction moveTo:[self GetLocationOfIndex:i NumberOfCircles:3] duration:0.5],[SKAction scaleXBy:1.0f y:1.0f duration:0.5], nil]];

    zoomOut.timingMode=SKActionTimingEaseIn;


    SKAction *enter=[SKAction sequence:[NSArray arrayWithObjects:[SKAction waitForDuration:1*i],zoom,zoomOut, nil]];
    [sprite runAction:enter];


    [self addChild:sprite];
}
}