Jump to content
Search Community

Tween Not Behaving Properly

beno test
Moderator Tag

Recommended Posts

Hi;

I have a very straightforward tween that *was* behaving properly before I deleted a removeChild() which preceded an addChild() statement for the same variable because I was told that was unnecessary and that it was causing another problem, which indeed it was and is now resolved. *However*, before that deletion the following tween was working. Now it waits the appointed 1 second and then all of a sudden moves the movie along the x axis in one sudden movement. I've tried many variations of this--TweenLite, TweenMax, TimelineMax, etc.-- and they all do the same thing.

 

function CallNextSlide(event:TimerEvent):void
	{
		TweenMax.to(parent_container, 1, {x:150, onComplete:NextSlide});
//			TweenLite.to(parent_container, 2, {x:150, onComplete:NextSlide()});
	}
}

 

Here's the full code:

 

package  {
import com.greensock.*;
import flash.display.MovieClip;
import flash.events.TimerEvent;
import flash.utils.Timer;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import Images;

[sWF(backgroundColor="0x000000", width="1008", height="548")]

public class Slides extends MovieClip
{
       var parent_container:MovieClip = new MovieClip();
	var imagesArray:Array = new Array("images/1.png", 
									   "images/2.png", 
									   "images/3.png", 
									   "images/4.png", 
									   "images/5.png", 
									   "images/6.png",
									   "images/7.png",
									   "images/8.png",
									   "images/9.png",
									   "images/10.png",
									   "images/11.png",
									   "images/12.png");
	var _totalSlides:Number = new Number(12);
	var _numOfSlides:Number = new Number(6);
	var _totalImages:Number = new Number(12);
	var _w:Number = new Number(120);
	var _h:Number = new Number(120);
	var _x:Number = new Number(213);
	var _y:Number = new Number(150);
	var img:Images;
	var img0:Images;
	var img1:Images;
	var img2:Images;
	var img3:Images;
	var img4:Images;
	var img5:Images;
	var i:int;
	var spacer:int;
	var j:int;
	var _firstImageInArray:int;
	var _secondImageInArray:int;
	var _thirdImageInArray:int;
	var _fourthImageInArray:int;
	var _fifthImageInArray:int;
	var _sixthImageInArray:int;
	var _firstImageInArrayURL:String = new String();
	var _secondImageInArrayURL:String = new String();
	var _thirdImageInArrayURL:String = new String();
	var _fourthImageInArrayURL:String = new String();
	var _fifthImageInArrayURL:String = new String();
	var _sixthImageInArrayURL:String = new String();
	var _const:int;
	var _const_mod:int;
	var k:int = 0;
	var _mod:int;
	var ImageDisplayArray:Array;
	var myTimer:Timer = new Timer(1000);
	var iterations:int=0;
	var urlXMLLoader:URLLoader;
	var shoppingCartImagesArray:Array = new Array();
	var shoppingCartURLArray:Array = new Array();
	var imageDir:String = new String();

	public function Slides(_numOfSlides:int, _totalSlides:int, _w:int, _h:int, _x:int, _y:int):void
	{
		addChild(parent_container);
		var container:MovieClip = new MovieClip();
		spacer = 10;
		XMLLoader();
		AddSlides();
		parent_container.x = 150;
		NextSlide();
	}

	function XMLLoader()
	{
		var urlXMLRequest:URLRequest = new URLRequest('shoppingCartImages.xml');
		urlXMLLoader = new URLLoader();
		urlXMLLoader.addEventListener(Event.COMPLETE, completeXMLListener);
		urlXMLLoader.load(urlXMLRequest);
	}

	function completeXMLListener(e:Event):void
	{
		var xmlData:XML = XML (e.target.data);
		imageDir = xmlData.IMAGE_FOLDER.text()
		var list:String = new String(xmlData.IMAGES.text());
		shoppingCartImagesArray = list.split(",");
		list = null;
		list = xmlData.URLS.text();
		shoppingCartURLArray = list.split(",");
		list = null;
		trace(imageDir);
		trace(shoppingCartURLArray);
	}

	function AddSlides():void
	{
		_const = (Math.round((iterations+_numOfSlides/2)/_numOfSlides));
		_const_mod = _const*_numOfSlides%imagesArray.length;
		if (_const_mod == _numOfSlides)
		{
			_const = 1;
		} else {
			_const = 0;
		}
		LoadImageDisplayArray();
	}

	function LoadImageDisplayArray():void
	{
		ImageDisplayArray = null;
		ImageDisplayArray = new Array();
		_mod = iterations%_numOfSlides;
		if (iterations == _numOfSlides)
		{
			_firstImageInArray = _const*_numOfSlides+_mod;
			_secondImageInArray = _const*_numOfSlides+_mod+1;
			_thirdImageInArray = _const*_numOfSlides+_mod+2;
			_fourthImageInArray = _const*_numOfSlides+_mod+3;
			_fifthImageInArray = _const*_numOfSlides+_mod+4;
			_sixthImageInArray = _const*_numOfSlides+_mod+5;
			_firstImageInArrayURL = imagesArray[_const*_numOfSlides+_mod].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_secondImageInArrayURL = imagesArray[_const*_numOfSlides+_mod+1].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_thirdImageInArrayURL = imagesArray[_const*_numOfSlides+_mod+2].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_fourthImageInArrayURL = imagesArray[_const*_numOfSlides+_mod+3].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_fifthImageInArrayURL = imagesArray[_const*_numOfSlides+_mod+4].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_sixthImageInArrayURL = imagesArray[_const*_numOfSlides+_mod+5].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			iterations = -1;
		} else {
			_firstImageInArray = _const*_numOfSlides+_mod-6;
			_secondImageInArray = _const*_numOfSlides+_mod-5;
			_thirdImageInArray = _const*_numOfSlides+_mod-4;
			_fourthImageInArray = _const*_numOfSlides+_mod-3;
			_fifthImageInArray = _const*_numOfSlides+_mod-2;
			_sixthImageInArray = _const*_numOfSlides+_mod-1;
			_firstImageInArrayURL = imagesArray[_const*_numOfSlides+_mod-6].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_secondImageInArrayURL = imagesArray[_const*_numOfSlides+_mod-5].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_thirdImageInArrayURL = imagesArray[_const*_numOfSlides+_mod-4].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_fourthImageInArrayURL = imagesArray[_const*_numOfSlides+_mod-3].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_fifthImageInArrayURL = imagesArray[_const*_numOfSlides+_mod-2].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
			_sixthImageInArrayURL = imagesArray[_const*_numOfSlides+_mod-1].replace("images/", "").replace("png","py").replace("jpg","py").replace("gif","py");
		}
		k = 0;
		while (k < _numOfSlides)
		{
			switch(k)
			{
				case(0):
					img0 = new Images(imagesArray[_firstImageInArray], _w, _h, ((_w+spacer)*j)+_x+600, _y, _firstImageInArrayURL);
					ImageDisplayArray.push(img0);
					break;
				case(1):
					img1 = new Images(imagesArray[_secondImageInArray], _w, _h, ((_w+spacer)*(j-1))+_x+600, _y, _secondImageInArrayURL);
					ImageDisplayArray.push(img1);
					break;
				case(2):
					img2 = new Images(imagesArray[_thirdImageInArray], _w, _h, ((_w+spacer)*(j-2))+_x+600, _y, _thirdImageInArrayURL);
					ImageDisplayArray.push(img2);
					break;
				case(3):
					img3 = new Images(imagesArray[_fourthImageInArray], _w, _h, ((_w+spacer)*(j-3))+_x+600, _y, _fourthImageInArrayURL);
					ImageDisplayArray.push(img3);
					break;
				case(4):
					img4 = new Images(imagesArray[_fifthImageInArray], _w, _h, ((_w+spacer)*(j-4))+_x+600, _y, _fifthImageInArrayURL);
					ImageDisplayArray.push(img4);
					break;
				case(5):
					img5 = new Images(imagesArray[_sixthImageInArray], _w, _h, ((_w+spacer)*(j-5))+_x+600, _y, _sixthImageInArrayURL);
					img5.addEventListener("xxx",URLs);
					ImageDisplayArray.push(img5);
					break;
			}
			++k;
		}
		k = 0;
		while (k < ImageDisplayArray.length)
		{
			parent_container.addChild(ImageDisplayArray[k]);
			k++;
		}
	}

	function URLs(e:Event):void
	{
//			trace('hit');
	}

	function NextSlide():void
	{
		spacer = 10;
		iterations++;
		LoadImageDisplayArray();
		myTimer.addEventListener(TimerEvent.TIMER, CallNextSlide);
		myTimer.start();
	}

	function CallNextSlide(event:TimerEvent):void
	{
		TweenMax.to(parent_container, 1, {x:150, onComplete:NextSlide});
//			TweenLite.to(parent_container, 2, {x:150, onComplete:NextSlide()});
	}
}
}

 

 

Here's the code for Images:

 

package  {
   import flash.events.IEventDispatcher;
   import flash.events.EventDispatcher;
   import flash.events.Event;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.filters.*;
import flash.filters.BitmapFilterQuality;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.events.IOErrorEvent;

public class Images extends MovieClip
{
	private var parent_container:Sprite = new Sprite();
	private var _path:String = new String("path");
	private var _myWidth:Number = new Number(20);
	private var _myHeight:Number = new Number(20);
	private var _myX:Number = new Number(20);
	private var _myY:Number = new Number(20);
	private var _myURL:String = new String("url");

	public function Images(path:String, myWidth:uint, myHeight:uint, myX:uint, myY:uint, myURL:String="")
	{
		_path = path;
		_myURL = myURL;
		_myWidth = myWidth;
		_myHeight = myHeight;
		_myX = myX;
		_myY = myY;
		LoadImage();
	}

	function LoadImage():void 
	{
		parent_container = new Sprite();
		addChild(parent_container)
		var req:URLRequest = new URLRequest(_path);
		var loader:Loader = new Loader();
		loader.load(req);
		loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);  		
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadedImage);
	}

	function LoadedImage(e:Event):void
	{
		var loaderInfo:LoaderInfo = e.target as LoaderInfo;
		var displayObject:DisplayObject = loaderInfo.content;
		displayObject.width = _myWidth;
		displayObject.height = _myHeight;
		parent_container.addChild(displayObject);
		parent_container.x = _myX;
		parent_container.y = _myY;
		if(_myURL == "BACKGROUND_IMAGE") // This is a cheat
		{
			dispatchEvent(new Event("loadcomplete"));
		} else if (_myURL != "") { // this is not a cheat
			dispatchEvent(new Event("xxx"));
		}
	}

	function ioErrorHandler(event:IOErrorEvent):void {
		trace("ioErrorHandler: " + event);
	}
}
}

 

 

I call this code from another class thus:

 

var _numOfSlides:Number = new Number(6);
		var _totalImages:Number = new Number(12);
		var _w:Number = new Number(120);
		var _h:Number = new Number(120);
		var _x:Number = new Number(213);
		var _y:Number = new Number(150);
		var mySlides:Slides = new Slides(_numOfSlides, _totalImages, _w, _h, _x, _y);
		addChild(mySlides);

 

but you could easily tweak the code to run it directly. Please advise.

TIA

Beno

Link to comment
Share on other sites

If your tween of parent_container is suddenly jerking or stopping, you must have code elsewhere that's controlling it and overriding the tween. Sometimes it's because you created a tween of that object (parent_container) and it's overwriting the previous one. See http://www.greensock.com/overwritemanager/ for details about overwriting.

 

Also, did you intend to create an infinite loop between NextSlide() and CallNextSlide()? Looks like NextSlide() starts a timer that calls CallNextSlide() after 1 second which creates a tween for 1 second which calls NextSlide when it completes and the cycle starts all over again.

Link to comment
Share on other sites

First up, yes, an infinite loop is indeed what I want. Concerning the overwrite manager, yes, there is a tween before the loop; however, according to the docs, it is immediately overwritten by the first call to the loop. Again, this worked fine before the aforementioned necessary edit, and that previous tween was there then, too. Ideas?

TIA,

Beno

Link to comment
Share on other sites

Here is the original code:

 

public function Slides(_numOfSlides:int, _totalSlides:int, _w:int, _h:int, _x:int, _y:int):void
       {
           if (parent_container.parent)
           {
               parent_container.parent.removeChild(parent_container);
           }
           addChild(parent_container);
           var container:MovieClip = new MovieClip();
           spacer = 10;
           AddSlides();
           timeline = new TimelineMax({onComplete:NextSlide()});
           timeline.append(TweenLite.to(parent_container, 0, {x:150}) );
       }

 

modified to this:

 

public function Slides(_numOfSlides:int, _totalSlides:int, _w:int, _h:int, _x:int, _y:int):void
	{
		addChild(parent_container);
		var container:MovieClip = new MovieClip();
		spacer = 10;
		XMLLoader();
		AddSlides();
		parent_container.x = 150;
		NextSlide();
	}

 

but since that only runs once and this happens with every tween, it is probably this code:

 

function NextSlide():void
       {
           spacer = 10;
           iterations++;
           i = 0;
           while (i != parent_container.numChildren)
           {
               parent_container.removeChildAt(i);
           }
           if (parent_container.parent)
           {
               removeChild(parent_container);
               parent_container = null;
               parent_container = new MovieClip();
               addChild(parent_container);
           }
           LoadImageDisplayArray();
           myTimer.addEventListener(TimerEvent.TIMER, CallNextSlide);
           myTimer.start();
       }

 

now modified to:

 

function NextSlide():void
	{
		spacer = 10;
		iterations++;
		LoadImageDisplayArray();
		myTimer.addEventListener(TimerEvent.TIMER, CallNextSlide);
		myTimer.start();
	}

 

TIA,

Beno

Link to comment
Share on other sites

I'm curious, that loadImagesDisplayArray() function has a lot going on with while loops, dynamic array searching and String replacement.

 

if you remove the call to loadImagesDisplayArray() in nextSlide() will the tween work?

 

maybe experiment with this:

 

function NextSlide():void
     {
        spacer = 10;
        iterations++;

//reset the parent_container.x to something arbitrary just for testing
parent_container.x = 400;


//do not call LoadImageDisplayArray        
// LoadImageDisplayArray();


        myTimer.addEventListener(TimerEvent.TIMER, CallNextSlide);
        myTimer.start();
     }

     function CallNextSlide(event:TimerEvent):void
     {
        TweenMax.to(parent_container, 1, {x:150, onComplete:NextSlide});
//         TweenLite.to(parent_container, 2, {x:150, onComplete:NextSlide()});
     }
  }

 

make sure parent_container contains something you can see of course.

 

Being that your code doesn't show any other tweens and you claim there is no other code with tweens that could overwrite the one in question, I wonder if you are doing something to choke the flash player in loadImageDisplayArray. might be worth looking into. At this point the only effective way of testing is to remove things until it works.

 

good luck on your project

 

Carl

Link to comment
Share on other sites

If you look at that function, you will see that virtually the only thing it does is:

 

1) assign values to variables, and;

2) call another function; namely, Images.

 

Now, remember, this function did in fact correctly tween before I took out the removeChild() calls. I guess I'm foolish, but to me it seems to me that there is either a bug in GreenSock or in Flash, because I don't see how taking out the removeChild() calls could possibly do this to my class.

 

Good luck? Ah, well, thanks for the vote of confidence, and thanks for your help in other posts of mine.

Beno

Link to comment
Share on other sites

Now, remember, this function did in fact correctly tween before I took out the removeChild() calls. I guess I'm foolish, but to me it seems to me that there is either a bug in GreenSock or in Flash, because I don't see how taking out the removeChild() calls could possibly do this to my class.

When I glanced at your "before" and "after" code, it looked like there were quite a few other differences than simply removing a few removeChild() calls.

 

If you'd like to nail down the issue, please post 2 FLA files: 1 with the old code where the tween worked and the other with the new code and the tween that doesn't work. That way we can just compile and see for ourselves. Please make sure that the files are identical with the exception of that removeChild() stuff. This will help identify if there's a bug somewhere (I've got to admit that I am 99.99999% sure that the problem has nothing to do with a bug in Flash or TweenLite/Max, but I'm curious to find out for sure).

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