All pastes #954972 Raw Edit

Unnamed

public text v1 · immutable
#954972 ·published 2008-03-24 07:13 UTC
rendered paste body
//get your list of thumbnails
var thumbs:Array = new Array("thumb01.jpg", "thumb02.jpg", "thumb03.jpg");

//this is how far the thumbnails will scroll each frame, set higher to scroll faster
var scrollAmount:Number = 10;

//these is the dimensions of the mask, the viewable area for the thumbnails to scroll through
var maskWidth:Number = 600;
var maskHeight:Number = 149;

//set thumbWidth to whatever the width of each thumbnail will be, maybe add 4 or 5 pixels so there's a space between images
var thumbWidth:Number = 170;

//these just initialize the widths for the createThumbs function
var mainWidth:Number = 0;
var containerWidth:Number = 0;

//now we're making the mask
var imgMask:Sprite = new Sprite();
imgMask.graphics.beginFill(0);
imgMask.graphics.drawRect(0, 0, maskWidth, maskHeight);
imgMask.graphics.endFill();

//this is the main container
var imgContainer:Sprite = new Sprite();

//we're adding the container and the mask to the stage here
addChild(imgContainer);
addChild(imgMask);

//we tell the container to be masked by imgMask
imgContainer.mask = imgMask;

//now that we have everything setup, we run the function to generate the thumbnails
createThumbs();

function createThumbs():void {
	
	//we create a thumbnail container, this is what will house the thumbnails, and this is what scrolls inside the main container
	var thumbContainer:Sprite = new Sprite();
	thumbContainer.x = mainWidth;
	containerWidth = 0;
	
	//loop through your list of thumbs here
	for(var i:int = 0; i < thumbs.length; i++){
		//we create a clip for the image to sit in
		var thumbClip:Sprite = new Sprite();
		
		//we create a loader object to load the thumbnail
		var loader:Loader = new Loader();
		loader.load(new URLRequest(thumbs[i]));
		
		//we can add the loader to the thumbClip, so the image will display
		thumbClip.addChild(loader);
		
		//we tell the thumbClip where to sit in the thumbContainer
		thumbClip.x = thumbWidth * i;
		
		//we add the thumbClip to the thumbContainer so the thumbnail will display
		thumbContainer.addChild(thumbClip);
		containerWidth += thumbWidth;
	}
	mainWidth += containerWidth;
	
	//we add the ENTER_FRAME event to the thumbContainer
	thumbContainer.addEventListener(Event.ENTER_FRAME, scrollThumbs);
	
	//we add thumbContainer to imgContainer so the group of thumbnails will display
	imgContainer.addChild(thumbContainer);
	
	//now we check to make sure there are enough thumbnails to scroll by, and if there aren't, then we run this function again
	if(mainWidth <= imgMask.width + containerWidth ){
		createThumbs();
	}
}

//this is the event that fires every frame for thumbContainer's
function scrollThumbs(e:Event):void {
	//we initialize the thumbContainer variable from the event data passed to this function
	var thumbContainer:Sprite = e.target as Sprite;
	
	//we scroll the thumbContainer by the amount specified at the top
	thumbContainer.x -= scrollAmount;
	
	//if the thumbContainer has scrolled past the mask, we no longer need it, so remove it and add a new group of thumbnails to the end of the main container
	if(thumbContainer.x <= -(thumbContainer.width)){
		thumbContainer.parent.removeChild(thumbContainer);
		createThumbs();
	}	
}