问题描述:

I am creating an infinite scrolling background for a game that I am creating. when I run it it scrolls vertically. How would I get it to scroll horizontally? This is the code that makes it scroll.

override func update(currentTime: NSTimeInterval) {

background.position = CGPoint(x: background.position.x - 4, y: background.position.y)

background2.position = CGPoint(x: background2.position.x - 4, y: background2.position.y)

//this creates an infinte scrolling background

if (background.position.x < -background.size.width) {

background.position = CGPointMake(background2.position.x + background2.size.width, background.position.y)

}

if (background2.position.x < -background2.size.width) {

background2.position = CGPointMake(background.position.x + background.size.width, background2.position.y)

}

}

网友答案:

This is making the .x lessen by 4 on each pass

background.position = CGPoint(x: background.position.x - 4, y: background.position.y)
    background2.position = CGPoint(x: background2.position.x - 4, y: background2.position.y)

Move that increment to the .y

background.position = CGPoint(x: background.position.x, y: background.position.y-4)
    background2.position = CGPoint(x: background2.position.x, y: background2.position.y-4)

As an extra bonus.... If you want to achieve a sort of paralax effect, you should use a slower increment on the background that is in the back.

网友答案:

background.position = CGPointMake(0.0,0.0);

相关阅读:
Top