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

Fading Script not working

hans

Member
Im trying to get a script that when given coordinates,a speed to fade at, and a room to go to afterwards, will fade the scene into another room. I dont think the for loop is intializing for some reason I cant figure out.
GML:
function fade(x,y,fadeSpeed,roomTarget){
var xx = argument[0];
var yy = argument[1];
var fade_speed = argument[2];
var room_target = argument[3];
var size = obj_camera.max_fov+500;

for(var i = 0; i > 1; i += fade_speed)
{
    draw_set_alpha(i);
    draw_rectangle(xx-size,yy-size,xx+size,yy+size,0);
}
room_goto(room_target);
}
 

chamaeleon

Member
Change i > 1 to i < 1. But this will probably not do what you want anyway. If you want to fade something, it implies time passing. Time passing implies you let steps execute. Time does not pass by having a loop in an event. All this code you have will for all intents and purposes execute at one point in time, after which it goes to another room, without some anticipated fade effect.
 

FrostyCat

Redemption Seeker
Do you know the difference between closed loops and actual time delays?
When NOT to Use Loops

NEVER
check a condition in loop form if you answer "yes" to any of the following:

- Do the actions within need to repeat gradually over time in order to be meaningful?
- Do you need other background processes to continue running while it is repeating? (e.g. speed-based movement, alarms/countdowns, user input, networking packets)
- Do the actions that work toward the stopping condition lie outside the block?
- Does the repeating condition rely on a background process to work properly?
- Does the repeating condition involve real-time user input? (e.g. keyboard, mouse, gamepad and touch screen presses)
The correct action is to spawn a helper like this:
GML:
function fade(_xx, _yy, _fadeSpeed, _roomTarget){
    with (instance_create_depth(_xx, _yy, depth, objFade)) {
        fadeSpeed = _fadeSpeed;
        roomTarget = _roomTarget;
        size = obj_camera.max_fov+500;
    }
}
Then handle the transition in the helper one frame at a time.

objFade Create:
GML:
/* fadeSpeed, roomTarget, size filled by fade() */
image_alpha = 0;
objFade Step:
GML:
image_alpha += fadeSpeed;
if (image_alpha >= 1) {
    room_goto(roomTarget);
}
objFade Draw:
GML:
draw_set_colour(c_black);
draw_set_alpha(image_alpha);
draw_rectangle(x-size, y-size, x+size, y+size, false);
draw_set_alpha(1);
 
Top