How to increase (animate) the width of the square on both ends?

空扰寡人 提交于 2019-12-18 05:49:07

问题


I have created a square that is 40x40, as shown above. I have a 4x40 strip that I'd like to use to animate (increase) the width of my square till it takes the the width of the whole screen within a second, regardless of the square's position. Quite similar to that of a progress bar loading on both sides.

UPDATE

I forgot to mention that the square is a physics body, hence the physics body must also increase as the sprite increases.


回答1:


What you want to do is use SKAction.scaleXTo to achieve what you are looking for:

SKAction.scaleXTo(sceneWidth / spriteWidth, duration: 1).  

Now if you want the left and right side to not scale evenly, but instead reach both edges at the same time, what you can do is change the anchor point.

The math behind this assumes that your original anchor point is (0.5,0.5)

sprite.anchorPoint = CGPointMake(sprite.position.x / scene.width,sprite.anchorPoint.y)

E.G. Scene size width is 100, sprite is at x 75

What this is basically saying is that your sprite is at some percentage of the scene, in case of the example, 75%. so by changing the anchor point to .75, what is going to happen is the left side will fill faster than the right side when you are expanding your width since the left side of the anchor point has 75% of the width, and the right side has 25% of the width .

Lets say we set the scale to 2, that means the left side of the anchor point will now be at 150%, while the right side will be at 50%.




回答2:


In general, assuming the origin of your objects are in the top-left (or at least the left, since we're only changing things on one axis) if you set start_x to the original x position of your square, start_width to its width, target_x to the x position of your strip, and target_width to its width, then:

x = start_x + (target_x - start_x) * a;

and

width = start_width + (target_width - start_width) * a;

And as a goes from 0.0 to 1.0, x and width will grow to match the strip.

Hope this helps.



来源:https://stackoverflow.com/questions/34768415/how-to-increase-animate-the-width-of-the-square-on-both-ends

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!