Jump to content
Search Community

Tweening a Value in an Array

cmal test
Moderator Tag

Recommended Posts

Is there a way to tween a value that is not something like x, y, width, or height?

 

As an example:

 

Let's say I have an array:

 

var myArray:Array = new Array;
myArray[0] = 5;

 

I want to change the value of myArray[0] over time, let's say from 5 to 10, and I want the values to tween over time just like the Y value could tween in a typical TweenLite example. Is it possible to do this?

Link to comment
Share on other sites

I should add the reason why I, or someone else, might want to do this.

 

I have an EnterFrame event set up to animate based on the value in an array. And to make this animation smooth it would be easier to use TweenLite.

Link to comment
Share on other sites

Of course - that's precisely what the endArray plugin is for. Please look at the interactive example in the Plugin Explorer. And don't forget to activate it before you try using it.

 

Also, for the record, TweenLite isn't limited to a preset list of property names that it can tween - it will tween ANY numeric property of ANY object. Like TweenLite.to(myCustomObject, 1, {myCustomProperty:300});

 

http://www.TweenLite.com

Link to comment
Share on other sites

Not directly, but there are a few other options:

 

1) Just make the values in the slots of the array that you DON'T want to tween match the values in the target array because endArray will only tween the elements that are different.

 

-OR-

 

2) You could use an onUpdate like:

 

var start:Array = myArray.slice();
var end:Array = [1, 2, 3, 4, 5, 6]; //or whatever - your destination values
TweenLite.to(start, 2, {endArray:end, onUpdate:applyValues});
function applyValues():void {
   myArray[1] = end[1];
   myArray[4] = end[4];
  ...
}

Link to comment
Share on other sites

  • 3 months later...

I'm trying to use TweenLite with an Array but I don't know why it doesn't change me the values...

 

import com.greensock.*;

import com.greensock.easing.*;

 

var myArray:Array = [15, 25, 63, 44, 53, 67]

var end:Array = [1, 2, 3, 4, 5, 6];

TweenLite.to(myArray, 2, {endArray:end,onUpdate:applyValues});

 

function applyValues():void {

trace(end)

}

 

This code returns me:

 

1,2,3,4,5,6

1,2,3,4,5,6

.............

.............

 

and not a sequence of intermediate numbers.

May I have a help, please?

 

Thk,

Fil

Link to comment
Share on other sites

I see two problems:

 

1) You probably forgot to activate the EndArrayPlugin. You need to do that in order for TweenLite to recognize that special property. Activation only needs to take place once in your swf:

 

import com.greensock.plugins.*;
TweenPlugin.activate([EndArrayPlugin]);

 

2) You're tracing the wrong array. You're telling TweenLite to tween myArray, not end. So do this:

 

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.plugins.*;
TweenPlugin.activate([EndArrayPlugin]);

var myArray:Array = [15, 25, 63, 44, 53, 67]
var end:Array = [1, 2, 3, 4, 5, 6];
TweenLite.to(myArray, 2, {endArray:end, onUpdate:applyValues});

function applyValues():void {
   trace(myArray)
}

 

By the way, there's an interactive example that even writes the code for you in the Plugin Explorer which is in all the downloads and on the web site.

 

Happy tweening!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...