How to move ALL objects in a room?

R

rangers1001c

Guest
I have a 2D endless runner where the player can pick up boosts that increase his/her speed.

The Problem:
When the player picks up a boost, I need all the objects in the room to move towards the left at a faster rate, and eventually slow down as the boost is used up. How do I implement this?

Information that might help:
Currently, all the objects in the room move towards the left at a set speed (Like in any other 2D endless runner).
I'm using alarms to automatically generate objects (like boosts, obstacles, etc).
 

Bingdom

Googledom
Are the objects parented? Because if they are, you can do something like this

Code:
with(Parent) {
    hspeed = -4;
}
Then when the boost runs out, simply just slow everything down

EDIT:
If you want absolutely everything to move to the left, then use 'all'
 
Last edited:

Tthecreator

Your Creator!
Well i suggest doing what Bingdom says.
So either you parent all the objects to an object, for which you can find information here: https://docs.yoyogames.com/source/dadiospice/001_advanced use/more about objects/parents.html

Or you can use a with(all){} construction, using the keyword "all"
Doing this means that every object will change it's speed, including the player and if you use a diffrent object for health or something, that might also move, What you'll have to do then is evalate what objects DONT need to move, and set that up like this:

Code:
//this code is run when the powerup is activated
with(all){
hspeed=-4;
}
player.hspeed=0;
anotherobject.hspeed=0;
alarm[0]=room_speed*2;//saying this allows us to create some code to disable the powerup that will run after two seconds

//in alarm event
with(all){
hspeed=-2;
}
player.hspeed=0;
anotherobject.hspeed=0;
Or we can use a multiplication trick, but we will have to make sure that the power up isn't activated twice
Code:
//create event, initialize some variables
ispowerup=0;//a variable making sure the powerup isn't activated


//this code is run when the powerup is activated
if ispowerup=0 then{
ispowerup=1;
with(all){
hspeed*=2;//we multiply the speeds
}
alarm[0]=room_speed*2;//saying this allows us to create some code to disable the powerup that will run after two seconds
}


//in alarm event
ispowerup=0
with(all){
hspeed/=2;//we divide by 2;
}
 
R

rangers1001c

Guest
Awesome, is there a way to exclude the player object from the 'with(all)' ? I want every object except the player to move to the left, to create the illusion of the player moving to the right
 

Tthecreator

Your Creator!
Awesome, is there a way to exclude the player object from the 'with(all)' ? I want every object except the player to move to the left, to create the illusion of the player moving to the right
Well, rangers, in the first example this is done using the code: "player.hspeed=0" which is outside the with construction.
Alternatively you could check the variable "object_index" inside the with all loop(which Bingdom did), but it think just setting OBJ_player.hspeed=0 is faster. just take the name of your player object and add ".hspeed=0" after it.


In the second example I made, we are multiplying, meaning that if the hspeed of the player is 0 and we multiply it by 2 than the hspeed will still be 0.
Thus we won't have to worry about those objects. Another advantage of this method is that if you have things moving at diffrent speeds, the speeds will scale correctly.
 
R

rangers1001c

Guest
Thank you for the help guys, I got it boosting the way I want. However, during the boost it doesn't generate any objects, is there a way to fix this?

The alarm event that generates more boosts is as follows:

alarm[0]=35; //reset alarm
h=irandom_range(300,1850); //choose a random height
instance_create(room_width+200,h,obj_boost); //create a boost
 
Top