Copy link to clipboard
Copied
I have a function that draws a rectangle on the screen (see createInfoPanel())
While drawing rectangle, I am adding 2 text fields on it.
But as you may guess, it is adding those immediately.
I want to delay adding these text fields, then I want to remove these panels after a while.
The problem is, when I set an interval or timer, they won't work after I using once (I had to stop them by clearing/removing, it didn't set them again).
Since my panel is being created each time image changes, I need them to work every time image changes.
So, I have 2 questions:
1- How can I re-set interval each time my createInfoPanel() function works? It won't work anymore after setting and claring once.
2- You can see infoPanel.addChild(titleField); line in addInfoPanel() function. How can I work a smooth animation here? I mean, text appears slowly?
Thanks in advance.
Code file:
public class ImageRotator extends Sprite
{
private var ... ; //Some variables
public function ImageRotator(xmlPath:String = "images.xml", interval:int = 8301😞void
{
timer = new Timer(interval);
loadXML(xmlPath);
}
private function loadXML(file:String😞void
{
urlLoader = new URLLoader(new URLRequest(file));
urlLoader.addEventListener(Event.COMPLETE, parseXML);
}
private function parseXML(e:Event😞void
{
xml = new XML(e.target.data);
loadImages();
}
private function loadImages():void
{
for (var i:int = 0; i < xml.children().length(); i++)
{
var loader:Loader = new Loader();
loader.load(new URLRequest(xml.children()[i].@Deleted User));
imagesVector.push(loader);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, sortImages);
}
}
private function sortImages(e:Event😞void
{
imagesCounter++;
for (var i:int = 0; i < imagesVector.length; i++)
{
imagesVector.reverse();
addChild(imagesVector[i]);
}
//I have only 3 images, I needed to set indexes because
//they were covering each other
this.setChildIndex(imagesVector[2], 0);
this.setChildIndex(imagesVector[1], 0);
this.setChildIndex(imagesVector[0], 0);
if (imagesCounter == imagesVector.length)
{
createInfoPanel();
timer.addEventListener(TimerEvent.TIMER, autoChange);
timer.start();
}
}
private function createInfoPanel():void
{
infoPanel.graphics.beginFill(0x000000, 0.0);
infoPanel.graphics.drawRect(0, 0, 967, 138);
infoPanel.graphics.endFill();
////Here I want to run addInfoPanel() function with 2 seconds delay,
////After it starts, I want to run removeInfoPanel() function with 2 more seconds delay
addChild(infoPanel);
}
private function addInfoPanel():void {
titleField.text = xml.children()[infoCounter]. @ title;
titleField.x = 425;
titleField.y = 0;
description.text = xml.children()[infoCounter]. @ description;
description.x = 427;
description.y = 26;
infoPanel.y = 300;
infoPanel.addChild(titleField);
infoPanel.addChild(description);
}
private function removeInfoPanel():void {
infoPanel.removeChild(titleField);
infoPanel.removeChild(description);
}
private function addActions():void
{
//Some function
}
private function changeImage(e:MouseEvent😞void
{
//Image changing function
}
private function changeDepth(e:TweenEvent😞void
{
//Some function
}
private function autoChange(e:TimerEvent😞void
{
//Some function
}
}
Edit: How I used to work the intervals:
private function createInfoPanel():void
{
//lines above code sample
intervalInfoPanel = setInterval(addInfoPanel,2000);
addChild(infoPanel);
}
private function addInfoPanel():void {
//lines above code sample
clearInterval(intervalInfoPanel);
intervalInfoPanelRemove = setInterval(removeInfoPanel,3500);
}
private function removeInfoPanel():void {
//lines above code sample
clearInterval(intervalInfoPanelRemove);
}
Copy link to clipboard
Copied
As for 2- You can see infoPanel.addChild(titleField); line in addInfoPanel() function. How can I work a smooth animation here? I mean, text appears slowly? -- the easiest way is to use a tweening library, or to write tweening functions yourself.
With TweenLite it would be:
titleField.alpha = 0;
TweenLite.to(titleField, 1.0, { alpha: 1 });
Since you would probably used this librarly for smooth transition, you could as well use it insted of Timer:
TweenLite.delayedCall(2, addInfoPanel);
Copy link to clipboard
Copied
Thanks,
I don't understand, I imported TweenLite library,
1st- TweenLite.to(titleField, 1.0, { alpha: 1 }); code works, I mean at least it has no errors and I got a trace before this call, but it doesn't appear on screen! I even deleted titleField.alpha = 0; line but no, still same... I tried to set setChildIndex 1, 2 but it doesn't differ.
2nd- I added 2 lines:
TweenLite.delayedCall(2, addInfoPanel);
TweenLite.delayedCall(4, removeInfoPanel);
But it works only once, after that it doesn't work, even traces don't respond...
What am I doing wrong? I shared almost the whole code, if something was not right, it should have been realized. This is getting harder.
Copy link to clipboard
Copied
Did you debug your code? If you get runtime errors and you don't debug, sometimes nothing happens but the rest works ok. Also, did you paste TweenLite.to(titleField, 1.0, { alpha: 1 }) (and titleField.alpha = 0; before) to the end of your addInfoPanel function?
Copy link to clipboard
Copied
Hi,
No, no debug/runtime, I also export my movieclip.
Here's how I use:
--------------------------------------------------------------------------
import com.greensock.*;
private function sortImages(e:Event):void
{
imagesCounter++;
for (var i:int = 0; i < imagesVector.length; i++)
{
imagesVector.reverse();
addChild(imagesVector);
}
if (imagesCounter == imagesVector.length)
{
createInfoPanel();
timer.addEventListener(TimerEvent.TIMER, autoChange);
timer.start();
}
}
private function createInfoPanel():void
{
infoPanel.graphics.beginFill(0x000000, 0.0);
infoPanel.graphics.drawRect(0, 0, 967, 138);
infoPanel.graphics.endFill();
addInfoPanel();
addChild(infoPanel);
}
private function addInfoPanel():void {
infoPanel.y = 300;
titleField.text = xml.children()[infoCounter]. @ title;
titleField.x = 425;
titleField.y = 0;
description.text = xml.children()[infoCounter]. @ description;
description.x = 427;
description.y = 26;
titleField.alpha = 0;
TweenLite.to(titleField, 1.0, { alpha: 1 });
description.alpha = 0;
TweenLite.to(description, 1.0, { alpha: 1 });
}
--------------------------------------------------------------------------
Nothing appears...