SKSpriteNode ScaleUpFrom?

SKSpriteNode ScaleUpFrom?

问题

放大 2D 图像,但是从图像中心点开始放大。我需要它从特定坐标放大吗?

[backgroundImage setScale:rifleZoom];

我目前放大图像的技术。

我需要从中心屏幕而不是中心图像放大

现在我在屏幕@中心放置步枪射击的方式是这样的:

CGPoint positionNow = CGPointMake(backgroundImage.position.x, backgroundImage.position.y);
CGPoint positionPrev = CGPointMake(0.5, 0.5);

float xdiff = positionNow.x - positionPrev.x;
float ydiff = positionNow.y - positionPrev.y;

CGPoint newPositionOne = CGPointMake(0.5- xdiff, 0.5 - ydiff);
newPositionOne = CGPointMake(newPositionOne.x/rifleZoom, newPositionOne.y/rifleZoom);

但是,现在无论图像缩放多少,它都能完美运行。我似乎无法将其实现为从相对于图像中心的屏幕中心向上缩放图像。

我试过的

我试过在放大图像之前更改图像的位置。与 newPositionOne 同样的观点,但是,这不起作用或没有正确完成。

编辑

这会缩放并将中心带到屏幕中心点,或者完全混乱。不能即兴决定它在做什么。

CGPoint positionNow = CGPointMake(backgroundImage.position.x, backgroundImage.position.y);
CGPoint positionPrev = CGPointMake(0.5, 0.5);

float xdiff = positionNow.x - positionPrev.x;
float ydiff = positionNow.y - positionPrev.y;

CGPoint newPositionOne = CGPointMake(0.5- xdiff, 0.5 - ydiff);
newPositionOne = CGPointMake(newPositionOne.x/rifleZoom, newPositionOne.y/rifleZoom);


double xPosition = newPositionOne.x / backgroundImage.size.width;
double yPosition = newPositionOne.y / backgroundImage.size.height;

CGPoint prevAnchorPoint = backgroundImage.anchorPoint;
backgroundImage.anchorPoint = CGPointMake(xPosition,yPosition);

double positionX = backgroundImage.position.x + (backgroundImage.anchorPoint.x - prevAnchorPoint.x) * backgroundImage.size.width;
double positionY = backgroundImage.position.y + (backgroundImage.anchorPoint.y - prevAnchorPoint.y) * backgroundImage.size.height;

backgroundImage.position = CGPointMake(positionX,positionY);


[backgroundImage runAction:[SKAction repeatAction:[SKAction scaleTo:rifleZoom duration:1.0] count:1]];

您可以使用背景节点的锚点属性来更改图像缩放的起点。默认情况下 anchorPoint 位于 (0.5,0.5)。这表示节点的中心。如果你把anchorPoint设为(0,0),那么它会移动到左下角。

anchorPoint : Defines the point in the sprite that corresponds to the node’s position. You specify the value for this property in the unit coordinate space. The default value is (0.5,0.5), which means that the sprite is centered on its position.

当你移动anchorPoint时,你必须再次调整背景位置以抵消由于改变anchorPoint造成的移动。

所以你可以使用,

CGPoint xPosition = convertedPoint.x / background.size.width
CGPoint yPosition = convertedPoint.y / background.size.height

CGPoint prevAnchorPoint = background.anchorPoint
background.anchorPoint = CGPointMake(xPosition,yPosition)

CGFloat positionX = background.position.x + (background.anchorPoint.x - prevAnchorPoint.x) * background.size.width
CGFloat positionY = background.position.y + (background.anchorPoint.y - prevAnchorPoint.y) * background.size.height

background.position = CGPointMake(positionX,positionY)