Jump to content
Search Community

Can i use delayedCall() to change a variable or a setter?

Ahrengot test
Moderator Tag

Recommended Posts

Hey tweening rock stars!

 

Quick question:

Is there any way to define variables or setter methods with delayedCall() or any of the callbacks in TweenMax/Lite?

 

I mean without creating a function inbetween like this:

TweenMax.delayedCall(1, setVar, [value]);

function setVar($value:*):void
{
   myVar = $value;
}

Link to comment
Share on other sites

interesting ... set up a little test:

public class Main extends Sprite
{
public var myVariable:Boolean = false;
private var _var:Boolean = false;

public function Main()
{
	addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true)
}

private function init(e:Event):void
{
	stage.addEventListener(MouseEvent.CLICK, onClick);
}

private function onClick(e:MouseEvent):void
{
	// TweenMax.to(this, 0, {variable:!_var, delay:1});
	// TweenMax.to(this, 0, {delay:1, onComplete:variable, onCompleteParams:[!_var]});
	// TweenMax.to(this, 0, {myVariable:!myVariable, delay:1, onComplete:trace, onCompleteParams:[string(this + " myVariable: ") + myVariable]});
}

public function set variable($value:Boolean):void
{
	_var = $value;
	trace(this + " variable changed to: " + _var);
}
}

 

With the public variable it works fine:

TweenMax.to(this, 0, {myVariable:!myVariable, delay:1, onComplete:trace, onCompleteParams:[string(this + " myVariable: ") + myVariable]});

 

With the public setter it fails

Test 1:

TweenMax.to(this, 0, {variable:!_var, delay:1});

 

throws:

ReferenceError: Error #1077: Illegal read of write-only property variable on Main.
at com.greensock::TweenLite/init()
at com.greensock::TweenMax/init()
at com.greensock::TweenMax/renderTime()
at com.greensock.core::SimpleTimeline/renderTime()
at com.greensock::TweenLite$/updateAll()

 

Test 2:

TweenMax.to(this, 0, {delay:1, onComplete:variable, onCompleteParams:[!_var]});

 

throws:

1120: Access of undefined property variable.

 

So it seems that ActionScript doesn't completely treat setter methods as public properties, and if i want to do a timed call to a setter i need the function inbetween to modify the setter.

Link to comment
Share on other sites

Those were all expected results. You didn't define a getter (only a setter), so when the tween tried to figure out the current value, your class burped because it won't allow READING that variable. See what I mean? Add a getter and you'll be golden.

 

Oh, and it wouldn't make sense to do this if "variable" is a getter/setter instead of a function:

 

BAD:

TweenMax.to(this, 0, {delay:1, onComplete:variable, onCompleteParams:[!_var]});

 

Because that effectively does this at the end of the tween:

 

variable(!_var);

 

But a getter/setter would need to be set like this:

 

myInstance.variable = !_var;

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