Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
maurik

create tweens from string [SOLVED]

Recommended Posts

hello!

i have made a tool for controlling objects/properties/methods at runtime. it works fine, but i wish to integrate tweenmax so i could easily control complex things like colorMatrixFilter as well.

it almost works and about half the time it tweens them, but sometimes it doesnt, and it overwrites filters in a funny unpredictable way etc. i was wondering if someone could help me. this is my code:

package {

import flash.display.MovieClip;
import com.gskinner.utils.StringUtils;
import com.greensock.*;

public class test extends MovieClip {

	public var resets:Array = new Array();

	public function control(variable:String, property:String, value:String, time:Number = 0, before:String = ""):void {
		var m = getMC(this, variable);

		if (before != "" && resets[before] == undefined) {
			var pre:Object = new Object();
			for (var i:String in m) {
				pre[i] = m[i];
			}
			resets[before] = pre;
		}

		if (property.indexOf("Filter") != -1) {

			var h:Object = new Object();
			var n:Object = stringToObject(value);
			h[property] = n;
			h["overwrite"] = true;

			TweenMax.to(m, time, h );

		}else if (property in m) {
			if (m[property] is Boolean) m[property] = Number(value);
			else m[property] = value;
			trace(property, ":", m[property]);
		}
	}

	public function getMC(prev:Object, str:String):Object {
		var m:Object;
		if (StringUtils.contains(str, ".")) m = getMC(prev[stringUtils.beforeFirst(str, ".")], StringUtils.afterFirst(str, "."));
		else m = prev[str];
		return m;
	}

	public function stringToObject(tString:String):Object {
		var object:Object = new Object();
		var stripped:String = tString.substr(tString.indexOf('{') + 1);
		stripped = stripped.substr(0, stripped.lastIndexOf('}'));
		var propArray:Array = stripped.split(',');

		for (var i:uint = 0; i < propArray.length; i++) {
			var objectProp:Array = propArray[i].split(':');

			var propName:String = StringUtils.trim(objectProp[0]);
			var propValue = StringUtils.trim(objectProp[1]);

			object[propName] = propValue;
		}
		return object;
	}

	public function reset(variable:String, id:String):void {
		var m = this[variable];
		for (var i:String in resets[id]) {
			m[i] = resets[id][i];
		}
		resets[id] = undefined;
	}
}
}

 

thanks!

PS if anybody finds this code useful feel free, + if anybody can optimize it in any way please let me know!

thanks again,

michael

Link to comment
Share on other sites

Could you make your question a bit more focused and concise? What do you mean by "overwrites filters in a funny unpredictable way"? When exactly do things not work as expected? Better yet, please post an FLA that clearly demonstrates the problem so we can just hit "publish" and see it break. This makes troubleshooting MUCH faster. (the simpler the example the better, and don't forget to zip your files before posting)

Link to comment
Share on other sites

i was wrong. it doesnt overwrite filters, what actually happens is:

the second time i try to tween the same property, it's value is either unpredictably changed or ignored. i have no idea how to explain it, and i can't seem to find any specific pattern it follows. it happens to filters as well as to simple properties.

attached source files, explanation on how to use everything is written on the stage.

many thanks

Link to comment
Share on other sites

It sure looks like the way you're parsing your variables isn't correct but I don't have time to go through all this and debug it right now. When I followed the example text you had, it didn't do anything at all (entering "top.mc1", "rotation", "120", etc.) and when I spent a few minutes looking at the stringToObject() and how propArray is getting populated, it didn't look correct but again, I didn't have time to really dig in and follow everything through and troubleshoot your project. I may have time later to swing back around and look at it, but for now I'd recommend looking over your logic in there. For example, in control(), you do define your vars object as stringToObject(value) but that doesn't really do anything - it doesn't return an object like {rotation:120} because you're just passing 120 into that function.

Link to comment
Share on other sites

the time i pass just "120" in "value" is if i dont use stringToObject. in the other cases i pass a full object (with or without brackets).

 

normal example - doesnt use stringToObject

property "alpha"

value ".5"

 

simple tween example

property ""

value "alpha:.5"

 

think i found something. after the tween is applied, all the properties are reset as if the mc is in native state.

so if i tweened mc1 to rotation 90, in order to tween it back to 0, i have to type -90. same thing happened with the filters - they were all added to previous filters. just no idea why.

 

i uploaded it to http://annacarmi.co.il/control.html. checked there and the above examples work, made a reset button for easier debugging.

 

thanks for taking the time to look into this, meanwhile i'll play around, and ask in other places and post what i find. hope somebody will find a use for it when it works.

Link to comment
Share on other sites

think i found something. after the tween is applied, all the properties are reset as if the mc is in native state.

so if i tweened mc1 to rotation 90, in order to tween it back to 0, i have to type -90. same thing happened with the filters - they were all added to previous filters. just no idea why.

 

That's because you're passing the values as Strings instead of Numbers. There's a feature of TweenLite/Max that allows you to define relative amounts by casting the values as Strings. It's very useful actually. So if you want to define absolute values, make sure they are Numbers.

 

//let's set rotation initially to 45
mc.rotation = 45;

//tweens mc.rotation to exactly 90
TweenLite.to(mc, 1, {rotation:90}); 

//tweens mc.rotation to whatever it is currently plus 90, so in this example it will end up at 135
TweenLite.to(mc, 1, {rotation:"90"});

//tweens mc.rotation to whatever it is currently minus 90, so in this example, it will end up at -45
TweenLite.to(mc, 1, {rotation:"-90"});

 

Remember, to cast your value, all you need to do is wrap it in Number(), like:

TweenLite.to(mc, 1, {rotation:Number(myValue)});

Link to comment
Share on other sites

thank you so much, works perfectly now.

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.
×