Score change animation

template

#1

Hi,
I try to make simple scorebug. It’s feed from external XML file. I want to start an animation when score changing, but i doesn’t found event listener or dispatcher to get change of value.
My code is:

> 
> import flash.net.URLLoader;
> import flash.events.Event;
> import flash.events.TimerEvent;
> import flash.net.URLRequest;
> import flash.utils.Timer;
> import flash.geom.ColorTransform
> import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
> 
> var myTimer:Timer = new Timer(100);
> myTimer.addEventListener(TimerEvent.TIMER, timerListener);
> function timerListener (e:TimerEvent):void
{
>   var myXMLLoader:URLLoader = new URLLoader();
>   myXMLLoader.load(new URLRequest("D:/Download/SCOREBOARDS/Scoreboard+(16)/New folder/Output/Xml.xml"));
>   myXMLLoader.addEventListener(Event.COMPLETE, processXML);
> 
>    function processXML (e:Event):void
>    {
>    var myXML:XML = new XML(e.target.data);
> 
> ShotClock.text = myXML.timestamp;
> GameClock.text = myXML.Clock;
> HS.HomeScore.text = myXML.HomeScore;
> AS.AwayScore.text = myXML.AwayScore;
> Period.text = myXML.Period;
> AwayTeam.text = myXML.AwayName;
> HomeTeam.text = myXML.HomeName;
> 
> 	    if ((myXML.timestamp) > 5) 
>  
>       {	  
>         var colorYELL:ColorTransform = ShotClock.transform.colorTransform;
>         colorYELL.color = 0xFFCC00;
>         ShotClock.transform.colorTransform = colorYELL; 
>       }
> 	  else 
> 	  {
> 		  var colorRED:ColorTransform = ShotClock.transform.colorTransform;
>         colorRED.color = 0xFF0000;
>         ShotClock.transform.colorTransform = colorRED;
> 	  }
>         if ((myXML.HomeScore).CHANGE) 
		
	{var myTween:Tween = new Tween(HS.HomeScore, "alpha", Strong.easeIn, 0, 1, 1, true);
> 		
> 	}
> 
> 	}
> myTimer.start();

#2

Where is that code? In a frame? See this for a proper way to run code inside your template.


#3

Yes. It is in first frame. Can you show me an example to get external data via Advanced Flash Templates?


#4

There is a basic code example for dynamic images here. You can copy your code into the as file and overwrite postInitialize to add the event listener and start the timer.

I have no example as I normally process external data in a custom client and not directly in the template.


#5

I did it, It’s work, but I’m not sure I did it by the right way. What is your opinion?

package  {
	
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.geom.ColorTransform
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	
	public class SCOREBUG_TEST2_AS extends CasparTemplate {
		
	override public function postInitialize():void
   {
	var myTimer:Timer = new Timer(100);
	myTimer.addEventListener(TimerEvent.TIMER, timerListener);
	function timerListener (e:TimerEvent):void{
	var myXMLLoader:URLLoader = new URLLoader();
	myXMLLoader.load(new URLRequest("D:/Xml.xml"));
	myXMLLoader.addEventListener(Event.COMPLETE, processXML);

	function processXML (e:Event):void
	{
		var myXML:XML = new XML(e.target.data);

		TXT.ShotClock.text = myXML.timestamp;
		TXT.GameClock.text = myXML.Clock;
		TXT.HomeScore.text = myXML.HomeScore;
		TXT.AwayScore.text = myXML.AwayScore;
		TXT.AwayTeam.text = myXML.AwayName;
		TXT.HomeTeam.text = myXML.HomeName;

			if (myXML.Period >= 1 && myXML.Period <= 4)
			
				{
				TXT.Period.text = (myXML.Period + "/4");
				}	
			
			else
				
				{
				TXT.Period.text ="OT";
				}
		
			if ((myXML.timestamp) > 5) 
		 
				{	  
				var colorYELL:ColorTransform = TXT.ShotClock.transform.colorTransform;
				colorYELL.color = 0xFFCC00;
				TXT.ShotClock.transform.colorTransform = colorYELL; 
				}
			  else 
				{
				var colorRED:ColorTransform = TXT.ShotClock.transform.colorTransform;
				colorRED.color = 0xFF0000;
				TXT.ShotClock.transform.colorTransform = colorRED;
				}
			}


		}
			myTimer.start();
	}		

	}

	}

#6

It’s kaind of messy from the structure. You should indent properly to get to know, what function starts where and where something else starts. I would do something like that (did not test it):

package
{
	import flash.display.MovieClip;
	import flash.net.URLLoader;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.utils.Timer;
	import flash.geom.ColorTransform
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
	
	public class SCOREBUG_TEST2_AS extends CasparTemplate 
	{
		private var myXMLLoader:URLLoader;
		private var myTimer:Timer; 
		
		private function timerListener (e:TimerEvent):void
		{
			myXMLLoader = new URLLoader();
			myXMLLoader.load(new URLRequest("D:/Xml.xml"));
			myXMLLoader.addEventListener(Event.COMPLETE, processXML);
		}
		
		private function processXML (e:Event):void
		{
			var myXML:XML = new XML(e.target.data);

			TXT.ShotClock.text = myXML.timestamp;
			TXT.GameClock.text = myXML.Clock;
			TXT.HomeScore.text = myXML.HomeScore;
			TXT.AwayScore.text = myXML.AwayScore;
			TXT.AwayTeam.text = myXML.AwayName;
			TXT.HomeTeam.text = myXML.HomeName;

			if (myXML.Period >= 1 && myXML.Period <= 4)
			{
				TXT.Period.text = (myXML.Period + "/4");
			}	
			else
			{
				TXT.Period.text ="OT";
			}
		
			if ((myXML.timestamp) > 5) 
			{	  
				var colorYELL:ColorTransform = TXT.ShotClock.transform.colorTransform;
				colorYELL.color = 0xFFCC00;
				TXT.ShotClock.transform.colorTransform = colorYELL; 
			}
			else 
			{
				var colorRED:ColorTransform = TXT.ShotClock.transform.colorTransform;
				colorRED.color = 0xFF0000;
				TXT.ShotClock.transform.colorTransform = colorRED;
			}
		}

		override public function postInitialize():void
		{
			myTimer = new Timer(100);
			myTimer.addEventListener(TimerEvent.TIMER, timerListener);
			myTimer.start();
		}
	}		
}

I prefere not to have functions local inside other functions, but would do the event handles outside. For that to work you need to declare the variables myXMLLoader and myTimer outside the postInitialize function. I don’t say, that your way of doing it is wrong. But I think mine is a least more readable :slight_smile: