Search code examples
game-maker

cannot change background during game play gamemaker?


I use this on event create to change background during game play. But won't change the background, any idea?

seconds = current_second;

if seconds < 30
{
    background_index[0] = background0;
    background_visible[0] = true;
}

if seconds > 30
{
    background_index[5] = background5;
    background_visible[5] = true;
}

// Set background
background_hspeed[0] = -2;
background_hspeed[5] = -2;

Solution

  • I don't understand your goal, so a few examples:

    Example 1.

    Create event:

    background_index[0] = background0;
    background_index[1] = background1;
    

    Step event:

    seconds = current_second;
    
    if seconds < 30
    {
        background_visible[0] = true;
        background_visible[1] = false;
    }
    else 
    {
        background_visible[0] = false;
        background_visible[1] = true;
    }
    

    Example 2. Same, but using alarm

    Create event:

    background_index[0] = background0;
    background_index[1] = background1;
    
    event_perform(ev_alarm, 0);
    

    Alarm 0 event:

    seconds = current_second;
    
    if seconds < 30
    {
        background_visible[0] = true;
        background_visible[1] = false;
    }
    else 
    {
        background_visible[0] = false;
        background_visible[1] = true;
    }
    
    alarm[0] = room_speed * 30;
    

    Example 3. Other idea...

    Create event:

    background_index[0] = background0;
    background_index[1] = background1;
    background_visible[0] = true;
    background_visible[1] = true;
    
    event_perform(ev_alarm, 0);
    

    Alarm 0 event:

    seconds = current_second;
    
    if seconds < 30
    {
        background_index[0] = background0;
        background_index[1] = background1;
    }
    else 
    {
        background_index[0] = background1;
        background_index[1] = background0;
    }
    
    background_x[0] = 0;
    background_x[1] = 0;
    background_hspeed[0] = 0;
    background_hspeed[1] = -2;
    
    alarm[0] = room_speed * 30;
    

    If you need to change backgrounds many times then you need use step event or alarms, because create event does it only once. Also when you use scrolling, you need remember that after some time x position of the background will be outside a screen, so you can't see background (except cases when your background is tiled).