swift - 如何使用根据节点移动方式运行的 .xscale?

标签 swift sprite-kit skaction sktexture sktextureatlas

我正在使用 SpriteKit 制作一个游戏,其中有一个节点使用以下 SKAction 序列从屏幕的左侧到右侧反复来回移动:

func move(){

    let recursive = SKAction.sequence([
        SKAction.moveByX(frame.size.width/2.8, y: 0, duration: NSTimeInterval(4),
        SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(4),
        SKAction.runBlock({self.move()})])
        doc.runAction(recursive, withKey: "move")
}  

该节点是一个 SKTexture,我使用的是一个名为 WalkRight 的图集。我想做的是,当 SKAction.moveByX(-frame.size.width/2.8, y: 0,uration: NSTimeInterval(4) 运行时,我想使用 .xscale 来旋转我的 walkRight 图像。我无法弄清楚如何将此代码合并到我的 SKAction 序列中。

下面是我添加图集的代码,以防需要它来回答问题。

//In class
var docWalkingFrames: [SKTexture]!

 // general runAction method to make doc walk.
func walkingDoc() {
    doc.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(docWalkingFrames,timePerFrame: 0.3,resize: false,restore: true)),withKey:"walkingInPlaceDoc")
}

//In didMoveToView
  //doc animation walk right
    let docAnimatedAtlas = SKTextureAtlas(named: "WalkRight")
    var walkFrames = [SKTexture]()
    let numImages = docAnimatedAtlas.textureNames.count
    for var i=1; i<=numImages; i++ {
        let docTextureName = "docRight\(i)"
        walkFrames.append(docAnimatedAtlas.textureNamed(docTextureName))
    }

    docWalkingFrames = walkFrames

    let firstFrame = docWalkingFrames[0]
    doc = SKSpriteNode(texture: firstFrame)
    doc.position = CGPoint(x:self.frame.size.width/3.1, y:self.frame.size.height/2)
    doc.size = CGSize(width: 220, height: 470)
    doc.zPosition = 2
    addChild(doc)

walkingDoc()

最佳答案

利用自定义 block 操作,并在该操作中运行检查以查看它是向左还是向右移动,然后相应地调整节点的xScalexScale -1xScale 1 是您将使用的值。

关于swift - 如何使用根据节点移动方式运行的 .xscale?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30957350/

相关文章:

ios - 保持 iAds 横向 SpriteKit/Swift

ios - 将 SKCameraNode 用于通用应用程序 SWIFT

ios - 知道所有 SKActions 何时完成或没有任何运行

swift - 如何从自定义 SKAction Swift 中引用节点

ios - 在 UITableView 中选择具有过滤结果的行时,如何获取实际的数组索引值? swift

ios - 如何在用户平移和移动 map 时将 MKAnnotation View Image 固定在 map 的中心(Swift)?

ios - swift: 根据内部约束调整 UIView 高度

ios - swift ios 中的坐标系错误

ios - 当应用程序进入后台时,DispatchQueue.asyncAfter 的行为如何?