• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM Parallax Background

S

SkyGuy

Guest
Hello there, nice work for the brand new site !

I have a request :
On the old forum, there was a nice post about parallax background for a side-scrolling game (cf link below)
http://gmc.yoyogames.com/index.php?showtopic=531761

But, as you can see, the link is broken...

I'm currently working on a mobile endless runner game and I'm looking for a script to handle the parallax background.
I have searched here and there, especially on the old GMC, and expect the link above, I didn't find anything good or complete enough...

can anybody help me or explain me what can I do something like this in my game :
http://tutsplus.github.io/2D-Parallax-Scrolling/HTML5/index.html

Thanks in advance

SkyGuy
 
S

SkyGuy

Guest
Hi Alexx,

Yes, I can do what you've done in your example but I would like to generate my background with code only (and not in the room properties...)

@Wintery, Hi ! Thanks for the link, I will check it out asap.
 

Desix

Member
Simply put, background layers should be drawn at a factor of the view position, some layers will follow the view more closely than others.

To make a layer seem closer to the camera you draw it at a very small percentage of the view's x.
x = view_xview*0.1
To make a layer seem further away, you draw it closer to the real position of the view.
x = view_xview*0.8
 
S

SkyGuy

Guest
A quick precision about what I want :
my game is a side scrolling runner game, so every object (obstacles, items, ennemies) has to be created with controller objects.

My character's animation is the master of every background's speed, what it means, if my character's animation is in motion, background 1 goes at the same speed, background 2 is twice slower of faster than the background 1, and so on.
(it's a runner, so, the character must stay at the same position)

If my character's animation is idle, I want my background stops.
Now, my question is :
If I define the background's speed in the room properties, how can I do so that if my character's animation is in motion, the background's speed has a value, and if my character's animation is idle, all the background's speed is set to 0?


I don't know if i'm clear enough
 
Last edited by a moderator:

Desix

Member
It is bad practice to move the background on the character's movement in most cases, its much better to use the view position in the room.

Unless of course if your view does not move...

In your game, do all the objects move around the player, or does the player move around all the objects? I think we need to know more about how your game works. :)
 

Desix

Member
Think I just realised what you mean.

You will need to not add the background in the room editor. Sadly not using the view really complicates things here.

Make a new object called objBackground and set the depth to a very high number.

In the create event add this code:
Code:
offset = 0;
In the end step event add this code:
Code:
if (objPlayer.sprite_index = sprRunning) offset += //whatever the players walking speed is
In the draw event you will need to draw the background like you would draw a sprite:
Code:
draw_background(bgmBackgroundLayer1, offset*0.1, view_yview);
draw_background(bgmBackgroundLayer2, offset*0.5, view_yview);
You will need to tile the background horizontally however there are scripts available online I'm sure.

Then add this object into the room.

Hopefully you understand what's going on here and you can edit the code to work with your game.
 
Last edited:
S

SkyGuy

Guest
@DesixStudios: that's exactly what I need, thank you.

But, yeah, you are right, I have to tile my background horizontally.
I've searched all night a script to do this but I didnt' find anything.

In the GML documentation, there is a "background_htiled[0...7]" function, but it doesn't work
(I try to put the code : "background_htiled[0] = true" (because my ground is a background 0) everywhere (end step event, draw event, create event) with no success....

And I also want to control the delay before I draw my backgrounds (for example: I want to create the background 0 when the game starts and create the background 1 about 30 secondes later), and destroy them when they are outside the room (I never knew how to destroy something you've created with a draw event....)

any idea?

Thanks in advance

@Ehsan: Yeah, the link I put above works, but when you open it, you have a great tutorial on how to make a good responsive parallax background with a broken link to download the project..., that's what I meant....
 
Last edited by a moderator:

Desix

Member
Code:
///draw_background_tiled_horizontal(back,x,y)

    var left, right, step;    
    step = background_get_width(argument0);
    left = view_xview+((argument1-view_xview) mod step)-step;
    right = view_xview+view_wview+step;
    while (left < right)
    {
        draw_background(argument0, left, argument2);
        left += step;
    }
Add this script and replace 'draw_background' in your background object code with 'draw_background_tiled_horizontal'.


It will only draw the backgrounds that are on screen.
 
Top