PDA

View Full Version : Tween hangs



richjamison
February 27th, 2008, 12:02 AM
I have been working on a project that creates a table of cells from a xml data source. The cells animate onto the screen using the Tween class. At a specified timer the table fades away, updates the xml and rebuilds the table. The problem is that the Tween animation intermittently hangs up and doesn't complete.

Here is what it should look like every time:
http://homepage.mac.com/richjvp/Right.jpg

Here is an example of the kind of hang ups I'm getting:
http://homepage.mac.com/richjvp/Wrong.jpg

Here's my code. The flash file I'm using is a 2 frame movie. frame 1 sets some variables and fetches the xml.

Frame 2 processes the xml and creates the table via for loops.

Frame 1 Code:


if (allCells == null) {
var allCells:Array = new Array();
}


for (var i:int = 0; i < allCells.length; i++) {
//fadeOut(allCells[i]);
removeChild(allCells[i]);
}

var xml:XML;
var xmlList:XMLList;

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("http://10.10.10.154/statuscharts/webservices/chartutil.asmx/getMemberCounts")); //spreadSheet.xml

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void {
xml = XML(event.target.data);
xmlList = xml.children();
//trace(xmlList);
gotoAndStop("parse");
}


function getXMLNode(nodeName:String, theXmlList:XMLList) {
var theXmlListLength:int = theXmlList.length();
//trace(theXmlListLength);
for (var i:int = 0; i < theXmlListLength; i++) {
//trace(String(theXmlList[i].name()));
if (String(theXmlList[i].name()) == nodeName) {
//trace(theXmlList[i]);
return theXmlList[i].children();
break;
}
}
}


stop();

Frame 2 code:


import fl.motion.easing.*;
import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;

var titleBar_mc:MovieClip;
var myStates:XMLList = xmlList;
var columnHeader:MovieClip;
var columnCell:MovieClip;
var addColumnCellsTarget:Object;

function makeTable(theXMLList:XMLList) {
var allCellsIndex:int = 0;
titleBar_mc = new tableTitle_mc();
titleBar_mc.addEventListener(Event.ADDED_TO_STAGE, fadeOn);
titleBar_mc.alpha = 0;
titleBar_mc.x = 0;
titleBar_mc.y = 0;
allCells[allCellsIndex] = titleBar_mc;
addChild(titleBar_mc);
allCellsIndex++;
var theAttributes:XMLList = theXMLList[0].attributes();
var theAttribute_count:int = theAttributes.length();
for (var i:int = 0; i < theAttribute_count; i++) {
var theAttribute_name:String = theAttributes[i].name();
columnHeader = new tableHeader_mc();
columnHeader.addEventListener(Event.ADDED_TO_STAGE ,slideToRight);
columnHeader.width = 550 / theAttribute_count; //stage.stageWidth / theAttribute_count;
columnHeader.x = 0;
columnHeader.y = columnHeader.height;
columnHeader.tableCellHomeX = columnHeader.width * i;
columnHeader.tableCellHomeY = columnHeader.y;
columnHeader.cellValue_text.text = theAttribute_name;
allCells[allCellsIndex] = columnHeader;
allCellsIndex++;
addChild(columnHeader);
for (var j:int = 0; j < theXMLList.length(); j++) {
var theCellValue:String = theXMLList[j].attribute(theAttribute_name);
columnCell = new tableCell_mc();
columnCell.addEventListener(Event.ADDED_TO_STAGE,s lideDown);
columnCell.width = stage.stageWidth / theAttribute_count;
columnCell.x = columnHeader.tableCellHomeX;
columnCell.tableCellHomeX = columnHeader.tableCellHomeX;
columnCell.y = columnHeader.tableCellHomeY;
columnCell.tableCellHomeY = (columnHeader.tableCellHomeY) + (columnHeader.height * (j + 1));
columnCell.cellValue_text.text = theCellValue;
allCells[allCellsIndex] = columnCell;
allCellsIndex++;
addChild(columnCell);
}
}
}



var mySlideRightX:Tween;
function slideToRight(event:Event) {
event.target.removeEventListener(Event.ADDED_TO_ST AGE,slideToRight);
mySlideRightX = new Tween(event.target, "x", Linear.easeOut, event.target.x, event.target.tableCellHomeX, .5, true);
}

var mySlideLeftX:Tween;
function slideToLeft(event:Event) {
addColumnCellsTarget = event.target;
mySlideLeftX = new Tween(event.target, "x", Linear.easeOut, event.target.tableCellHomeX, event.target.x, .5, true);
}

var mySlideDownY:Tween;
function slideDown(event:Event) {
event.target.removeEventListener(Event.ADDED_TO_ST AGE,slideDown);
mySlideDownY = new Tween(event.target, "y", Linear.easeOut, event.target.y, event.target.tableCellHomeY, .5, true);
}

var mySlideUpY:Tween;
function slideUp(theMc) {
mySlideUpY = new Tween(theMc, "y", Linear.easeOut, theMc.tableCellHomeY, theMc.y, .5, true);
}

var myFadeOn:Tween;
function fadeOn(event:Event) {
event.target.removeEventListener(Event.ADDED_TO_ST AGE, fadeOn);
myFadeOn = new Tween(event.target, "alpha", Linear.easeOut, 0, 1, 1, true);
}

var myFadeOut:Tween;
function fadeOut(theMc) {
myFadeOut = new Tween(theMc, "alpha", Linear.easeOut, 1, 0, 1, true);
}




function fadeOutChartTimer() {
var myTimer:Timer = new Timer(60000);
myTimer.addEventListener(TimerEvent.TIMER, fadeOutChartTimerHandler);
myTimer.start();
}


function refreshChartTimer() {
var myTimer:Timer = new Timer(2000);
myTimer.addEventListener(TimerEvent.TIMER, refreshChartTimerHandler);
myTimer.start();
}


function fadeOutChartTimerHandler(event:TimerEvent):void {
//trace(event.target);
event.target.removeEventListener(TimerEvent.TIMER, fadeOutChartTimerHandler);
for (var i:int = 0; i < allCells.length; i++) {
fadeOut(allCells[i]);
//removeChild(allCells[i]);
}
refreshChartTimer();
}

function refreshChartTimerHandler(event:TimerEvent):void {
event.target.removeEventListener(TimerEvent.TIMER, refreshChartTimerHandler);
gotoAndPlay("init");
}



makeTable(myStates);

fadeOutChartTimer();

stop();

Anyone got any ideas?

excogitator
February 27th, 2008, 03:14 AM
Well. What you have encountered is a known issue and its termed as garbage collection. Unless you find a way to solve it through another post from someone. Im not sure whether there exists any logic problem in your code. Though I have seen many people switch over to other tween libraries such as caurina or tweenlite which does not stop midway. Hope that helps.

JYCaR
February 27th, 2008, 07:36 AM
Hey....

I have to say that I am very pissed off right now, because i have a similar problem...
I move elements on the screen when somebody presses arrow keys and sometimes the tween just hangs and i have no idea what to do.:evil::m:

The problem is, that the whole project depends on these tween and if one hangs.. than everything stops working since further action happens on TweenEvent.MOTION_FINISH.

So what to do...

I will try those other Tweens... caurina or tweenlite, hope it works.

regards...

richjamison
February 27th, 2008, 09:12 AM
Thanks excogitator, I'll give those a look. I appreciate your reply.

richjamison
February 27th, 2008, 10:52 AM
That seems to have fixed it! I ended up using the caurina tween class.

Thanks again.

Felixz
February 27th, 2008, 01:35 PM
It is because u create a variable referencing a Tween, then u create one Tween and pass there so it has one reference (no garbage collection), but when u create new Tween old reference is replaced by it and old one becomes applicable for garbage collection (zero references). To avoid that u could use a Dictionnary or an Array as those external tweening engines to store references

excogitator
February 28th, 2008, 12:52 AM
Felixz it would be great and helpful to everyone if you could direct how to carry forward the implementation of the dictionary. Because this is something everyone would like to know although other third party tween libraries are faster and to an extent stable. Any inputs would be valuable.
Glad that the caurina tweens worked for you richjamison. However we will try solving out the tween problem. I understand that project requirements require completion on deadlines so every way can be explored. Have a nice one.

unindexed
February 28th, 2008, 06:13 AM
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.utils.Dictionary;



var dict:Dictionary = new Dictionary();

for (var i:int = 0; i < 1000; i++){
var myObject:Sprite = new Sprite();
var myTween:Tween = new Tween(myObject, "x", Elastic.easeOut, 0, 300, 3, true);
dict[myObject] = myTween;
}


//test
for (var key:* in dict){
trace (dict[key])
//output: [object Tween]
}
Dictionary stores refs to Tweens and they don't get garbage-collected.

Felixz
February 28th, 2008, 11:17 AM
to remove an item from a dictionnary just use
delete dict[reference];

excogitator
February 29th, 2008, 12:04 AM
Sweet. Thanks for pitching in all of you. This should really be helpful to many.