SKSpriteNode动画,其中动画中的每一帧都不居中
SKSpriteNode animation where each frame in the animation is not centered
我有一个 SKSpriteNode
,大小为 120px x 120px。
精灵的默认纹理只是一个半径为 60px 的圆。
我想为精灵设置动画:
SKAction.animate(with:timePerFrame:resize:restore:)
问题是作为动画一部分的每个纹理的大小都小于 120px x 120px,因此如果我这样做并将精灵锚点设置为
CGPoint(x: 0.5, y: 0.5)
动画帧将以精灵的中间为中心,这不是我想要的正确动画。我希望动画如下:
这意味着我必须将 sprite atlas 中的所有动画纹理添加为 120px x 120px,对吗?如果是这样,这将导致浪费的地图集 space 具有空像素。有没有更好的方法来实现这一目标?即指定动画中每个纹理帧的中心点?
提前致谢。
你应该也可以试试:
关注resize = true
如果 true,精灵会调整大小以匹配每个新纹理。如果false,精灵的大小保持不变
如果您需要更多官方详细信息,请查看 here。
关于您对以下评论的请求,您可以 re-create SKAction.animate
例如进行如下扩展:
extension SKAction
{
static func animate(withMyTextures textures:[texture:SKTexture], timePerFrame:TimeInterval ,resize:Bool, restore:Bool) ->SKAction {
var originalTexture : SKTexture!
let duration = timePerFrame * Double(textures.count)
return SKAction.customAction(withDuration: duration)
{
node,elapsedTime in
guard let spriteNode = node as? SKSpriteNode
else
{
assert(false,"animateWithMyTextures only works on members of SKSpriteNode")
return
}
let index = Int((elapsedTime / CGFloat(duration)) * CGFloat(textures.count))
//If we havent assigned this yet, lets assign it now
if originalTexture == nil
{
originalTexture = spriteNode.texture
}
if(index < textures.count)
{
spriteNode.texture = textures[index].texture
}
else if(restore)
{
spriteNode.texture = originalTexture
}
if(resize)
{
spriteNode.size = spriteNode.texture!.size()
}
}
}
}
我有一个 SKSpriteNode
,大小为 120px x 120px。
精灵的默认纹理只是一个半径为 60px 的圆。
我想为精灵设置动画:
SKAction.animate(with:timePerFrame:resize:restore:)
问题是作为动画一部分的每个纹理的大小都小于 120px x 120px,因此如果我这样做并将精灵锚点设置为
CGPoint(x: 0.5, y: 0.5)
动画帧将以精灵的中间为中心,这不是我想要的正确动画。我希望动画如下:
这意味着我必须将 sprite atlas 中的所有动画纹理添加为 120px x 120px,对吗?如果是这样,这将导致浪费的地图集 space 具有空像素。有没有更好的方法来实现这一目标?即指定动画中每个纹理帧的中心点?
提前致谢。
你应该也可以试试:
关注resize = true
如果 true,精灵会调整大小以匹配每个新纹理。如果false,精灵的大小保持不变
如果您需要更多官方详细信息,请查看 here。
关于您对以下评论的请求,您可以 re-create SKAction.animate
例如进行如下扩展:
extension SKAction
{
static func animate(withMyTextures textures:[texture:SKTexture], timePerFrame:TimeInterval ,resize:Bool, restore:Bool) ->SKAction {
var originalTexture : SKTexture!
let duration = timePerFrame * Double(textures.count)
return SKAction.customAction(withDuration: duration)
{
node,elapsedTime in
guard let spriteNode = node as? SKSpriteNode
else
{
assert(false,"animateWithMyTextures only works on members of SKSpriteNode")
return
}
let index = Int((elapsedTime / CGFloat(duration)) * CGFloat(textures.count))
//If we havent assigned this yet, lets assign it now
if originalTexture == nil
{
originalTexture = spriteNode.texture
}
if(index < textures.count)
{
spriteNode.texture = textures[index].texture
}
else if(restore)
{
spriteNode.texture = originalTexture
}
if(resize)
{
spriteNode.size = spriteNode.texture!.size()
}
}
}
}