[php]
<?php
//folder for include file
$C_INC = $C_ROOT."inc/";
//folder for background file
$C_BG = $C_ROOT."background/";
//folder for screensaver file
$C_SS = $C_ROOT."screensaver/";
?>
[/php]
The $C_ROOT var will be sent by php file including the config.inc.php, it refers to the MainFolder folder. We will see this later on the tutorial.
b. const.inc.php
Now create a new "const.inc.php" php file inside the "inc" folder. This file will have all the constants needed (for our tutorial and for your future website).
Here is the file:
[php]
<?php
//all the valid extension for the background
$C_MYEXTENSION = array ( 'jpg', 'jpeg', 'png', 'gif', 'swf' );
//all the valid extensions for the screensaver
$C_MYEXTENSIONSS = array ( 'swf' );
?>
[/php]
In this file we are creating two arrays. The first one is holding all the extension for the background file, the second for the screensaver. I hope it's enough clear. I could put all in one file, but I think it is better this way: one php file for path, another one for constants used.
c. listfile.php
Ok, now we need to make a php file that will check inside a folder all the file, then keep only the file with the extension we want (those which are define in "const.inc.php"). The php file will finally echo the name of the file to the flash movie.
We will save the file in the folder "background" with the name "listfile.php".
[php]
<?php
//give the relative path of the root (MainFolder) of the
//website.
$C_ROOT = "../";
//get the config.inc.php then know all the relative path from the root.
include_once $C_ROOT."config.inc.php";
//include all the constant like our array of extension
include_once $C_INC."const.inc.php";
//opening the current folder
$handle=opendir(".");
//make an empty array that will get our background
//filename of the actual folder
$ourFile = array();
// while next file in the folder exist
while ($file = readdir($handle)) {
// don't check ./ and ../
if ( ($file != ".") && ($file != "..")) {
// split the string in an array, substring
// separate by a '.'
$ext = explode('.', $file);
// how many value in the array
$howmany = count(ext);
// redefine the var ext. Get the extension ie the
// last substring
$ext = strtolower($ext[$howmany]);
// check the extension of the file. If the
// extension is on our array (const.inc.php file)
// then add the filename to our array of
// background
if( in_array ($ext, $C_MYEXTENSION) ) array_push($ourFile, $file);
};
};
// selecting one of the value of the ourFile array, means
// that we are taking one of the background file name
$theFile = $ourFile[array_rand($ourFile)];
//deleting the handler
closedir($handle);
//send it to flash
echo "&thefile=".$theFile."&";
?>
[/php]
Ok, so now we have a php file that echo a filename, like eg:
"Myfirstpic.jpg".
This file is inside the folder "background", so you need to put some background image files inside the "background" folder.
Then test the file by opening your browser like:
[code]localhost/your_Path_to_MainFolder_From_the_Root_of_The_Webserver/background/lisfile.php[/code]
You should have something like:
[code]&thefile=Myfirstpic.jpg& [/code]
For the screensaver it's the same thing.
Just copy this file, and some image files inside the "screensaver" folder.
Ok so all the php side is done :)
Let's go in Flash now !!! (I was impatient too)
IV. THE FLASH FILES
a. What we need
Ok. So we want to make a class that load a random image file.
The name of the image file is sent by the php file.
We want a dynamic textfield displaying how many bytes are load in total bytes.
We will add some extra stuff:
b. Add new method to MovieClip class
[as]
MovieClip.prototype.scaleToStage= function () {
//get the value that help me rescaling the mc
this._xscale = 100;
this._yscale = 100;
var W = this._width;
var H = this._height;
var RW = Stage.width;
var RH = Stage.height;
//rescaling
this._xscale = (RW/W)*100;
this._yscale = (RH/H)*100;
this._x = 0;
this._y = 0;
}
[/as]
Some brief explanations:
Why did we use the OLD WAY to add new method to the MovieClip?
Because we need to be able to create new empty movie clip at runtime, and if we are using the new way (means defining a new class extended MovieClip) we are not able to associate at runtime the new class to the MovieClip object created (update 2006: this is no more true ,check for MCE).
This function is rescaling the MovieClip to the stage's size and setting _x and _y to 0.
c. The loadBackground class
Ok so let's start declaring our loadBackground class now:
[as]
class froggies.loadBackground {
private var _nDelay:Number;
private var _nSICheck:Number;
private var _nStartTime:Number;
private var _nModulo:Number;
private var _oBackground:Object;
private var _mcBackground:MovieClip;
private var _mcFirstBackground:MovieClip;
private var _mcSecondBackground:MovieClip;
private var _mclBackground:MovieClipLoader;
private var _sFolder:String;
private var _tfDispInfo:TextField;
function loadBackground (mcBackground, tfDispInfo, nDelay, sFolder) {
//getting the properties send by the user
_nDelay = nDelay;
_mcBackground = mcBackground;
_tfDispInfo = tfDispInfo;
_sFolder = sFolder;
_nStartTime = null;
_nSICheck = null;
_nModulo = -1;
_oBackground = new Object();
_mclBackground = new MovieClipLoader();
_mcFirstBackground = _mcBackground.createEmptyMovieClip("mc0", 0);
//if you want to change background every X milliseconds then need another mc to make transition without waiting the loading
if (_nDelay != -1) _mcSecondBackground = _mcBackground.createEmptyMovieClip("mc1", 1);
_oBackground._owner = this;
//defining my object
_oBackground.onLoadStart = function (cible_mc) {
cible_mc._visible = false;
cible_mc.gotoAndStop(1);
this._owner._tfDispInfo.text = "Loading starting";
this._owner._tfDispInfo._visible = true;
}
_oBackground.onLoadProgress = function (cible_mc, loadedBytes, totalBytes) {
cible_mc._visible = false;
cible_mc.gotoAndStop(1);
this._owner._tfDispInfo.text = "Loading "+Math.ceil(loadedBytes/1024)+" / "+Math.ceil(totalBytes/1024)+" kbytes";
}
_oBackground.onLoadComplete = function (cible_mc) {
cible_mc._visible = false;
cible_mc.gotoAndStop(1);
this._owner._tfDispInfo._visible = false;
}
_oBackground.onLoadInit = function (cible_mc) {
//scale the mc to the size of the stage;
this._owner._mclBackground.removeListener(this._owner._oBackground);
cible_mc.swapDepths(1);
cible_mc._parent.scaleToStage();
cible_mc._visible = true;
cible_mc.gotoAndPlay(1);
if(this._owner._nDelay != -1) this._owner.nextBackground(cible_mc);
}
_oBackground.onLoadError = function (cible_mc, errorCode) {
this._owner._mclBackground.removeListener(this._owner._oBackground);
}
Init();
}
private function Init():Void {
newBackground();
}
private function newBackground ():Void {
_tfDispInfo._visible = true;
_tfDispInfo.text = "Loading php file";
var _lvBackground:LoadVars = new LoadVars();
_lvBackground._owner = this;
//who's the next container?
_nModulo++;
var num:Number = _nModulo%2;
//to make url unique
var dMyDate:Date = new Date();
var unique:Number = dMyDate.getTime();
_lvBackground._nextContainer = _mcBackground["mc"+num];
//loading the php file that returning the background
_lvBackground.onLoad = function (ok):Void {
if(ok) {
/*********************************************************
Add the listener to the MoveiClipLoader object and
load the background inside the movieclip background
**********************************************************/
this._owner._mclBackground.unloadClip(this._nextContainer);
this._owner._mclBackground.addListener(this._owner._oBackground);
this._owner._mclBackground.loadClip(this._owner._sFolder+this.thefile, this._nextContainer);
}
}
_lvBackground.load(_sFolder+"listfile.php?"+unique);
}
private function nextBackground ():Void {
_nStartTime = new Date().getTime();
_nSICheck = setinterval(checktime, 48, this);
}
private function checktime (_owner):Void {
var nNow:Number = new Date().getTime();
if (nNow - _owner._nStartTime > _owner._nDelay) {
clearInterval(_owner._nSICheck);
_owner.newBackground();
}
}
}
[/as]
d. How this class is working?
We are creating a new class inside which is inside froggies package (class froggies.loadBackground ).
We are defining all the properties (var/object) used in this class. All are private (protected in fact).
Then is coming the constructor where we are setting up all the properties.
Four properties are setting up by parameters. It's the delay (if equal to -1 means no delay so no reloading), the MovieClip (container) which is holding the two sub containers, the textField which is displaying the bytes loaded, and the folder where the files (image and php) are.
The other are the
Ok then we are adding a new property to the _ oBackground object defining its parent: (_oBackground._owner = this;).
Then we are defining all the methods of this object:
Then we are calling Init() function. Why making a function with just one call to another one? Because when we will do the screensaver class the Init will not be the same, but the constructor will be. That's all. If we are putting the newBackground call inside the constructor we must rewrite ALL the constructor for the screensaver class. We will see that later.
Ok now the functions (methods) of the class. I put comments on the code, so I think it's enough. That's why here I'm telling briefly what the function are doing.
[as]class froggies.loadScreensaver extends froggies.loadBackground {
//inherit from the loadBackground class
private var _mcContinue:MovieClip;
function loadScreensaver (mcBackground, tfDispInfo, nDelay, sFolder, btn) {
//inherit from the loadBackroung constructor
super(mcBackground, tfDispInfo, nDelay, sFolder);
_mcBackground._owner = this;
_mcBackground.onMouseMove = function() {
this._owner.clearDelay();
}
//btn you click to stop the screensaver
_mcContinue = btn;
//hidding the button
_mcContinue._visible = false;
_mcContinue._owner = this;
_mcContinue.onRelease = function () {
this._owner._mcBackground.onMouseMove = function() {
this._owner.clearDelay();
}
this._owner.clearDelay();
}
_oBackground.onLoadComplete = function (cible_mc) {
delete(this._owner._mcBackground.onMouseMove);
this._owner._mcContinue._visible = true;
cible_mc._visible = false;
cible_mc.gotoAndStop(1);
this._owner._tfDispInfo._visible = false;
}
}
private function Init():Void {
nextBackground();
}
public function clearDelay ():Void {
if(_nSICheck != null) {
//clearing the interval
clearInterval(_nSICheck);
_nSICheck = null;
//removing the listener --> stop loading the contents if loading
_mclBackground.removeListener(_oBackground);
_mclBackground.unloadClip(_mcBackground.mc0);
_mclBackground.unloadClip(_mcBackground.mc1);
//hidding the button
_mcContinue._visible = false;
//hidding the textField
_tfDispInfo._visible = false;
//hiding the container
_mcBackground.mc0._visible = false;
_mcBackground.mc1._visible = false;
//calling the nextScreensaver
nextBackground();
}
}
}
[/as]
[as]import froggies.loadBackground
//This will load the Background only once
aBackground = new loadBackground(_root.backgroundImage, _root.infoBackground, -1, "./background/");
//This will load the Background every 1 minute
//aBackground = new loadBackground(_root.backgroundImage, _root.infoBackground, 60000, "./background/");
[/as]
[as]import froggies.loadScreensaver
//This will load the Screensaver every 1 minute
aScreensaver = new loadScreensaver(_root. screensaverImage, _root.infoScreensaver, 60000, "./background/");
[/as]