简体   繁体   English

Swift中SpriteKit颜色填充动画

[英]SpriteKit color fill out animation in Swift

I have one image of a drop as below: 我有一个图像的下降如下:

在此处输入图片说明

I want to have its level of color decrease with time and with animation. 我希望其颜色级别随时间和动画而降低。 Currently, I am able to animate it with the sequence of images. 目前,我可以使用图像序列对其进行动画处理。

But I want to use this in sprite kit and animate. 但是我想在Sprite Kit中使用它并设置动画。 So how can I do this in sprite kit? 那么我该如何在Sprite Kit中做到这一点?

I have some idea to do it with SKTextureAtlas. 我有一些想法可以通过SKTextureAtlas完成。 Is it the right way to do it? 这是正确的方法吗?

Also is there any tool to generate multiple images for the textures? 是否有任何工具可以为纹理生成多个图像?

You should always try to show some code on Stackoverflow, otherwise people tend to not help. 您应该始终尝试在Stackoverflow上显示一些代码,否则人们往往会无济于事。

In SpriteKit you would do it using SKTextureAtlas like you assumed. 在SpriteKit中,您可以像假设的那样使用SKTextureAtlas进行操作。

1) Go to your asset catalogue and click + and select new sprite atlas from the drop down menu. 1)转到资产目录,然后单击+,然后从下拉菜单中选择新的Sprite地图集。 Call it something like DropAtlas. 称它为DropAtlas。 Add as many image sets as you need within that atlas, label each image set something like this 在该地图集中添加所需数量的图像集,对每个图像集进行标签,如下所示:

dropImage_1
dropImage_2 
dropImage_3 

2) Create a helper class for your textures, this way you only need to preload your textures once. 2)为纹理创建一个帮助器类,这样,您只需要预加载纹理一次。 You can easily extend this class for other texture animations you might use. 您可以轻松地将此类扩展为可能使用的其他纹理动画。 This will also help with performance. 这也将有助于提高性能。

 class TextureHelper {

       static var dropTextures = [SKTexture]()

       static func setup() {

           // Drop textures
           let dropAtlas = SKTextureAtlas(named: "DropAtlas") // or the name you gave the atlas in step 1
           for image in 1...3 { // how many images there are in the animation
               let texture = dropAtlas.textureNamed("dropImage_\(image)") // or the name you gave the image sets
               dropTextures.append(texture)
           }
      }
 }

3) Than call the setup method when your app launches eg App Delegate or GameViewController. 3)不要在应用启动时调用设置方法,例如App Delegate或GameViewController。

TextureHelper.setup()

4) And finally in your scene you animate the node like so. 4)最后,在场景中像这样对节点进行动画处理。

 private let dropAnimationKey = "DropAnimationKey"

 class GameScene: SKScene {

  let dropNode = SKSpriteNode(imageNamed: "dropImage_1") // defaults to image 1

  override func didMove(to view: SKView) {
      dropNode.position = ...
      addChild(dropNode)

      startDropNodeTextureAnimation()
  }

  private func startDropNodeTextureAnimation() {
       guard !TextureHelper.dropTextures.isEmpty else { return } // so you dont crash incase texture array is empty for some reason
       let textureAnimation = SKAction.animate(with: TextureHelper.dropTextures, timePerFrame: 0.3)
       dropNode.run(SKAction.repeatForever(textureAnimation), withKey: dropAnimationKey)
   }

 }

To stop the animation you can simple remove the action with the correct key 要停止动画,您可以使用正确的键简单地删除动作

dropNode.removeAction(forKey: dropAnimationKey)

I am not sure what a good tool is for creating different images. 我不确定用于创建不同图像的好工具。

Hope this helps 希望这可以帮助

The way I would do this is have two images; 我这样做的方法是有两个图像。 the white outline as png with the center cut out and the blue water as a separate image behind it. 白色轮廓为png,中心被切掉,蓝色水作为单独的图像位于其后。 Set them in a view that has clipToBounds turned on so you wont see overflow of the blue image. 将它们设置在启用clipToBounds的视图中,这样就不会看到蓝色图像的溢出。

Then use an animation around setting the new value, somewhat like this: 然后使用动画设置新值,如下所示:

UIView.animate(withDuration: 0.3, animations: {
    waterImage.center.y = newOffset
}, completion: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM