Jump to content
Search Community

Failure in composing a dynamic tween

azpaul test
Moderator Tag

Recommended Posts

Hi.

 

I am new to working with TweenMax, AS 3 and CS5 Flash but I think I am making some pretty good headway. Lots of stalls along the way but I am getting closer to my goal.

 

I have an XML file that holds 11 number variables that I will use through out my movie. I am thinking that it is easiest if I just assign the tween motion effect to the symbols as they get loaded into the array.

 

The variable that holds the numbers is numberDraw. An example of the symbol names are num2 and num2Stroke. As the name implies, the symbol is a number 2 and the number 2 with a stroke effect on it.

 

The xml data that I am using here is say 2 and 58. So, when 2 is populated in the array, the TweenMax object below should be num2Stroke, correct?

 

I can use this syntax to make the symbol num2Stroke visible

"num" + numberDraw + "Stroke".visible = true;

 

The symbol shows on the stage with no problems but I get this error at compile 1050 Cannot assign to a non-reference value.

 

I am thinking that I should be able to write a function that loads these variables into the tweenMax effect like this but when I use it,

TweenMax.to("num"+numberDraw+"Stroke", 1, {z: -200, yoyo: 1, repeat: 1, delay: 2});

The object should be interpreted as num2Stroke

 

I get an error at compile of Line 76 1050: Cannot assign to a non-reference value.

 

How can I go about calling the function to have an effect on each number in the array and have it dynamically apply to the appropriate symbol?

 

I can use the delay method at the end of the statement to have each animation wait X seconds. I should be able to assign a counting variable to that as well so it increments with each value in the array.

 

Any ideas?

 

Thanks so much!

 

import flash.utils.Timer;
import flash.events.TimerEvent;
import com.greensock.TweenMax;
import com.greensock.easing.*;
import com.greensock.plugins.VisiblePlugin;

var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
var fields:Array;

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

//A bit of error handling in case the XML goes missing
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlError);

xmlLoader.load(new URLRequest("data.xml"));

//numbers = new Array(num1, num2);
fields = new Array("one", "two");

function xmlError(e:IOErrorEvent):void {
		//Just letting the user know something's screwed			
}




function LoadXML(e:Event):void {

xmlData = new XML(e.target.data);
ParseContent(xmlData);

}

function ParseContent(contentInput:XML):void {

trace("XML Contents");
trace("------------------------"); 

var numberList:XMLList = contentInput.numberdraw;
//var gameNumber:XMLList = contentInpute.gamenumber;

//trace ('length is ' + numberList.length());
trace('Game Number is ' + contentInput.gamenumber);
gnumber.text= contentInput.gamenumber;

//var numTween:Tween = new Tween (num2Stroke, "z", Strong.easeIn,-250, 0, 30, false);
//numTween.addEventListener(TweenEvent.MOTION_FINISH, doSomething);

 for (var i:int = 0; i < numberList.length(); i++){
	//trace ("I is " + i);
	//trace ("List Length is " + numberList.length());

	var numberDraw:XML = numberList[i];

	trace (' Number Draw is  ' + int(numberDraw));
	trace ("num"+numberDraw);

	//"num"+numberDraw+"Stroke".visible = true;	

	var tweenObj:String;
	tweenObj= "num" + int(numberDraw);
	trace ("tweenObj is  " + tweenObj);


	myTween();
	//num2Stroke.visible =true;
	//"num"+int(numberDraw) + "Stroke".visible = true;




	function myTween(): void {
		//TweenMax.to("num"+numberDraw+"Stroke", 1, {z: -200, yoyo: 1, repeat: 1, repeatDelay: 0});
		trace ("num" +int(numberDraw) + "Stroke");
		"num" + int(numberDraw) + "Stroke".visible = true;

	}

}

}

Link to comment
Share on other sites

You're using malformed syntax - you need to define the scope of the variable you're trying to find. So assuming your variable has a scope of "this", you'd wrap it in this[] like:

 

TweenMax.to(this["num"+numberDraw+"Stroke"], 1, {z: -200, yoyo: 1, repeat: 1, delay: 2});

Also, be very careful about using nested functions. They're generally considered a bad practice because they get deleted as soon as the parent function finishes running. This can cause all sorts of problems.

Link to comment
Share on other sites

Hi GreenSock...

 

OK, I guess I am missing something here. You mentioned that I have malformed syntax. Can you explain what that is and where in my code it is malformed? I read up on it and from what I can tell, it refers to undeclared variables. I have declared all the variables I use. I am thinking that this may be my underlying issue?

 

So I used your code and it works but when I place it into a function so I can call the tweens dynamically, I get this error

TypeError: Error #1009: Cannot access a property or method of a null object reference.

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()

 

So I trace the variable like this in the function and it comes back as undefined which is causing it to be null??

trace(this["num"+numberDraw+Stroke"]);

 

So my real question now is why does this variable become undefined within the function but not outside the function?

 

Here is the function code.

function myTween(): void {
        //TweenMax.to(this["num"+numberDraw+"Stroke"], 1, {z: -200, yoyo: 1, repeat: 1, repeatDelay: 0});
        trace (this["num" +numberDraw+ "Stroke"]);
        //"num" + int(numberDraw) + "Stroke".visible = true;
      TweenMax.to(this["num"+numberDraw+"Stroke"], 1, {z: -200, yoyo: 1, repeat: 1, repeatDelay: 0});        
     }

 

Once I get past this part, then I can continue to move on. I have been stuck here for about a day trying to figure this out.

 

Also, I have a separate piece of code that I used to program all the tweens using timelineMax and the animation is so smooth. I was able to use the bezier tween along with this one to get the effects I wanted. I have to agree that once you understand it, the possibilities are phenomenal. In October, I plan on signing up for your $99.00 membership! I have some ideas for the splittext feature.

 

Thanks again for all your help. I really appreciate it!

Link to comment
Share on other sites

Just to clarify, these two traces produce the same result:

 

var numberDraw:Number = 2;
this.num2Stroke = new Sprite();
trace(this["num"+numberDraw+"Stroke"]); //traces [sprite]
trace(this.num2Stroke); //traces [sprite]

 

Without seeing your FLA or class file, I can't say for sure what the problem is, but here are some possibilities:

 

1) You didn't define numberDraw properly, so when you do this["num"+numberDraw+"Stroke"], it's null because numberDraw is NaN.

 

2) The numberDraw is a valid number, but you don't have a variable or property that's named accordingly. For example, if numberDraw is 2 and you don't have a num2Stroke variable/property defined.

 

3) You defined numberDraw as a local variable in some other function. Local variables are only available inside the function where they're defined and they get deleted as soon as their parent function finishes running. They do not persist. So this would break:

 

function myFunction1():void {
   var numberDraw:Number = 2;
   myFunction2();
}
function myFunction2():void {
   trace(numberDraw); //undefined!
}
myFunction1();

 

By the way, glad to hear you're planning on joining Club GreenSock in a few months. I think you'll dig all the bonus stuff, and I appreciate your support.

Link to comment
Share on other sites

Hi GreenSock,

 

Your response opened some valid possibilities as to what I am doing wrong. I will look into them more closely.

 

I have not worked with sprites but I will look at this as well.

 

Thanks for explaining the functions and how the variables get deleted once the function is completed. That may explain part of the current issue.

 

Please check out my pm, but the way I have my layout is my main fla file includes 2 .as files (what are these considered when you include from a separate .as file? Class, Container etc...) , get_numbers2.as and timer.as.

 

I have made a few changes in the get_numbers2.as file since my last post. I created a getData function that I call from the timer.as once the timer reaches 00:00. The timer function will be called the same amount of times as the number of variables in data.xml.

 

The sequence of events goes like this.

1. The main fla file kicks off and shows the main stage. I have all number symbols, timer and status visible.

2. The timer.as function counts down from a predetermined time, it will be 5 minutes when I get done.

3. When the timer reached 00:00 the timer is no longer visible and calls the getData function in get_numbers2.as.

4. The ball symbol, num2Stroke symbol is visible, num2 symbol is no longer visible and a background image becomes visible.

5. There is a tween action on the ball that follows a bezier curve to the .x and .y values of num2. This ends the ball ovver the num2 symbol. This will eventually be dynamic which matches each number in the XML file.

6. There are more tween actions for num2Stroke. This needs to be dynamic because I will be using this to tween the appropriate numXStroke symbols on the stage that match the xml data.

7. This routine will repeat for as many numbers in the xml dtata file. There will be 10 or 20 all together.

8. At the end of the last tween, the timer appears and and we wait another 5 minutes for the next round.

9. The beginning of the next round the stage is cleared, status changes and we do this process all over again.

 

Steps 7-9 are not written yet. The problem I am running into must be related to how I am defining my functions and the variables. The declarations must be being deleted for numDraw and that is why it is undefined. It does not come back as a NAN but simply null.

 

So a question I have is how do declare global variables that can be used through the functions with out them being deleted.

 

The numberDraw variable that I am having a problem with is declared as an XML variable in the load variable array. In order to use that data I converted it to in integer variable such as int(numberDraw).

 

I though about originally using a switch case statement but I don't think I need to. I am pretty sure I can just call the dynamic function when the XML data gets loaded and for off the tweens.

 

I need to research and learn how to use listeners because I do not have any as of yet.

 

Although I am running into these issues, I believe once I understand the proper way to set up the initial routines, my future projects will be easier to complete.

 

Thanks. Looking forward to your remarks. I will let you know what the sprite code below turns out.

Link to comment
Share on other sites

If you want a variable to persist, declare it at the class-level or as a property of a MovieClip. For example:

 

var numberDraw:int;
function myFunction1():void {
   numberDraw = 2;
   myFunction2();
}
function myFunction2():void {
   trace(numberDraw); // 2
}
myFunction1();

Link to comment
Share on other sites

I used the code below in the exact function that does the dynamic tween.

var numberDraw:Number = 2;
this.num2Stroke = new Sprite();
trace(this["num"+numberDraw+"Stroke"]); //traces [sprite]
trace(this.num2Stroke); //traces [sprite]

and the result came back as

[object Sprite]

[object Sprite]

from the trace statements. So it looks to me like I am not getting to the root of the object??

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