Eri-Sklii
Eri-Sklii

Reputation: 589

Endless scrolling background

I am trying to get my background to move endless from right to left.

My let:

let background = SKSpriteNode(imageNamed: "background")
let background2 = SKSpriteNode(imageNamed: "background")

my didMoveToView

background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
    background.zPosition = 2

    background2.position = CGPointMake(CGRectGetMidX(self.frame)+(background.position.x/2),CGRectGetMidY(self.frame))
    background2.zPosition = 2

    background.setScale(2)
    background2.setScale(2)

    self.addChild(background)
    self.addChild(background2)

    var backgroundMovement = SKAction.moveByX(background.size.width, y: 0, duration: 1)
    background.runAction(backgroundMovement, completion: {() -> Void in
        self.background.position = CGPointZero
    })

    var backgroundMovement2 = SKAction.moveByX(background2.size.width, y: 0, duration: 1)
    background2.runAction(backgroundMovement2, completion: {() -> Void in
        self.background2.position = CGPointZero
    })}

My update func is empty.

I have uploaded a picture of how it looks like when running on device: http://s18.postimg.org/kbn83tvmx/i_OS_Simulator_Screen_Shot_09_Aug_2015_23_33_39.png

The image is still on half of the screen, and the image does not move now either.

Upvotes: 1

Views: 220

Answers (2)

b26
b26

Reputation: 198

I'm going to copy and paste my code from another game I've made. This code moves a background from right-left forever. I'll do best to explain this code.

func createBackground () {
    var backgroundTexture = SKTexture(imageNamed: "bg")
    var moveBackgroundByX = SKAction.moveByX(-backgroundTexture.size().width, y: 0, duration: backgroundSpeed)
    var replaceBackground = SKAction.moveByX(backgroundTexture.size().width, y: 0, duration: 0)
    var moveBackgroundForever = SKAction.repeatActionForever(SKAction.sequence([moveBackgroundByX, replaceBackground]))

    for var i:CGFloat = 0; i < 3; i++ {
        background = SKSpriteNode(texture: backgroundTexture)
        background.position = CGPoint(x: backgroundTexture.size().width/2 + (backgroundTexture.size().width * i), y: CGRectGetMidY(self.frame))
        background.size.height = self.frame.height
        background.runAction(moveBackgroundForever)
        movingObjects.addChild(background)
    }
}

What you need to do is:

  1. Move background (moveBackgroundByX). This moves the background in the X direction. You need to set a duration.
  2. Replace background1 (replaceBackground).
  3. Create an ACTION forever that moves background1, then replaces it automatically with NO delay.
  4. Run a LOOP that decides how many backgrounds you will need. Maybe you might need more than 3.
  5. Inside that loop, adjust the background.position.
  6. Call runAction on your SKSpriteNode instance.
  7. Add the new SKSpriteNode into your another SKView.

If you have any questions please let me know.

Upvotes: 1

MaxKargin
MaxKargin

Reputation: 1545

The reason your background only takes up half of the screen is because you are using the regular size of the image in pixels. You need to edit the xScale and yScale properties in order to make it make it bigger. What you need is:

background.setScale(2)

Next, to move the background sprites, use SKAction instead since you have more control over timing. The motion within update won't be consistant because, unless you have an algorithm to control exactly when update: is called, you will see that the movement speeds up or slows down. To move the background, use code that looks something like this after your didMoveToView method:

var backgroundMovement = SKAction.moveByX(background.size.x, y: 0, duration: someDuration)
background.runAction(background, completion: {() -> Void in
    //place background image at start again
    background.position = ...
})

Loop this code if you want the background to move continuously and edit it in any way you'd like to make it look better (like having both backgrounds move with SKAction). Hope this helps

Upvotes: 1

Related Questions