GMS 2 Parallax Scrolling Backgrounds

Discussion in 'Programming' started by HA2ARD_R380RN, Apr 26, 2017.

  1. Hi guys.

    Sorry for the noob question, I have searched google and these forums up and down and can't seem to figure out how to get parallax scrolling backgrounds in GMS2.

    That being, a "further" background layer (sky, mountains, what have you) moving slower than "closer" background layers.

    Anybody know the answer to this?

    I found 1 post I think but it was incomplete and they never got back to explaining how they did it.

    Also, I am using the DND tools. But I assume I can add code anyway so I don't think there should be any problem if you guys only know in code, I'll just drop it anyway. (I mean, I ended up having half of the game I am making "code-like" anyway by this point).

    Thank you in advance.
     
  2. RobbSnow

    RobbSnow Guest

    Give the image an hspeed a negative number. In the step event check to see if it's x value is equal to 0 minus your hspeed. If it is, instance create another one with an x of room_width + 1. Also in the step event see if the x is equal to 0 - the image width. If it is, destroy it because it is fully out of the room.

    Create as many layers as you like with different speeds.
     
  3. Hey, thanks. I know what you are saying in logic, just going to figure out if I also indeed understand how to apply it. On my break now so it might take a while before I get to trying this out, but I will get back to you tell you my results.
    Again, thank you.
     
  4. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,896
    Okay, you need to set the background layers that you want to parallax in the room editor first, and make sure that the "Horizontal Tile" checkbox is flagged for all of them so that they loop while moving. You then need to simply set their layer position relative to the camera view. You WILL need to use some code though, as DnD doesn't have anything for manipulating layers currently. The code itself is fairly straightforward:

    Code:
    // STEP EVENT OF THE PLAYER OR SOME CONTROLLER OBJECT
    var _cam_x = camera_get_view_x(view_camera[0]) ;
    layer_x("parallax_background_1", _cam_x * 0.25); // Change the background layer name to whatever you use in the room editor
    layer_x("parallax_background_2", _cam_x * 0.5);   // Change the 0.25 and 0.5 values to change the speed of the effect
    That code will parallax two backgrounds with the camera used for Viewport[0]. You can add as many backgrounds as you wish too, just change the speed value for each of them...

    Hope that helps!
     
  5. OMG YES! Thank you that was exactly what I was looking for. I had stumbled across some of those functions but I had no idea what was what and neither knew about that whole layer_x part. Perfect. Simple, straight forward, and working great :)

    So to summarise in case I need to adapt this to something else. I am assuming that layer_x is the function that allowed me to manipulate the layers properties correct? And then I just go ahead and give it the layer name, the variable (being the camera value, player value, what have you depending on the situation) and then the math/application what you coding guys call it :p that determines the percentage of that number... more or less?

    (I am really not experienced in code so forgive me if I am making it sound like cave-man explanations, lol).
     
  6. I am assuming that I can also now do this for vertical, I simply need to create a second variable for y and get the y values too right?
     
  7. Nocturne

    Nocturne Friendly Tyrant Forum Staff Admin

    Joined:
    Apr 13, 2016
    Posts:
    6,896
    Yes indeed! Vertical will be the exact same as the horizontal code, only using the "layer_y" function... and you have understood how it works perfectly too. :)
     
    HA2ARD_R380RN likes this.
  8. You're the best. Thnx so much again :)
     
  9. Landonbay

    Landonbay Member

    Joined:
    Jun 1, 2017
    Posts:
    64
    thanks for having this.
    My problem, I still cannot get the parallax to work, even with this code, anything I am doing wrong?
     
  10. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,207
    Please post the code you are using and describe what if anything is happening in your game.

    Have you created the backgrounds in the room editor? What names did you give them?

    Are the backgrounds moving at all etc...
     
  11. Landonbay

    Landonbay Member

    Joined:
    Jun 1, 2017
    Posts:
    64
    I used the same one posted in this fourm (further up)
    I gave the backgrounds the proper names and have adjusted the code to such, it simply just doesn't do anything.
     
  12. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,207
    The code above responds to movement of the active camera in view 0.

    What code are you using to move the camera? (view_camera[0])
     
  13. Landonbay

    Landonbay Member

    Joined:
    Jun 1, 2017
    Posts:
    64

    Im not quite sure what you mean. I have a test player character and the camera currently follows it. so the Dnd that enables player movement.

    I am able to see the paralax now that I switched some backgrounds (one that does not over cover the screen), however, it is infinitely drawing upon itself. not only that only the main background seems to work.
     
  14. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,207
    Sorry, I missed the bit about you using DnD up above.

    In your room settings, are Clear Display Buffer and Clear Viewport Background selected?

    upload_2017-6-14_9-23-6.png
     
  15. Landonbay

    Landonbay Member

    Joined:
    Jun 1, 2017
    Posts:
    64
    The were unselected, so I selected them, it cleared up the glichiness, but now I am back to square 1, the issue of the code on this fourm does not seem to do anything.
     
  16. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,207
    Ok I just made a test project in DnD and I got it to work, but I still had to use code to move the view.

    First, make sure view 0 is enabled and visible

    upload_2017-6-14_11-54-16.png

    Second, modify the code so it looks like this:

    Code:
    camera_set_view_pos(view_camera[0], x - camera_get_view_width(view_camera[0]) * 0.5, y - camera_get_view_height(view_camera[0]) * 0.5)
    var _cam_x = camera_get_view_x(view_camera[0]) ;
    layer_x("parallax_background_1", _cam_x * 0.25); // Change the background layer name to whatever you use in the room editor
    layer_x("parallax_background_2", _cam_x * 0.5); /
    
    This code should be in the step event of the player object or whatever object is moving about that you want the camera to follow.

    When I tried to use the room editor to set an object to follow, it didn't work, so with this code enabled, I disabled the option for the view to follow an object (set Follow Object to none)

    upload_2017-6-14_11-57-0.png

    Anyway, try this out, its working for me, in case anythings not clear, let me know.
     
  17. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    826
    I also had problems coming up with a parallax system in GM:S2, but in the end I came up with a code that worked pretty well.

    I'm not at home, but if you want I can send you example of my code
     
  18. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,703
    Yeah, setting the position to 0.5, 0.25 etc times the view's x coordinate is the way to go.... if it doesn't have the intended effect you're either getting the view's coordinate in the wrong / not working way, or setting the backgrounds' coordinates the wrong/not working way.
     
  19. Landonbay

    Landonbay Member

    Joined:
    Jun 1, 2017
    Posts:
    64

    I tried it out, but still no success, it is not on the test character, and when it moves, the backgrounds do not follow still
    Has it always been this difficult with GMS2?
     
  20. Landonbay

    Landonbay Member

    Joined:
    Jun 1, 2017
    Posts:
    64
    I got it working. turns out, I had the background named wrong, so the program did not work ^^;
     
  21. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    826
    glad it worked out :)

    it's not that hard in GMS2, it's just a different method :)
     
  22. Sorry I was really into the whole game making process along with working for a client at the same time and I haven't had any time for anything else. I completely even forgot to drop by the forum all this time, sorry I wasn't logged on earlier to give my input as well.

    Yeah I found the system to be really easy imo, so I don't think it is hard. I think, just by looking at it, that it might be easier than GMS1 actually.
    Glad you found what was wrong, I was guessing it was probably a typo. Typos are usually the most common of mistakes >.< If I had a penny for every typo in code that I stumbled across later I should be able to pay rent just from typos :p
     
  23. Suffice to say I got it to work the same way for the y axis as well (just substituting all the parts relative with the x axis to a y axis), so the code is sound :) Just need to make sure there are no mistakes in it.
     
  24. jbgood2018

    jbgood2018 Member

    Joined:
    Nov 21, 2018
    Posts:
    2
    Does anybody know if this parallax scrolling solution works as HTML5-export?
    Because for me it works fine until I test the html5-executable. There it didnt work.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice