How to use .xscale that runs depending on which way node is moving?

233 views Asked by At

I'm making a game with SpriteKit where I have a node that is repeatedly moving back and forth from the left to the right side of the screen using this SKAction sequence:

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")
}  

The node is an SKTexture, and I'm using an atlas called WalkRight. What I am trying to do, is when the SKAction.moveByX(-frame.size.width/2.8, y: 0, duration: NSTimeInterval(4) is run, I want to use a .xscale to rotate my walkRight images. I can't figure out how to incorporate this code into my SKAction sequence.

Below is my code for adding the atlas in case this is needed to answer the question.

//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()
1

There are 1 answers

2
Wraithseeker On

Make use of a custom block action and in that action, run a check to see whether it's going left or right and then adjust your xScale of the node accordly. xScale -1 and xScale 1 are the values that you will be using.