Jump to content
Search Community

News Ticker

mrittman test
Moderator Tag

Recommended Posts

I'm working on a horizontal scrolling news ticker and was wondering if someone could go over my code and help clean it up a bit. I'm fairly new to Actionscript 3 so I'm sure there are a lot of redundant areas in the code that could be optimized.

 

Here is a working demo of what I have: http://www.info2gousa.com/news/News2.swf

 

Here's my code and I have attached the source files:

 

import com.greensock.*;
import com.greensock.easing.*;

//hides the image until it is loaded
theImage.alpha=0;
loadingBar.visible = false;

//variables to hold the final coordinates of the image tween
var finalX:Number;
var finalY:Number;
var endX:Number;
var endY:Number;
var pausedX:Number;
var pausedY:Number;
var beginX:Number;
var beginY:Number;

//variable to hold the number of images in the XML
var listLength:Number;

//keeps track of what image should be displayed
var currPhoto:Number=0;

//arrays to hold the contents of the XML, using this to allow
//for the random order of the images
var photoArray:Array = new Array();
var descriptionArray:Array = new Array();
var titleArray:Array = new Array();

//Loader event for the XML
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onLoaded);

var xml:XML;

loader.load(new URLRequest("news.xml"));

function onLoaded(e:Event):void {
//load XML
xml=new XML(e.target.data);
var il:XMLList=xml.article;
listLength=il.length();

populateArray();
}

function populateArray():void {
//takes the properties defined in the XML and stores them 
//into arrays
var i:Number;
for (i = 0; i < listLength; i++) {
	photoArray[i]=xml.article[i].photo;
	titleArray[i]=xml.article[i].title;
	descriptionArray[i]=xml.article[i].description;
}
beginImage();
}

function beginImage():void {
//grabs a random number between 0 and the number
//of images in the array
currPhoto=Math.floor(Math.random()*photoArray.length);

//load title and description
theTitle.text=titleArray[currPhoto];
theDescription.text=descriptionArray[currPhoto];

theImage.scaleX=1;
theImage.scaleY=1;

var imageLoader = new Loader();

//catches errors if the loader cannot find the URL path
imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, catchFunction);
//actually loads the URL defined in the image array
imageLoader.load(new URLRequest(photoArray[currPhoto]));
//adds a listener for while the image is loading
imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imgLoading);
//adds a listener for what to do when the image is done loading
imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);


function catchFunction(e:IOErrorEvent) {
	trace("Bad URL: " + photoArray[currPhoto] + " does not exist");
	//take out the bad URL from the array
	photoArray.splice(currPhoto,1);
	titleArray.splice(currPhoto,1);
	descriptionArray.splice(currPhoto,1);

	//check to see if there are images left,
	//else restart the news ticker
	if (photoArray.length==0) {
		populateArray();
	} else {
		beginImage();
	}
}

function imgLoading(event:ProgressEvent):void{
	//show the loading bar, and update the width
	//based on how much is loaded
	loadingBar.visible = true;
	loadingBar.bar.width = (event.bytesLoaded/event.bytesTotal)*100;
}

function imgLoaded(event:Event):void {
	loadingBar.visible = false;

	//add the image and get the dimensions to center the image
	theImage.addChild(imageLoader);
	//take the contents of the loaded image and cast it as bitmap data
	//to allow for bitmap smoothing
	var image:Bitmap = imageLoader.content as Bitmap;
	image.smoothing=true;
	//starting position of image
	theImage.x = (stage.stageWidth+10);
	theImage.y = (stage.stageHeight-71) - (imageLoader.content.height / 2);
	//paused position of image
	finalX = (142-imageLoader.content.height)/2;
	finalY = (stage.stageHeight-71) - (imageLoader.content.height / 2);
	//Ending position of image
	endX = ((0-stage.stageWidth)+10) - (imageLoader.content.width * .8 / 2);
	endY = (stage.stageHeight-71) - (imageLoader.content.height / 2);

	theDescription.x = (finalX*2)+imageLoader.content.width;
	theDescription.width = (stage.stageWidth - imageLoader.content.width) - (finalX*3);
	pausedX = theDescription.x;
	beginX = pausedX + 520;

	//start tween function
	easeIn();
}
}

function easeIn():void {
TweenLite.to(theImage, 1.5, {x:finalX, y:finalY, onComplete:pauseCurrent});
TweenLite.to(theImage, 1, {alpha:1, overwrite:0});

TweenLite.from(theTitle, 1.5, {x:528, y:5});
TweenLite.to(theTitle, 1.5, {x:8, y:5, onComplete:pauseCurrent});
TweenLite.to(theTitle, 1, {alpha:1, overwrite:0});

TweenLite.from(theDescription, 1.5, {x:520+pausedX, y:44});
TweenLite.to(theDescription, 1.5, {x:pausedX, y:44, onComplete:pauseCurrent});
TweenLite.to(theDescription, 1, {alpha:1, overwrite:0});
}

function pauseCurrent():void {
TweenLite.to(theImage, 20, {onComplete:easeOut});
TweenLite.to(theTitle, 20, {onComplete:easeOut});
TweenLite.to(theDescription, 20, {onComplete:easeOut});
}

function easeOut():void {
TweenLite.to(theImage, 1.5, {x:endX, y:endY, alpha:0, onComplete:nextImage});
TweenLite.to(theTitle, 1.5, {x:-512, y:5, alpha:0});
TweenLite.to(theDescription, 1.5, {x:pausedX-520, y:44, alpha:0});
}

function nextImage():void {
//take out the image that was just displayed
photoArray.splice(currPhoto,1);
titleArray.splice(currPhoto,1);
descriptionArray.splice(currPhoto,1);

//remove the image
theImage.removeChildAt(0);

//start over
if (photoArray.length==0) {
	populateArray();
} else {
	beginImage();
}
}

 

Thank you!

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