Skip to content

class_audio_tween

gl326 edited this page Dec 14, 2021 · 4 revisions

A class_audio_tween is a generally useful tween class which can change the named variable of any struct or class over time.

tween_audio(variable_string_name,dest,timeInSeconds=1,curve=1,isColor=false)

This creates a new tween and returns a pointer to the tween struct which you can use to add additional functions or change its internal variables. This is scoped to the calling object, so as long as the calling instance has access to a variable with the name you provided, it will work.

About "curve":

Curve describes how the value animates from its starting point to its destination. By default, you can leave "curve" as 1 to create a linear animation, which works perfectly fine 90% of the time, especially if you are tweening an audio state like volume.

If you set curve to any value 0 - 10 it will apply a power function to the change (ie. a curve of 1 is linear, whereas a curve of 3 will start changing slowly and then rush to the final volume, and a curve of 0.5 will move quickly at first and then settle at the destination). In place of a number, you can also supply an easing script. A handful of easing scripts are included in Scripts > general > easing with the rest of these resources. The most common one I use is QuadInOut, which has a very smooth in/out animation.


These functions can be applied to an existing class_audio_tween:

.callback(func)

Allows you to provide a function which is run when the tween is completed. The function is scoped to the calling instance, much like the tween variable is. So for example, you could say:

audio_tween("x", room_width+100, 2).callback(function(){instance_destroy();})

This would cause the calling object to move to 100 pixels outside the room over the course of 2 seconds and then destroy itself.

.pause(pauseForTime = -1)

This pauses the tween until it is unpaused later. If you provide a number 0 or greater as an argument, it will pause for that amoutn of time (measured in seconds) and then automatically resume. Calling pause(0) is technically possible but effectively does nothing since it is a pause lasting zero seconds.

.unpause()

This unpauses the tween.

.on_update(func)

This allows you to provide a function--scoped to the calling instance--which runs each time the tween updates. So for example, you could say:

audio_tween("x", room_width+100, 2).on_update(function(){if x>room_width/2{sprite_index = sprScreamingMan; }})

This would cause an object to move to 100 pixels outside the room over the course of 2 seconds, and during that movement, if it is on the right side of the screen it would also change its sprite to be one of a screaming man.

Clone this wiki locally