用Flash和Php动态的载入背景:

      Flash 2004-8-2 16:54
作者:Xavier MARTIN, xxlm AT netcourrier DOTcom, www.webbymx.net
1.简介:
这篇文章的目的是用一个简单的方法去更换你主页的背景.文本区域显示载入了多少字节.
2.建立工作区:
MainFolder是存放swf文件的文件夹
在MainFolder里面建立一个名叫background的文件夹
screensaver:存放图像文件和PHP文件
inc:存放const.inc.php文件的文件夹(站点中的其他的所有文件)
3.PHP文件:
a. config.inc.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/";
?>
---------------------------------------
$C_ROOT :这个变量将会被发送的.
b.const.inc.php这个文件被存到inc这个文件夹里面.这个文件包含所有的常量:
---------------------
<?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' );
?>

----------------------
这个文件中包含有两个数组:
第一个包含所有背景文件的扩展名:
第二个存入到Screensaver里面.
c. listfile.php:
现在我们需要一个检查插入到文件夹中的文件的文件,保持那些文件是我们想要的扩展名.这些文件的名字最终会在flash的movie中输出的.
我们会用listfile.php中的名字把它们保存到background这个文件夹里面.
-------------------------------
<?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."&";
?>


------------------------------
现在我们有了一个文件名为Myfirstpic.jpg的文件了
这些文件被 保存到了background这个文件夹里面.所以我需要把一些图像文件存入到这个文件夹./
测试时这样的:打开浏览器:
localhost/your_Path_to_MainFolder_From_the_Root_of_The_Webserver/background/lisfile.php
你应该看到这个&thefile=Myfirstpic.jpg &
PHP的部分就到这里,下面是Flash的部分
a.写一个类来随即的载入一个图像文件.
一个动态文本区域显示载入的情况.
再加一些额外的东西:
背景能过X秒刷新.
----------------
LoadVars 用来载入PHP文件.
MMoiveClip作为主容器和子容器.
MovieClipLoader and listener 用来获取载入的字节数和监听事件
变量Numbers用来保存延时.
extField用来显示信息
----------------
在MoiveClip里面增加一个新的方法:
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;
}

-----------------------
为什么我不用旧的方法去为MovieClip新的方法呢?
因为我希望在运行的时候能建立一个空的影片剪辑.
c.载入背景类:
----------------------------
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);
//trace ("*********Premiere occurrence de mon_mcl*********");
//trace ("CODE ERREUR = " + codeErreur);
//trace ("Votre chargement a 飨ou頳ur le clip = " + cible_mc + "\n");
}

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();
}
}
}
------------------------------------------
d.这个类是如何运行的?
我们新建一个类插入到包里面,在这个类里面定义所有的私有属性.其中有四个属性被设置了参数.分别是:时间延迟:(如果是-1的话说明没有延迟,也就是没有重载) 影片剪辑:包含有两个子容器
文本域: 显示多少字节被载入.包含有文件的文件夹.
_nStartTime :如果currentTime - _nStartTime > _nDelay 我们将会呼叫下一个背景.
_nSICheck 控制呼叫的时间间隔
_nModulo :确认我们把新背景载入到子容器里面
_oBackground :监听MovieClipLoader
_mclBackground :MovieClipLoader
我们可以增加一个新的属性给_oBackground 定义到他的父级:(_oBackground._owner = this)
然后我们可以给这个队像定义所有的属性
onLoadStart:隐藏子集 停止到第一帧 显示文本域
onLoadProgress:显示载入的过程
onLoadcomplete,载入完成,隐藏文本域
onLoadInit 删除子集和监听的连接,显示子集放缩到场景大小,并播放它,呼叫检查下一个背景的函数.
onLoadError 如果我们删除Listener和MovieClipLoader之间的连接.
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}