0
votes

I'm working on a SpriteKit game with animal characters. The animals move left and right across the screen using agents from GameplayKit. They follow an invisible agent that goes from one side of the screen to the other. This works ok except that the sprites are moving upside down when I go from right to left (they're ok when they go from left to right). I've tried changing the xScale to get a mirror image but it looks like the agent is overriding the SKAction I've made to change this (the characters are jumpy on the screen for an instant). Does anyone know how to get the sprite characters to remain upright? My code is triggered in func didBeginContact.

if contact1 is RightWall {
   if contact2 is AnimalNode {
      invisibleNode.position = CGPointMake(0, CGFloat(10 + arc4random_uniform(UInt32(size.height - 20))))
      invisibleNode.agent.position = float2(x: Float(invisibleNode.position.x), y: Float(invisibleNode.position.y))
   contact2.turnLeft() // this triggers the xScale change I've tried directly on the SKNode
}

Any ideas how to fix this?

Update: the code is shown in the original question.

The moving node is prepared in a child of SKNode (class: AnimalNode) with the following function:

override func turnLeft() { 
removeActionForKey("rightTurn") let turnRight2Left = SKAction.sequence([SKAction.scaleXTo(0.3, duration: 0.1), SKAction.scaleXTo(-0.3, duration: 0.1), SKAction.scaleXTo(-1.0, duration: 0.1)]) runAction(turnRight2Left, withKey: "leftTurn") 
}

RightWall is an SKShapeNode set up in GameScene. The contact works ok as I added a print statement to check if the if statements are working. The call to func turnLeft also shows no problems, just nothing changes in the node. Maybe this isn't the best way to flip the character. I'm open to other approaches. The turnLeft func is set-up to make the change in character's orientation look more seamless when it transitions from moving right to moving left (that's why I change the sprite's xScale in stages to give it a more 'animated' feel).

1
show the code that is actually changing the scales, all you have posted is where sprites are on the screen, the texture does not change with the code providedKnight0fDragon
The code I've used to turn the sprite from facing right to left is as follows. I've made an extension with the function turnLeft(). The node is prepared in a child of SKNode with the following function: override func turnLeft() { removeActionForKey("rightTurn") let turnRight2Left = SKAction.sequence([SKAction.scaleXTo(0.3, duration: 0.1), SKAction.scaleXTo(-0.3, duration: 0.1), SKAction.scaleXTo(-1.0, duration: 0.1)]) runAction(turnRight2Left, withKey: "leftTurn") }Shane O'Seasnain
post it in your question with formattingKnight0fDragon
ok I think I understand what you are doing here, you want the guy to shrink in size flip then expand, this code works fine, so I am going to need to see turn right, and how you actually call these functionsKnight0fDragon
Thanks Dragon, it's good to know that the code should work. The project is too large to load into a question, but it sounds like I need to look for something else that's acting on the node's size. I'll come back when I've done some more digging.Shane O'Seasnain

1 Answers

0
votes

thank you both for your help. It took some digging but I found another call to xScale which I was subclassed to. This was over-riding my call and causing the jumping action I'd noticed. I can combine the parent method and solve the problem. Thanks very much for the tips and feedback.