how to have an infinite background

azemurderazemurder Member Posts: 2
edited November -1 in Working with GS (Mac)
i want the back ground to keep moving left and repeating itself after a certain time.. possibly making it go faster, but i can not figure out a way to make that possible...

Comments

  • iPhoneDevForMeiPhoneDevForMe Member Posts: 362
    This can be accomplished with a few simple rules. This will assume you are using the iPhone landscape, but it will work with any orientation with some number adjustments.

    Create a game attribute called "scrollSpeed" and set it to 100.

    Create your background actor and add the image to the actor. In this case we will use a 480 x 320 image. Place the actor on the screen and set its x position to 240 and y to 160 (centered)

    Drag the "Move" behavior into this actor and set its direction to move left, and its speed to game.scrollSpeed.

    Create a rule like so:

    When self.position.x is < 240
    --Spawn actor Bacground
    -- x self.position.x + 480
    -- y 160

    Create another rule like so:

    When self.position.x is < -241
    -- Destroy

    So basically this will make your background actor move left at the preset speed. If you want it to increase or decrease in speed, just change the game attribute "scrollSpeed".

    When your background moves more than half way across the screen, you spawn a new background that is in the exact placement to match up with your current moving actor. This is why we spawn at position.x + 480 instead of just telling it to spawn at 480. When the moving background goes off screen, we destroy it so that it does not keep eating away at memory. We are setting this to happen at -241 so that it will ONLY be destroyed when the actor is COMPLETELY off the screen. This prevents gaps in the background.

    This method works very well from my experience; however, you must be aware that continually spawning actors can affect performance. By destroying the actor when its done, we are helping this performance, but older devices could potentially have problems, but it seems to work just fine on my 3G.

    Hope this helps! If you are confused, ill upload a quick demo!

    -Kyle
  • AfterBurnettAfterBurnett Member Posts: 3,474
    Yeah, better just to move the actor back to the original position, rather than continually spawning new ones ;)

    Have two images side by side... the following is for two 480px wide images:

    Have a move left rule then:

    When self.positionX < or = to -240, change attribute self.positionX to 720 + 240 + self.positionX

    That'll work ;)
  • reddotincreddotinc Member Posts: 653
    Unfortunately it seems due to a recent update that the move behaviour stutters a bit which causes a gap in between the images that you are trying to loop. I tried for ages to fix it, but had to cover that from happening in order for it to look good.
  • AfterBurnettAfterBurnett Member Posts: 3,474
    The way I listed gets rid of the gap. I had the same problem, JCFord gave me that fix. Trust me, it works!
  • reddotincreddotinc Member Posts: 653
    omg are you serious... :(

    I spent HOURS finding a way to make images for the background to cover the gap..

    Thanks for the tip, I'll test it soon.
  • AfterBurnettAfterBurnett Member Posts: 3,474
    Yeah, I did the same, making images too big so they'd overlap only to find the gaps eventually return anyway! Not sure why this little extra bit to the code works but it does!
  • reddotincreddotinc Member Posts: 653
    I have it set up currently so that there is an image behind that fills in the gap when it appears so that it looks perfect, but obviously not a very nice workaround from a dev point of view.

    Will be so glad if this works.
  • AfterBurnettAfterBurnett Member Posts: 3,474
    I know the feeling!
  • FanStudioUKFanStudioUK Member Posts: 459
    Yes, it's working with a 480 wide image, but my question is if I can use a wider image, for example a 1024x160 px ?

    Thanks
  • AfterBurnettAfterBurnett Member Posts: 3,474
    Yup. Up to 1024. Just adjust the values ;)
  • FanStudioUKFanStudioUK Member Posts: 459
    I tried and it's not working, could you suggest the values...? :)
  • AfterBurnettAfterBurnett Member Posts: 3,474
    gah, don't make me do math! lol. Try:

    When self.positionX < or = to -576, change attribute self.positionX to 1600 + 576 + self.positionX

    I don't understand it myself so may not work. LOL. Actually... maybe you can't do it with images that big as I think they get killed when too far off the screen... give it a go anyway.
  • FanStudioUKFanStudioUK Member Posts: 459
    thanks :) i'll try that!

    btw, I love your new game Max Vector! it's made with GS ?
  • AfterBurnettAfterBurnett Member Posts: 3,474
    Yup :) Cheers! Leave a review? hahaha.
  • FanStudioUKFanStudioUK Member Posts: 459
    Of course I will! ;)
  • jstrahanjstrahan Member Posts: 498
    On exp jet I just made the background twice as big as my scrolling direction doesn't have to be but it gave me more room to draw the background so it dissent look as if it was repeating as often then set the background to wrap added a move behavior in the direction I wanted to move and had an attribute for the speed so it could be adjusted by the app
  • reddotincreddotinc Member Posts: 653
    Sorry guys but this is STILL glitchy.. even at 40 speed.. Back to my workaround!
  • DimensionGamesDimensionGames PRO Posts: 993
    POLYGAMe said:
    Yeah, better just to move the actor back to the original position, rather than continually spawning new ones ;)

    Have two images side by side... the following is for two 480px wide images:

    Have a move left rule then:

    When self.positionX < or = to -240, change attribute self.positionX to 720 + 240 + self.positionX

    That'll work ;)

    That wouldn't work for a copter game would it?
  • AfterBurnettAfterBurnett Member Posts: 3,474
    Don't see why not... as long as you don't mind repeating backgrounds... otherwise scroll the camera along a bigger image.
  • DimensionGamesDimensionGames PRO Posts: 993
    The problem I was thinking Is the random spawning of items would then just repeat themselves?
    Would that use a lot of memory?
  • DimensionGamesDimensionGames PRO Posts: 993
    Unless I set them to destroy after 5 seconds and then they could keep be regened'!!!
  • AfterBurnettAfterBurnett Member Posts: 3,474
    With my game I was constantly spawning new enemies... it does take a hit on the CPU but can be done :)
  • forscienCeforscienCe Member Posts: 5
    I cant seem to get this right. What is the calculation for a background of Height: 320px and width 960px?

    Please help out :)
  • MagoNicolasMagoNicolas Member, PRO Posts: 2,090
    I have this problem on ePig Dive and ePig Surf, Tried ALL method possibles.

    But, On Old Devices, It stills has a Gap.
Sign In or Register to comment.