PDA

View Full Version : Passing a variable value to another variable


Cipes
10-21-2008, 08:39 PM
hi There, It will become quite obvious that Im very inexperienced, and Im trying to amalgamate a few tutorials together in order to get what I need.
Its a XML based slide show that's randomly generated. I've been trying to tweak it in order that the first picture loaded is randomly generated and then from there onwards it progresses from one picture to the next in order.

Here's the codeimport mx.transitions.Tween;
import mx.transitions.easing.*;

pauseTime = 4000;

xmlImages = new XML();
xmlImages.ignoreWhite = true;
xmlImages.onLoad = loadImages;
xmlImages.load("images.xml");


function loadImages(loaded)
{ if (loaded) { xmlFirstChild = this.firstChild; imageFileName = [];
totalImages = xmlFirstChild.childNodes[0].childNodes.length;

for (i=0; i<totalImages; i++) { imageFileName[i] = xmlFirstChild.childNodes[0].childNodes[i].attributes.title;}
randomImage();

}
}
function randomImage()
{ if (loaded == filesize)
{var ran = Math.round(Math.random() * (totalImages - 1));
picture_mc._alpha = 0; // Start image clip as invisible
picture_mc.loadMovie(imageFileName[ran], 1); //Load random image from xml
var pictureTweenIn:Tween = new Tween (picture_mc,"_alpha",Normal.easeIn,0,100,1,true); //
pictureTweenIn.onMotionFinished = function () { // When done fading
_root.pause(); // Start pause() function
}
}
}


function pause() {
myInterval = setInterval(pause_slideshow, pauseTime);
function pause_slideshow() {
clearInterval(myInterval);
var pictureTweenOut:Tween = new Tween (picture_mc,"_alpha",Normal.easeOut,100,0,1,true); // After pause, start fade out
pictureTweenOut.onMotionFinished = function () { // Once faded out
_root.slideShow(); // Call next Image in order after random()
}
}
}

function slideShow() {
myInterval = setInterval(pause_slideshow, pauseTime);
function pause_slideshow() {
clearInterval(myInterval);
nextImage();
}
}

function nextImage() {
//if (i<(totalImages-1)) {
//i++;
if (loaded == filesize) {
{
picture_mc._alpha = 0; // Start image clip as invisible
picture_mc.loadMovie(imageFileName[ran],1); //Load next image from xml
var pictureTweenIn:Tween = new Tween (picture_mc,"_alpha",Normal.easeIn,0,100,1,true); //
pictureTweenIn.onMotionFinished = function () { // When done fading
_root.pause(); // Start pause() function
slideShow();
}
}
}
}
Basically I want to secure the var ran declared in the randomImage
function and just continue in the nextImage fuction by adding +1 to it and and repeating the slideShow function.

Is the ran value accessible outside of its block? seems to be a local variable.
I know nothing really. Thanks for any help you can give.

CyanBlue
10-21-2008, 08:52 PM
Howdy and Welcome... :)

The variable ran is a local variable that is declared inside the randomImage() function which is not accessible from other functions... You'd need to define it outside of the function to make it accessible from other functions like this...
//
var publicVariable:Number;

a();

function a()
{
publicVariable = 1;
trace("publicVariable in function a : " + publicVariable);
}

function b()
{
publicVariable = 2;
trace("publicVariable in function b : " + publicVariable);
}

Cipes
10-22-2008, 09:11 PM
Cyan! Thanks for your reply. I thought that a local variable may have been the problem. I've declared the ran variable globally, and its working alright,
now the first image from the XML is generated randomly and then it continues to the next image in order. But after getting the next image in order, it keeps on that one image and doesn't advance any further.
I've been looking at my code to find out why this is..
Im suppose it must be some fault in my if statement?
Your help again is much appreciated. I feel like its almost there!
import mx.transitions.Tween;
import mx.transitions.easing.*;

pauseTime = 4000;

xmlImages = new XML();
xmlImages.ignoreWhite = true;
xmlImages.onLoad = loadImages;
xmlImages.load("images.xml");
var ran:Number;

function loadImages(loaded)
{ if (loaded) { xmlFirstChild = this.firstChild; imageFileName = [];
totalImages = xmlFirstChild.childNodes[0].childNodes.length;
for (i=0; i<totalImages; i++) { imageFileName[i] = xmlFirstChild.childNodes[0].childNodes[i].attributes.title;}
randomImage();

}
}

function getRandom () {
ran = Math.round(Math.random() * (totalImages - 1));
trace("ran in function getRandom : " + ran);
}


function randomImage()
{ if (loaded == filesize)
getRandom();
picture_mc._alpha = 0; // Start image clip as invisible
picture_mc.loadMovie(imageFileName[ran],1);
var pictureTweenIn:Tween = new Tween (picture_mc,"_alpha",Normal.easeIn,0,100,1,true);
pictureTweenIn.onMotionFinished = function () {
_root.pause(); // Start pause() function
}
}

function pause() {
myInterval = setInterval(pause_slideshow, pauseTime);
function pause_slideshow() {
clearInterval(myInterval);
var pictureTweenOut:Tween = new Tween (picture_mc,"_alpha",Normal.easeOut,100,0,1,true);
pictureTweenOut.onMotionFinished = function () { /
_root.slideShow(); // Call next Image in order after random()
}
}
}

function slideShow() {
myInterval = setInterval(pause_slideshow, pauseTime);
function pause_slideshow() {
clearInterval(myInterval);
_root.nextImage();
}
}

function nextImage() {
if (ran<(totalImages-1)) {ran++;}
else {ran = 0;}
if (loaded == filesize)
{
picture_mc._alpha = 0; // Start image clip as invisible
picture_mc.loadMovie(imageFileName[ran],1);
var pictureTweenIn:Tween = new Tween (picture_mc,"_alpha",Normal.easeIn,0,100,1,true);
pictureTweenIn.onMotionFinished = function () {
_root.pause(); // Start pause() function
slideShow();
}
}
}

Cipes
10-24-2008, 03:41 PM
It was that _root.pause on the second last line of the AS code,
was keeping it stuck on the 2nd selected photo after the random start was generated.

Removed that and its works like a charm.

Thanks!

CyanBlue
10-24-2008, 06:25 PM
Cool... :)