GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2009-08-02 12:57:00

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Slide and Fade [Resolved! THANKS!]

Okay, so this is my first attempt at a real script... so be gentle, because I'm sure it looks like a total mess to you more experienced programmers.

Here's the idea.  You put the script in the step event of an object, and it will move towards a specific point.  As it slides between it's starting point and the destination, it fades from one alpha value to another.  I'm using gravity because it's a simple way to have the object accelerate as it slides which creates a nice effect. 

What I've got working so far: The object will slide in the proper direction and will fade from 0 to 1 or 1 to 0  (but the object's existing image_alpha value must already be equal to the starting value... so I'm not really sure why I include that as an argument at all.  Hey, I'll be the first one to admit that this thing needs a lot of help.

What I don't have working: The user can't specify any other alpha values besides 0 or 1... or, rather, they won't really work.  Also, if the alpha value to fade to is 1, the sprite will pop into it's final location after sliding past it for a frame.  And if you want to fade from 1 to 0, it will just keep sliding after it reaches it's destination.

So, it needs a lot of work, and I feel sort of stuck.  Here's the script:

Expand//moveFade(xend,yend,movespeed,alphastart,alphaend)
//place in the step event of the object you want to slide

xend=argument0
yend=argument1
movespeed=argument2
alphastart=argument3
alphaend=argument4

if (alphastart < alphaend && image_alpha >= alphaend) //If fading 0 to 1 (results in the object popping back into place messily)
{gravity=0; vspeed=0; hspeed=0; image_alpha=alphaend; x=xend; y=yend; exit;}
if (alphastart > alphaend && image_alpha <= alphaend) //if fading 1 to 0  (this doesn't work right now)
{gravity=0; vspeed=0; hspeed=0; image_alpha=alphaend; x=xend; y=yend; exit;}

if (alphastart > alphaend) 
{image_alpha=(point_distance(x,y,xend,yend)/point_distance(xstart,ystart,xend,yend));}
else 
{image_alpha=(point_distance(x,y,xstart,ystart)/point_distance(xstart,ystart,xend,yend));}

angle=point_direction(xstart,ystart,xend,yend);
gravity=movespeed;
gravity_direction=angle;

Also, here is an example of it in action.
slideFade() Script test
Again, you have to change the objects initial alpha value in it's create event to sync up with what you want for a starting alpha (0 or 1) because I'm retarded.

Thanks in advance for any helpful criticism you might have.

Last edited by Pixelated_Pope (2009-08-04 12:25:39)

Offline

#2 2009-08-02 13:44:05

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Slide and Fade [Resolved! THANKS!]

I dont like gravity for speed, it's misleading

You can use lerp to set the alpha too, it's much easier that way

Here is a path centric version

if(path_index == -1)
{
    if(variable_local_exists("__path")) path_delete(__path);
    __path = path_add();
    path_add_point(__path,x,y,100);
    path_add_point(__path,argument0,argument1,100);
    path_start(__path,argument2,0,1);
}
//from lerp
image_alpha = (argument3+path_position*(argument4-argument3));



You can now add code in the end path event to destroy to instance (and free the path) if you want.

Last edited by icuurd12b422 (2009-08-02 13:53:34)

Offline

#3 2009-08-03 11:29:13

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Slide and Fade [Resolved! THANKS!]

Ah, I haven't gotten around to playing with path's yet (still really new to GameMaker), but that make's sense.  I'll play with that.  Thanks for the help.

Offline

#4 2009-08-04 12:01:24

Pixelated_Pope
Member
Registered: 2009-07-30
Posts: 24

Re: Slide and Fade [Resolved! THANKS!]

I've never used paths before... and I'm having a bit of trouble figuring this out...  What seems to be happening is your code is creating a two point path, but when it reaches what I thought would be the end of the path, it turns around and heads back to the beginning.  path_position is interpreted as only being .5 at what should be the end of the path.  How do I create a linear path where point 1 is the start and point 2 is the end... not the middle?

EDIT

Nevermind.  I added a path_set_closed(__path,0); and that fixed it.  Thanks again for your help!  The script is now working exactly how I want it to, and I think I learned a little bit.  biggrin

Last edited by Pixelated_Pope (2009-08-04 12:25:13)

Offline

#5 2009-08-04 20:51:43

icuurd12b42
Member
Registered: 2008-12-11
Posts: 303

Re: Slide and Fade [Resolved! THANKS!]

Pixelated_Pope wrote:

I've never used paths before... and I'm having a bit of trouble figuring this out...  What seems to be
Nevermind.  I added a path_set_closed(__path,0); and that fixed it.  Thanks again for your help!  The script is now working exactly how I want it to, and I think I learned a little bit.  biggrin

So that's what was wrong with it.


You know after posting this, I let is run for a while and I too started to notice odd behaviour from the code. The first run looked fine but letting it run for a while the instances started to move wrong. Like path_start with the return to start or rewind when done option but the path_start param was obvioulsy set to "stop" when done.


It's really odd behavior and unexpected. If any one is interested in running my original code. I don't think it's normal and merits a closser look.

Offline

Board footer

Powered by FluxBB