Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 07-27-2011, 11:16 AM   #1
otuatail
Registered User
 
Join Date: Sep 2009
Posts: 49
Unhappy timer event trigger problem

Hi I have a presentation that nearly works. I use a timer event to swap images every 5 seconds. The problem is as the timer gets started in the constructor, I have to wait a full 5 seconds for the first image. Is there a way to forcefully trigger the function.

This is doing my head in.

Thanks

Desmond.

Code:
package code 
{
	/*****************************************
	 * MovieSlides :
	 * Demonstrates a PPT slide show.
	 * -------------------
	 * See MovieSlides.fla
	 ****************************************/

	import flash.events.*;
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.utils.Timer;
	import flash.net.URLRequest;
	import flash.sampler.NewObjectSample;
							   
	public class MovieSlides extends MovieClip 
	{		
		var imageLoader1:Loader = new Loader(); 
		var imageLoader2:Loader = new Loader(); 
		var myTimer:Timer = new Timer(5000,0);
		var TransissionTimer:Timer = new Timer(100,0); // Will start and stop this
		var Counter:int = 0;
		var Dir:String = "MalawiPics/";
		var MyImages:Array = new Array(
		"S_DSC_0106.jpg","S_DSC_0218.jpg","S_DSC_0768.jpg",
		"S_DSC_0790.jpg","S_DSC_0929.jpg","S_DSC_0988.jpg",
		"S_DSC_1173.jpg","S_IMG_0848.jpg","S_IMG_0871.jpg",
		"S_IMG_0877.jpg","S_IMGP0293.jpg","S_IMGP0295.jpg",
		"S_IMGP0307.jpg","S_IMGP0333.jpg","S_IMGP0358.jpg",
		"S_IMGP0362.jpg","S_IMGP0392.jpg","S_IMGP0422.jpg"
		);
		var AlphaValue1:Number = 0.2;
		var AlphaValue2:Number = 1;
		var AlphaStep:Number = 0.01;
		var CurrentPlayer:int = 1;
		
		public function MovieSlides() 
		{
			// constructor code
			imageLoader1.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading); 
			imageLoader1.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 
			imageLoader2.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading); 
			imageLoader2.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded); 
			// var link:URLRequest = new URLRequest();
			myTimer.addEventListener(TimerEvent.TIMER, timerListener);
			TransissionTimer.addEventListener(TimerEvent.TIMER,MyAlpha);
			//loadImage(Dir + MyImages[0]);
			//Counter++;
			myTimer.start();
		}

		function timerListener(event:TimerEvent):void
		{
			loadImage(Dir + MyImages[Counter]);
			Counter++;
			if(Counter > 16) // I only have 16 images in array
				Counter = 0;
		}

		function loadImage(url:String):void 
		{  
			if(CurrentPlayer == 1)
				imageLoader1.load(new URLRequest(url));
			if(CurrentPlayer == 2)
				imageLoader2.load(new URLRequest(url));

			if(CurrentPlayer == 1) // Toggle loaders 1 and 2
			 CurrentPlayer = 2;
			else 
			 CurrentPlayer = 1;			
		} 
  
		function imageLoading(e:Event):void
		{
			// do nothing yet
		}

		function imageLoaded(e:Event):void // Shared by both Loaders
		{ 
		    if(CurrentPlayer == 1)
			{
				AlphaValue1 = 0.2;
				AlphaValue2 = 1.0;
				imageLoader1.alpha = AlphaValue1;
				imageArea1.addChild(imageLoader1);
			}
			else
			{
				AlphaValue1 = 1.0;
				AlphaValue2 = 0.2;
				imageLoader2.alpha = AlphaValue2;
				imageArea2.addChild(imageLoader2);
			}
			TransissionTimer.start();
		} 
		
		function MyAlpha(event:TimerEvent):void // TransissionTimer
		{
			if(CurrentPlayer == 2)
			{
				AlphaValue1 += AlphaStep;
				AlphaValue2 -= AlphaStep;
				imageLoader1.alpha = AlphaValue1;
				imageLoader2.alpha = AlphaValue2;
				if(imageLoader1.alpha == 1)
				{
					TransissionTimer.stop();
					imageLoader1.alpha = 1;				
				}
			}
			
			
			if(CurrentPlayer == 1)
			{
				AlphaValue1 -= AlphaStep;
				AlphaValue2 += AlphaStep;
				imageLoader1.alpha = AlphaValue1;
				imageLoader2.alpha = AlphaValue2;
				if(imageLoader2.alpha == 1)
				{
					TransissionTimer.stop();
					imageLoader2.alpha = 1;				
				}
			}
			
			
		}		
	}
}
otuatail is offline   Reply With Quote
Old 07-27-2011, 12:29 PM   #2
jooniya
Interactive Des/Dev
 
Join Date: Jan 2010
Location: Philadelphia, PA
Posts: 232
Default

You could put the timer event code inside a new function and then call that function in the constructor as well as in the timerListener.
__________________
<< Design. Code. Interact. >>
jooniya is offline   Reply With Quote
Old 07-27-2011, 12:41 PM   #3
otuatail
Registered User
 
Join Date: Sep 2009
Posts: 49
Red face

Not sure what you mean? if you mean create a function to start the timer and call this function in the constructor then I am back to square one again.

I need to trigger the event to get my first image then start the timer.
otuatail is offline   Reply With Quote
Old 07-27-2011, 12:51 PM   #4
Rhuno
Let's Go Blues!
 
Rhuno's Avatar
 
Join Date: Nov 2007
Location: Orlando, FL
Posts: 252
Send a message via AIM to Rhuno
Default

You could just call the timerListener function in the constructor to kick start things. You would just need to adjust the function signature to make the event parameter optional.

ActionScript Code:
function timerListener(event:TimerEvent = null):void {     loadImage(Dir + MyImages[Counter]);     Counter++;     if(Counter > 16) // I only have 16 images in array         Counter = 0; } // at the end of the constructor, just call the function timerListener();

Hope that helps.
__________________
Check out my flashblog, yo.
Rhuno is offline   Reply With Quote
Old 07-27-2011, 12:58 PM   #5
jooniya
Interactive Des/Dev
 
Join Date: Jan 2010
Location: Philadelphia, PA
Posts: 232
Default

Quote:
Originally Posted by otuatail View Post
Not sure what you mean? if you mean create a function to start the timer and call this function in the constructor then I am back to square one again.

I need to trigger the event to get my first image then start the timer.

I mean:

ActionScript Code:
public function MovieSlides()         {             // constructor code             .                         .                         .                         displayImage();             myTimer.start();         } function timerListener(event:TimerEvent):void { displayImage(); } function displayImage():void         {             loadImage(Dir + MyImages[Counter]);             Counter++;             if(Counter > 16) // I only have 16 images in array                 Counter = 0;         }

I personally try to avoid putting anything more than function calls in event listeners because on larger programs it can get really messy.
__________________
<< Design. Code. Interact. >>
jooniya is offline   Reply With Quote
Old 07-27-2011, 01:13 PM   #6
otuatail
Registered User
 
Join Date: Sep 2009
Posts: 49
Smile Solved

All i needed to do was call loadImage() in the constructor with the first image then kick off the timer. CurrentPlayer will auto toggle. Also I can have a one of welcome image. Thanks for all who have helped with this project over the week in several topics.

*** In Constructor ***
loadImage("MalawiPics/S_IMGP0993.jpg"); // Malcolm's lost passport start image.
myTimer.start();
otuatail is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 03:41 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.