Read Tween value

1 favourites
  • 3 posts
From the Asset Store
Calculate parabolic trajectories and tween objects over them.
  • The manual says:

    // Value tween from 100 to 200 linearly over 3 seconds

    const t = Tween.startTween("value", 200, 3, "linear", {

    startValue: 100

    });

    // (then read t.value over time)

    Please could anybody provide an example of how exactly I could access this t.value over time in JS. Thank you!

    Ashley, it would be much appreciated if there were an example in the docs (for inexperienced users like me). Thank you!

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • After the tween starts, t.value will have the value the tween is currently in.

    In order to get the changing value over time you need to query that function multiple times during the lifetime of the tween. You can do that by listening for the "tick" event of the runtime.

    Try adding something like this to your main script.

    let t = null;
    
    async function OnBeforeProjectStart(runtime)
    {
    	// Set up a "tick" event listener
    	runtime.addEventListener("tick", () => Tick(runtime));
    	
    	// Get the first Sprite instance
    	// (assumes a Sprite instance already exists in the layout)
    	const sprite = runtime.objects.Sprite.getAllInstances()[0];
    	
    	// Get the Tween behaviour from the sprite
    	// (assumes the Tween behaviour has been added to the instance in the editor)
    	const tween = sprite.behaviors.Tween;
    	
    	// Start the tween
    	t = tween.startTween("value", 200, 3, "linear", {
    		startValue: 100
    	});
    }
    
    function Tick(runtime)
    {	
    	// Check every tick for the state of the tween,
    	// Make sure it has been created and that is hasn't been released before trying to use it
    	if (t)
    	{
    		if (t.isReleased)
    		{
    			// Null the reference to the tween once it has been completed
    			t = null;
    		}
    		else
    		{
    			// Get the value of the tween
    			console.log(t.value);
    		}	
    	}
    }
    

    You can check what is being printed in the console by pressing F12 when the preview window shows up.

  • After the tween starts, t.value will have the value the tween is currently in.

    Thank you, Diego!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)