• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM answer = [SOLVED]; Change background image + Object change sprite

T

TheRBZ

Guest
I am making a game (not to leak too much info) where I need the background to switch image, or just change colour. I try using the room_set_background feature, but it has no effect.

I also need the player object to change sprite from one to another when I press space. Currently, I use the change instance block, but it messes up the scrolling, and it doesn't lock on. I may seem bad, but I have only been on Gamemaker for a week.

Yes, I know it uses Minecraft textures...
normal.PNG
blackglitched.PNG
even more annoying, the player kinda 'moves' a bit when changing colour.

Basically, I want the background to go black, like everything else.
 

jo-thijs

Member
Hi and welcome to the GMC!

room_set_background doesn't work, because it sets the background of a room for when the room gets reloaded.
Before restarting the current room, changes made by room_set_background do not get applied.
Instead, you should change the value of some element of the array background_index.
If you only want to change its color, you could also use background_colour.

Changing instances for changing sprites is not a good way to go indeed.
You should instead just simply change the value of sprite_index to the new sprite
and set image_index to 0 if you want the animation to strt from the beginning.
 
T

TheRBZ

Guest
Hold up, firstly I did say I'm dumb at GML, secondly, Can you give an example where bacground_color is used? How would I make it black? If I made it black, would I be able to make it go back to an image instead of colour?
 
Z

zendraw

Guest
background_index=w/e you want
sprite_index=w/e you want

you can change them as much as you want, for more details check the manual and the other functions you can use to manipulate the background and sprites.
theres also image_index for the images in the sprite.
 
T

TheRBZ

Guest
blacklemon, wow you're such a genius! all I need now is to switch it back. Is there a way to make it 'check' what state it's in, so if it's black, it goes back normal.

I bet that character's smirk is for real now ;)
 

Attachments

F

frumple

Guest
This is for background images, not colors.

In the room editor:
Create two backgrounds for a room. One in slot 0 and one in slot 1. Set both to NOT be visible when room starts.

Next create an object to draw backgrounds. Call it obj_background for example. This is all that the object will do, and there will only be one in the room. In it's create event add a variable bg_index. This variable should have the value 0 or 1 or possibly 2 or 3 if you add more background to a room. You will use it to draw one of the backgrounds.

Create event:
bg_index = 0;

In the Draw event draw the background. This will be at the top left corner of the room unless you change the x,y for some reason.

Draw event:
draw_background( background_index[bg_index] , 0 , 0 );

You could do:
draw_background( background_index[0] , 0 , 0 );

but that would allways draw background 0. With variable bg_index, you can use the same line of code
and still flip through different backgrounds.

Now when you want to change backgrounds, simply change bg_index to a different number. If you have only two backgrounds just doing this will flip between them:

if ( bg_index == 0 )
{
bg_index = 1;
}
else
{
bg_index = 0;
}

If you put that in a keyboard event, the background will change everytime you press a key. You will probably not want to use key presses however, but some game event.
A problem you might run into is accessing the variable bg_index from a different object. You could make bg_index a global variable, but if you only have one instance of object obj_background in the room, you can use it's name to access it's variables.

For example, if you're changing backgrounds from some other object, you could do this:

if ( player_health <= 0 )
{
obj_background.bg_index = 3;
// do other things
}

This would cause obj_background to draw the background in slot 3 if player's health dropped to 0 or below. Normally you have to use instance_id.variable_name = value; in order to change variables in other objects, BUT if there is one and only one such instance present, then using it's name will work. This is because when you use an object name in such a way, you're telling GameMaker to access all instances of this object and change the value of a variable, or do something else to all of them. But there is only one obj_background, so you won't accidentaly modify several instances.

Definitely read up on the functions in the manual. It's a fountain of information. Middle-click on a function in the code editor and the manual will open with a usefull description of the function you clicked on. Very nice.
 
Z

zendraw

Guest
you simply check with if (background==black) {background=white} and vise versa
 
T

TheRBZ

Guest
frumple, your code for me did not work. It only does it in the top left corner. How do I make the whole background black? Well... atleast it toggles.
 
T

TheRBZ

Guest
Ok. So, I got blacklemon's code and added an if statement like frumple's It now works like a charm. I added the if statement to all sprites. Except the player... it still seems to move 4 pixels down and right when I toggle it's sprite.
 
T

TheRBZ

Guest
Oh, I fixed the player sprite moving when swapping color scheme glitch. The white player sprite wasn't centered properly.
 
Top