【问题标题】:SKShapeNode not maintaining center position while applying scaleSKShapeNode 在应用比例时未保持中心位置
【发布时间】:2014-03-16 19:25:02
【问题描述】:

我开始开发一个游戏,有一会儿我需要显示一个增加它的大小的圆圈,我明白了,但问题是它增加了它的大小并改变了它的位置,最后从屏幕上消失了.我希望它保持在其初始位置并从初始中心增加其大小。

我的代码如下:

CGRect circle = CGRectMake(skRand(0, self.size.width), skRand(0, self.size.height), 20, 20);
SKShapeNode *shapeNode = [[SKShapeNode alloc] init];
shapeNode.position = CGPointMake(skRand(0, self.size.width), skRand(0, self.size.height));
shapeNode.path = [UIBezierPath bezierPathWithOvalInRect:circle].CGPath;
shapeNode.fillColor = [SKColor blueColor];
shapeNode.strokeColor = nil;
[self addChild:shapeNode];
SKAction* zoom = [SKAction scaleTo:15.0 duration:10.0];
[shapeNode runAction:zoom];

有什么想法吗?

非常感谢你!

【问题讨论】:

    标签: objective-c ios7 position sprite-kit skshapenode


    【解决方案1】:

    如果您希望您的圈子从中心开始增大,请尝试不使用CGRectMake 而是使用CGMutablePathRef 来构建圈子,创建路径并将圈子的路径设置为您创建的路径。我的游戏中有类似的东西,这对我有用:

    SKShapeNode *turnSphere = [[SKShapeNode alloc] init];
        CGMutablePathRef path = CGPathCreateMutable();
        CGPathAddArc(path, NULL, 0, 0, 30, 0.0, (2 * M_PI), NO);
        turnSphere.path = path;
        turnSphere.position = CGPointMake(220, 440);
    [self addChild:turnSphere];
    
        [turnSphere runAction:[SKAction scaleBy:3 duration:2]];
    

    圆圈现在应该从中心放大。希望这行得通!

    【讨论】:

    • 效果很好!!!非常感谢!那么使用 CGRectMake 和 CGMutablePathRef 有什么区别呢?我想很好地理解解决方案:) 再次感谢!
    • 我很高兴它成功了!!通过使用 CGMutablePathRef,您可以指定圆从中心开始,而通过使用 CGRect,您可以创建一个从您指定的 x 和 y 坐标开始然后向外增长的矩形。这类似于节点的anchorPoint的概念,我认为Apple文档解释得更好:link欢呼!
    • 是的,我读到了,我尝试修改anchorPoint,但我没有实现任何目标.. 非常感谢!!
    猜你喜欢
    • 2010-11-20
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    相关资源
    最近更新 更多