用它的子元素调整SKSpriteNode的大小

时间:2023-01-12 17:46:08

I am trying to run a sequence of SKActions on a SKSpriteNode which has one child, all the actions are performed on the 2 shapes together except :

我试图在一个有一个子结点的SKSpriteNode上运行一系列skaction,除了:

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

Here is my code:

这是我的代码:

-(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 个解决方案

#1


9  

To achieve the desired effect you should use scaling instead of resizing.

为了达到预期的效果,您应该使用缩放而不是调整大小。

Replace:

替换:

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

With:

:

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

Following code should work for your case:

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

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

#1


9  

To achieve the desired effect you should use scaling instead of resizing.

为了达到预期的效果,您应该使用缩放而不是调整大小。

Replace:

替换:

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

With:

:

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

Following code should work for your case:

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

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