I’ve found a slideshow online and have been able to modify it to my needs, but I’ve struck a wall when it comes to centering. I figured out how to center all the images no matter their dimensions:
thisLoader._x = (Stage.width - thisLoader._width)/2;
thisLoader._y = (Stage.height - thisLoader._height)/2;
But occassionally when it loads, the first image’s width and height doesn’t get calculated - so the image appears off the screen. I patched this up with a little if statement:
if(thisLoader._x<0){
thisLoader._x = 0;
thisLoader._y = 0;
}
This prevents the image from displaying off screen, but not centered. Isn’t there a better way? With my understanding of the code, it shouldn’t display the image before it knows its width and height…but it does. Any ideas?
Here’s the player’s full actionscript. (It also features a simple mp3 player, that you should be able to safely ignore)
import mx.xpath.XPathAPI;
// set random # variables - each must be 0 for first 'while' loop below
var randomNum = 0;
var randomNumLast = 0;
// parent container
var container_mc = this.createEmptyMovieClip("container",0);
// movie clip containers
container_mc.createEmptyMovieClip("loader1_mc",2);
container_mc.createEmptyMovieClip("loader2_mc",1);
// preload watcher
this.createEmptyMovieClip("watcher_mc",100);
// load xml
images_xml = new XML();
images_xml.ignoreWhite=true;
images_xml.onLoad = parse;
images_xml.load(xmlfile);
function parse(success) {
if (success) {
imageArray = new Array();
songArray = new Array();
var root = this.firstChild;
_global.numPause = Number(this.firstChild.attributes.timer * 1000);
_global.looping = this.firstChild.attributes.looping;
_global.mycolor = this.firstChild.attributes.color;
_global.fadetime = Number(this.firstChild.attributes.fadetime);
_global.imagepath = this.firstChild.attributes.image_path;
_global.songpath = this.firstChild.attributes.song_path;
songArray = XPathAPI.selectNodeList(this.firstChild,"slideshow/songs/song");
var imageNode = root.firstChild.lastChild;
var s=0;
while (imageNode.nodeName != null) {
imageData = new Object;
imageData.filename = imageNode.attributes.file_name;
imageArray[s]=imageData;
imageNode = imageNode.previousSibling;
s++;
}
// place parent container
// parse array
imageArray.reverse();
imageGen(imageArray);
playPause.swapDepths(1);
if(songArray.length == 0){
playPause._alpha = 0;
} else{
playSong();
}
preloader.onEnterFrame = function():Void{
preloader._rotation += 20;
}
onEnterFrame = function(){
var color1:Color = new Color("preloader");
color1.setRGB(mycolor);
}
} else {
trace('problem');
}
}
// depth swapping
function swapPlace(clip,num) {
eval(clip).swapDepths(eval("container_mc.loader"+num+"_mc"));
}
function loadImages(data,num) {
if (i==undefined || i == 2) {
i=2;
createLoader(i,data,num);
i=1;
} else if (i==1) {
createLoader(i,data,num);
i=2;
}
}
function createLoader(i,data,num) {
thisLoader=eval("container_mc.loader"+i+"_mc");
thisLoader._alpha=0;
thisLoader.loadMovie(imagepath+data[num].filename);
watcher_mc.onEnterFrame=function () {
var picLoaded = thisLoader.getBytesLoaded();
var picBytes = thisLoader.getBytesTotal();
if (isNaN(picBytes) || picBytes < 4) {
return;
}
thisLoader._x = (Stage.width - thisLoader._width)/2;
thisLoader._y = (Stage.height - thisLoader._height)/2;
if(thisLoader._x<0){
thisLoader._x = 0;
thisLoader._y = 0;
}
if (picLoaded / picBytes >= 1) {
swapPlace("container_mc.loader2_mc",1);
alphaTween = new mx.transitions.Tween(thisLoader, "_alpha", mx.transitions.easing.Regular.easeOut,0,100,_global.fadetime,true);
alphaTween = new mx.transitions.Tween(myOLD, "_alpha", mx.transitions.easing.Regular.easeOut,100,0,_global.fadetime,true);
timerInterval = setInterval(imageGen,_global.numPause,data);
myOLD=thisLoader;
delete this.onEnterFrame;
delete _root.preloader.onEnterFrame;
_root.preloader._alpha = 0;
}
}
}
function imageGen(data) {
// start at 0, increment to total number of images, then drop back to zero when done
if (p==undefined || p==data.length && _global.looping=="yes") { p=0; } else { break; }
loadImages(data,p);
p++;
clearInterval(timerInterval);
}
stop();
// Setup sound object
var song:Sound = new Sound();
song.setVolume(50);
song.onSoundComplete = playSong;
// Currently playing song
var cps:Number = -1;
// Position of music
var pos:Number;
function playSong():Void{
song.onSoundComplete = playSong;
if(cps == songArray.length-1){
cps = 0;
song.loadSound(songpath + songArray[cps].attributes.file_name, true);
} else {
song.loadSound(songpath + songArray[++cps].attributes.file_name, true);
}
playPause.gotoAndStop("play");
}
// Pauses the music
function pauseIt():Void{
pos = song.position;
song.stop();
}
function unPauseIt():Void{
song.start(pos/1000);
}
playPause.onRelease = function(){
if(this._currentframe == 1){
this.gotoAndStop("pause");
this._parent.pauseIt();
}
else{
this.gotoAndStop("play");
this._parent.unPauseIt();
}
}
Thanks!