• 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!

A moving parallax object in the opposite direction to the player's movement

skofrant

Member
Hi all,

I have a grass object. It's a parallax on the stage. I would like it to move in the opposite direction to the player's movement..
I managed to get it in make, but when I walk the hero, the object grass ends too quickly from the edge as if a piece of it escaped from the board
I have view set to 1280x720
The parallax grass object has dimensions 2558x333


Here is my current code parallax grass object


Draw

GML:
draw_sprite(spr_grassparallax,0,x+view_xview[0]*-0.4   ,y+view_yview[0]  + obj_player.y /-1.5)
 

flyinian

Member
Hi all,

I have a grass object. It's a parallax on the stage. I would like it to move in the opposite direction to the player's movement..
I managed to get it in make, but when I walk the hero, the object grass ends too quickly from the edge as if a piece of it escaped from the board
I have view set to 1280x720
The parallax grass object has dimensions 2558x333


Here is my current code parallax grass object


Draw

GML:
draw_sprite(spr_grassparallax,0,x+view_xview[0]*-0.4   ,y+view_yview[0]  + obj_player.y /-1.5)
Check this video out. Its about Parallax Backgrounds by Shaun Spalding.
 

skofrant

Member
Thanks, Flyinian but I've seen it before.
This is not useful because I use GMS 1.4 and not 2.0 and my parallax is an object that pretends to be a background, not an ordinary background
 

TheouAegis

Member
There's PixelatedPope's video. It doesn't matter if it's for backgrounds, the same rule applies for instances.

What you are currently using is better suited for infinite scrolling of tiled backgrounds.
 

skofrant

Member
But I have an object that is the background, not the background that is the background. I want to get parallax on the object not against the background.
look at the code this is a grass object:-/
I have described everything above.
I have an object and the object draws a sprite and it has to move left right up down in opposite directions to the player.
part of my code is doing here already..but it's running away from the stage or it starts or ends too fast.
 

TheouAegis

Member
It doesn't matter if the background is an object drawing an image or a background image in the room, the principle is exactly the same. And I told you why your code is causing the edge of the sprite to show: your code is intended for tiled backgrounds. You aren't using a tiled background, so you either need to manually tile it in the Draw event, or use the method in PixelatedPope's video.
 

skofrant

Member
I understand. Tell me exactly how to manually set my background?
I can't use the background because I can't set the depth on the background, so I used an object that replaces the background.
When I use the background instead of an object that mimics the background, it covers it with another element, so I use that object because the depth can be set on the object.
I sent the pictures with the description what I mean.





 
Last edited:

TheouAegis

Member
The background in GMS1.4 will always be behind everything else. That's why it's called the background. Each of the backgrounds are drawn in order. So background0 is drawn first, then background1, then background2, then background3, and so on. If a background is marked as a foreground, it will be drawn after everything else, but still in order. Are you saying the background is covering something up that you don't want it to cover up? Then you could try to make whatever you don't want covered up a background also.

To tile a drawn image, you need multiple draw_sprite() or whatever function you're using. To tile horizontally, you need to draw the sprite at (x-sprite_width,y) and (x+sprite_width,y).

If you don't want to use the tiling method, you'll need to use lerp() like PixelatedPope explains at the end of his video.
 

skofrant

Member
We don't understand each other, I don't want to cover another background ... I just have an object which is a textbox and it must be at the bottom of the board.
And right in this place where the textbox is i have and i want this parallax object this grass.
In this way I can then set the depth for these two objects. When I use the foreground background it covers the textbox object.
This object the grass serves as my foreground.
If he wasn't in the foreground, the matter would be obvious ... but that's the problem

i used this function..now is the effect i want to get..the grass object doesn't end and the parallax works in the opposite direction to the player's movement.
there is only one problem ... on the scenery now I have a lot of this grass and there should be only one ...

GML:
draw_sprite_tiled(spr_grassparallax, 0, x+view_xview[0]*-0.8, y+view_yview[0]  + obj_player.y /-1.5);
 

TheouAegis

Member
Don't use draw_sprite_tiled(), just three draw_sprite() calls.
Code:
var xx = -view_xview[0]*0.8;
var yy = view_yview[0] - obj_player.y / 1.5;
var ww = sprite_get_width(spr_grassparallax);
draw_sprite(spr_grassparallax, 0, x - ww + xx, y + yy);
draw_sprite(spr_grassparallax, 0, x + xx, y + yy);
draw_sprite(spr_grassparallax, 0, x + ww + xx, y + yy);
 

skofrant

Member
It works! thank you very much TheouAegis,
How to set the grass object to move inversely on the y axis? because on the x-axis it moves perfectly opposite to the hero's movement, while on the y-axis it moves in the same direction as the hero.


In the picture below I explained it


Would you please explain these code for me? I would like to understand this better for the future?
as a thank you you will be in my game of CREDITS
 
Last edited:

skofrant

Member
what final form should it be to code?


When my hero dies on the stage, this error pops up ...
how to fix it?
obj_player.y does not exist only obj.player but if I remove y, there is no object and parallax does not work:/


GML:
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_grassparallax:

Unable to find any instance for object index '19' name 'obj_player'
at gml_Object_obj_grassparallax_DrawEvent_1 (line 2) - var yy = view_yview[0] - obj_player.y / 1.5;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_grassparallax_DrawEvent_1 (line 2)

I also declared these variables in create;

GML:
var xx = -view_xview[0];
var yy =  view_yview[0];
var ww =  sprite_get_width(spr_grassparallax);
 
Last edited:

skofrant

Member
I thought that too .. will you help me with this? and with that parralax on the y axis? that it moves opposite to the player's movements?


step

GML:
if (instance_exists(obj_player)){
    var yy = (view_yview[0] - obj_player.y);
 

TheouAegis

Member
Code:
If instance_exists(obj_player) {
var xx = -view_xview[0]*0.8;
var yy = view_yview[0] - obj_player.y / 1.5;
var ww = sprite_get_width(spr_grassparallax);
draw_sprite(spr_grassparallax, 0, x - ww + xx, y - yy);
draw_sprite(spr_grassparallax, 0, x + xx, y - yy);
draw_sprite(spr_grassparallax, 0, x + ww + xx, y - yy);
}
You may need to change yy to
var yy = view_yview[0] + obj_player.y / 1.5;
Try both codes.
 

skofrant

Member
Thank you very much, I have one last request for a similar subject ..
I am using this code for background parallax
the problem is that background # 1 is slightly down when I start the game, what can I do to make this background fit perfectly? on both the x and y axis?




step

GML:
background_x[0] = view_xview[0]*0.2
background_y[0] = view_yview[0]*0.2
background_x[1] = view_xview[0]*0.4
background_y[1] = view_yview[0]*0.2

I tried to take example from what you sent me earlier for the background, but parallax does not work.

I would like background 0 and background 1 to move in the same direction as the player.

in the picture below I described which background I have a problem with

 
Last edited:

TheouAegis

Member
To change the direction the background moves, as long as the background is tiled, is negate the number you multiply the view position by.

Do you want background 1 to move up and down as well as left and right, or do you want to just move left and right? If you want to move just left and right, then don't multiply the y position I'm. If you want to move it up and down and left and right, then you will have to add to its y position....um.... view_hview[0]*0.2 or -0.2 I think.
 

skofrant

Member
Yes, I want the backgrounds to move up and down left right, and to keep the background from falling down, could you help me with that? This is the last thing left with parallax: /
I have sceneries and backgrounds in 2560x1440 resolution and my game view is set to 1280x720
these are two background backgrounds 0 outside the window and background 1 main. I would like the backgrounds to move in every direction as the character moves.
did you see what I posted below on the screen? When I start the game, this background is already a little bit down, but it shouldn't.

 
Last edited:

skofrant

Member
Something is not working out for me ... the backgrounds fall ... and they shouldn't: /


step

GML:
background_x[0] = view_xview[0]*0.2 + view_wview[0]*0.2
background_y[0] = view_xview[0]*0.2 + view_hview[0]*0.2

background_x[1] = view_xview[0]*0.2 + view_wview[0]*0.2
background_y[1] = view_xview[0]*0.2 + view_hview[0]*0.2
 

skofrant

Member
GML:
background_x[0] = view_xview[0]*0.2 + view_wview[0]*0.2
background_y[0] = view_yview[0]*0.2 + view_hview[0]*0.2

background_x[1] = view_xview[0]*0.2 + view_wview[0]*0.2
background_y[1] = view_yview[0]*0.2 + view_hview[0]*0.2
still backgrounds fall or run away from the board: - ((
 

Nidoking

Member
Are you sure you should be multiplying all of the terms by 0.2? Surely you'd want just the wview and hview parameters scaled.
 

skofrant

Member
Exactly like that. I would like to scale these two parameters.
I want the backgrounds to move in all directions with the player and to scale well
 

skofrant

Member
I would like the backgrounds not to escape from the board and to move left and right up and down with the right speed following the player's movement.

as described in the picture below

 

skofrant

Member
I mean something like this ... so that the background does not fall ... and moves with the appropriate speed in every direction together with the player.

Step parllax

GML:
background_x[0] = view_xview[0]*0.2
background_y[0] = view_yview[0]*0.4-120

background_x[1] = view_xview[0]*0.4
background_y[1] = view_yview[0]*0.2-120
 

TheouAegis

Member
So if i'm understanding this correctly, you will want to set the background directly to the view's y coordinate (not as a fraction of the y coordinate) and then subtract a fraction of the player's y coordinate from that.

background_y[1] = view_yview[0]+view_hview[0] - obj_player.y*0.4;

I think.
 

skofrant

Member
What I wrote above, it works

GML:
background_x[0] = view_xview[0]*0.2
background_y[0] = view_yview[0]*0.4-120

background_x[1] = view_xview[0]*0.4
background_y[1] = view_yview[0]*0.2-120

And this just makes the background disappear from the board and move up with the player's movement

GML:
background_y[1] = view_yview[0]+view_hview[0] - obj_player.y*0.4;
 

TheouAegis

Member
And falls off because you'd have to adjust the height as drawn at relative to The view. In my case, using +view_hview made the sprite I was using sit on the bottom.

Point of the code I posted is to show that if you wanted to move when the player jumps but still stay position to the bottom of the view, you need to set its y explicitly to view_yview[0]+n and then offset that by a value relative to the player's y. You could possibly offset it further by subtracting view_yview from player's y.

background_y[1] = view_yview[0] - (obj_player.y - view_yview[1])*0.4
 

skofrant

Member
I understand.a tell me ..
For example, I have a background of 2560x1440,
And the window view is 1280x720
This background is not a typical background but one large object, because it wants to set the depths on it
And now like this ... I would like this object to behave as the background, i.e. move left right up down according to the player's movement
could you help me set it up?

Thank you TheouAegis
 
Top