SpriteKit

Hi I am currenltly learning swift and following a tutorial to make a Flappy Bird Game,


There is one change I would like to make which is I want the pipes to spawn from the left to right rather than the right to left.

What would be the best way to be able to do so? I tried to change the SKAction to "distanceToMove" by taking away the negative but once I do that, no pipes spawn at all. (line 131)


Many thanks in advance!




/

pipeTextureUp = SKTexture(imageNamed: "PipeUp")

pipeTextureUp.filteringMode = .nearest

pipeTextureDown = SKTexture(imageNamed: "PipeDown")

pipeTextureDown.filteringMode = .nearest

/

let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeTextureUp.size().width)

let movePipes = SKAction.moveBy(x: -distanceToMove, y:0.0, duration:TimeInterval(0.008 * distanceToMove))

let removePipes = SKAction.removeFromParent()

movePipesAndRemove = SKAction.sequence([movePipes, removePipes])

/

let spawn = SKAction.run(spawnPipes)

let delay = SKAction.wait(forDuration: TimeInterval(1.5))

let spawnThenDelay = SKAction.sequence([spawn, delay])

spawnThenDelayForever = SKAction.repeatForever(spawnThenDelay)


pipeSlaveNode.run(spawnThenDelayForever)


Accepted Reply

Well, when you create your pipe, you will need to tell SpriteKit where to create it. If you don't, it will create it at the origin, (0,0), wherever that is in your scene.


Probably, you just need to do something like this:


let pipeNode = PipeNode()    // or whatever you do to create the pipe sprite
theScene.addChild( pipeNode )  // add it to the scene so it can be seen
pipeNode.position = CGPoint( x: -200, y:0 )  // initially place 200 points to the left of the scene origin


The actual point position will depend on things like the size of your pipe, how far offscreen you want it, where its anchor point is, the size of your scene, the scaleMode of your scene, and the position, rotation, and scale of all the nodes up the parenting chain that the pipe node will be placed under.


Also, note that a single, fixed number may not cut it. You may have to do some computing to determine the exact edge of your screen, unless you have chosen a scaleMode that makes the width of your visible play area known (that would be aspectFit in a portrait orientation). And you might have to deal with "letterboxing" issues if you play your game on a device with a tall-and-skinny aspect ratio. The tutorial you're following has probably already picked some values for the scene size and the scaleMode, but if you have trouble with these issues, you'll want to read up on SKScene and its scaleMode in the Apple documentation on SpriteKit.

Replies

Are you sure no pipes are spawning, or could it be that you're not adding them to the scene, or that they are spawning offscreen and never moving onscreen?


Looking at your code, I don't see any code to actually create or place pipeSlaveNode into the scene, so it's hard to know for sure what the issue is.


Presumably, the original tutorial spawns the pipes offscreen on the right, and then animates them to the left. If all you do is change the distance to move, then what it will do is spawn the pipe offscreen on the right, and then move it in the opposite direction, which is even further offscreen to the right. You may just need to make sure that the pipe spawns offscreen to the left in addition to changing the distance it moves.

Thats correct as normal the Pipes are spawning off screen on the right and anamating to the left.

I have tested it a few times on the simulator and it is spawning the however when I change the distance to move I cannot tell if it is spawning on the left.


How can I ensure that pipe is being spawned off screen to the left and then be anamated to the right?

Well, when you create your pipe, you will need to tell SpriteKit where to create it. If you don't, it will create it at the origin, (0,0), wherever that is in your scene.


Probably, you just need to do something like this:


let pipeNode = PipeNode()    // or whatever you do to create the pipe sprite
theScene.addChild( pipeNode )  // add it to the scene so it can be seen
pipeNode.position = CGPoint( x: -200, y:0 )  // initially place 200 points to the left of the scene origin


The actual point position will depend on things like the size of your pipe, how far offscreen you want it, where its anchor point is, the size of your scene, the scaleMode of your scene, and the position, rotation, and scale of all the nodes up the parenting chain that the pipe node will be placed under.


Also, note that a single, fixed number may not cut it. You may have to do some computing to determine the exact edge of your screen, unless you have chosen a scaleMode that makes the width of your visible play area known (that would be aspectFit in a portrait orientation). And you might have to deal with "letterboxing" issues if you play your game on a device with a tall-and-skinny aspect ratio. The tutorial you're following has probably already picked some values for the scene size and the scaleMode, but if you have trouble with these issues, you'll want to read up on SKScene and its scaleMode in the Apple documentation on SpriteKit.

Hey Thank you so much


that was exactly what I had to do, it took me so long to try find the answer for that so honestly thank you! 🙂