I am trying to learn spritekit by working my way through developing a simple side scrolling game, but I cannot find a clear answer on how to move the hero through the level.

The width of my ground far exceeds the width of the view and I have always thought it was best to move the ground not the hero, but my enemies (who pace back and forth) tend to move much more quickly as the hero moves and to the wrong spot. I am guessing this is because the ground is moving and changing their destination during the move action.

Is moving the ground (not the hero) the best way to create a sidescroller without a repeating landscape. If so, how do I create enemies that move in a restricted area like mario?

Here is a portion of my code:

let enemy = SKSpriteNode(imageNamed: "enemyWalk1.png")

override func didMoveToView(view: SKView) {

    enemy.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "enemyWalk1.png"), size: enemy.size)
    enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
    enemy.physicsBody!.affectedByGravity = true
    enemy.physicsBody!.allowsRotation = false
    enemy.physicsBody!.dynamic = true

    enemy.position = CGPoint(x:CGRectGetMinX(self.frame) + enemy.size.width + 800, y:CGRectGetMinY(self.frame) + ground!.frame.size.height + enemy.size.height)

    var enemyGoTo1 = enemy.position.x + 100
    var enemyGoTo2 = enemy.position.x - 100

    var enemyXScale1 = SKAction.scaleXBy(1, y: 1, duration: 0.0)
    var enemyXScale2 = SKAction.scaleXBy(-1, y: 1, duration: 0.0)

 enemy.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(enemyTextureArray, timePerFrame: 0.15)), withKey: "enemyWalk")
    enemy.runAction(SKAction.repeatActionForever(SKAction.sequence([enemyXScale1, SKAction.moveToX(enemy.position.x + 100,duration: 1.5), enemyXScale2, SKAction.moveToX(enemy.position.x - 100,duration: 1.5), enemyXScale1, enemyXScale2])))


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    for touch in (touches as! Set<UITouch>) {
        let location = touch.locationInNode(self)

        if nodeAtPoint(location) == rightArrow {
                rightTouched = true


override func update(currentTime: CFTimeInterval) {
        if rightTouched == true {
            childNodeWithName("ground")!.position.x = childNodeWithName("ground")!.position.x - 8
  • \$\begingroup\$ I suggest to delete(and move it to another question) the last part(asking about how to make enemies) because the first question is big enough itself so you may get a better answer, also please elaborate your question more with codes and pictures. \$\endgroup\$
    – Iman
    Sep 10 '15 at 18:14
  • \$\begingroup\$ so you want to have fixed landscape, and it should stay there or move very slow? as a parallax? \$\endgroup\$
    – Iman
    Sep 16 '15 at 16:04
  • \$\begingroup\$ I did move it to the game developer site, but did not get a response. I would like the character to move throughout the level by pressing back or forward, so the user determines where he goes. I don't know if I should move the ground or move the character and somehow the view with him. \$\endgroup\$ Sep 16 '15 at 16:07
  • \$\begingroup\$ You shouldn’t move the character toward your scrolling axis, just background should be moved and the landscape should be moved slowly to create a paralax effect, I will post you a refrence to learn from it, this kind of game are known as endless runners. \$\endgroup\$
    – Iman
    Sep 16 '15 at 16:10
  • \$\begingroup\$ So, move the ground and keep the character stationary? \$\endgroup\$ Sep 16 '15 at 16:11

You need to define some classes first

  • Background

  • Enemy

  • Player (your hero)

  • Life Bonus (for example or power up or ….)

  • GameScene,ViewController,appDelegate

and you should define some essential method on them like generating a background for your game on class Background something like this (from the below book)

+ (ERGBackground *)generateNewBackground
    ERGBackground *background = [[ERGBackground alloc]initWithImageNamed:@"background.png"];
    background.anchorPoint = CGPointMake(0, 0);
    background.name = backgroundName;
    background.position = CGPointMake(0, 0);
    background.zPosition = 5;
    background.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(0, 34) toPoint:CGPointMake(background.size.width, 34)];
    background.physicsBody.collisionBitMask = playerCollisionBitmask;
    SKNode *topCollider = [SKNode node];
    topCollider.position = CGPointMake(0, 0);
    topCollider.physicsBody = [SKPhysicsBodybodyWithEdgeFromPoint:CGPointMake(0, background.size.height - 30) toPoint:CGPointMake(background.size.width, background.size.height - 30)];
    topCollider.physicsBody.collisionBitMask = 1;
    [background addChild:topCollider];
    return background;

In background moving you need to create just two set of background in a moment and switch the first to the secound, one when it reaches to the end.

Paralax should also be created in background class

(ERGBackground *)generateNewParallax
    ERGBackground *background = [[ERGBackground alloc] initWithImageNamed:@"parallax.png"];
    background.anchorPoint = CGPointMake(0, 0);
    background.name = parallaxName;
    background.position = CGPointMake(0, 0);
    background.zPosition = 4;
    return background;

Unfortunately the codes are in objective C you have to change them to swift which I that would be very easy.

Please read the following book which is totaly about how to create an endless runner game. It also has some example that you can download and start to change it to have your own. as I see, you hav’t started to code yet, so I think this is a good start for you :

Read This Book

I read this book, it is really a good one and will push you into endless runner games.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .