BLOG zzy.my

合抱之木, 生于毫末; 九层之台, 起于累土; 千里之行, 始于足下。

Flash动态读取xml数据来实现评论功能

结构为
comment_list.swf
comment_template.swf
comment_markup.swf
write_comment.swf
数据(comment_list1.xml,comment_list2.xml,comment_list3.xml)

首先制作 comment_template 这个文件, 作为一个模板,所有的评论都在这个文件里面显示

如图所示,template文件夹里面的各个layer都是图像,大家可以根据自己的爱好来修改

重点介绍一下as这个layer,可以看到这里主要是构建一个elemnts的array来保存所有在scene1里面的object,也就是各个MC以及textfield,在这个array里面,每个成员有如下属性
name,这个成员的名称
obj,这个成员在scene1里的实际object
distance,这个成员和他下面成员的距离(本例不涉及)

_parent.getInfo(this, elements); 这句的意思是调用这个mc上一层的函数getInfo并且把数组和本身传入,最后是测试语句,看你是不是把所有object都包含了

stop();
elements = new Array();
elements.push({name:"titles", obj:this.titles, distance:0});
elements.push({name:"titleBG", obj:this.titleBG, distance:0});
elements.push({name:"contentOfMask", obj:this.contentOfMask, distance:0});
elements.push({name:"contents", obj:this.contents.contents, distance:0});
elements.push({name:"contentBG", obj:this.contentBG, distance:0});
elements.push({name:"sep", obj:this.sep, distance:0});
elements.push({name:"arrows", obj:this.arrows, distance:0});
elements.push({name:"authorip", obj:this.authorip, distance:0});
elements.push({name:"infoBG", obj:this.infoBG, distance:0});
elements.push({name:"templateBG", obj:this.templateBG, distance:0});
_parent.getInfo(this, elements);
/*for(i in elements){
trace(i);
trace(elements[i].name);
trace(elements[i].obj._height);
trace("=====");
}*/


下面就是重点的comment_markup.swf
comment_markup.swf其实就是一个整合xml和swf的中间件

如图所示,这个flash里面只有一个as,但确实非常×××

stop();
XML.prototype.ignoreWhite = true;
//set action
var page_current = _root.page_current;
var page_total;
var page_comment;
//var loadXML = "/stream/showMessageServlet.dojQuery15207059699629201319_1331657721766programID=" + _root.programID + "&catalogNumber=" + _root.catalogNumber + "&current_page=" + _root.page_current;
var loadXML = "comment_list"+page_current+".xml";
//trace(loadXML);
comment_listXML = new XML();
comment_listXML.load(loadXML);
//comment_listXML.load("comment_list1.xml");
comment_listXML.onLoad = function(success) {
comment_listNode = this.firstChild.childNodes;
//trace(comment_listNode);
if (success == false) {
// No
trace("The XML file failed to load");
return;
}
// Check for a malformed XML document
if (this.status != 0) {
trace("The XML document was invalid. Error: "+this.status);
return;
}
// Make sure the first node is the <FlashBlog> node
if (this.firstChild.nodeName.toLowerCase() != "comment_list") {
trace("First node wasn't the expect <comment_list> node.");
return;
}
//trace(comment_listNode.length);
for(i=0;i<comment_listNode.length;i++){
if(comment_listNode[i].nodeName == "page_info"){
get_page_info(comment_listNode[i].childNodes);
}
if(comment_listNode[i].nodeName == "comment_info"){
get_comment_info(comment_listNode[i].childNodes);
}
}
};
//set function
function getInfo (mc, elementsArray){
assoc = new Object();//create a new object which associate the commentArray and templateMC
for(e=0;e<elementsArray.length;e++){//loop throught the elementsArray

if (elementsArray[e].name == "titles") {
assoc.titles = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "titleBG"){
assoc.titleBG = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "contentOfMask"){
assoc.contentOfMask = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "contents"){
assoc.contents = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "contentBG"){
assoc.contentBG = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "arrows"){
assoc.arrows = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "authorip"){
assoc.authorip = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "sep"){
assoc.sep = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "infoBG"){
assoc.infoBG = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else if(elementsArray[e].name == "templateBG"){
assoc.templateBG = {ref:elementsArray[e].obj, dist:elementsArray[i].distance};
}else{
trace("else");
}
}
counterArray[id]= 0; //set for the looptitles function so that they know the started point
counterMC[id]= 0;//set for the show content
//get titles value
// assoc.titles.ref.text = commentArray[id].comment_title;
loopTitles[id] = setInterval(showTxt,60,assoc.titles.ref,commentArray[id].comment_title,id);
trace("counterArray "+id+"is"+counterArray[id]);
//trace(loopTitles[id]);
assoc.titles.ref.autoSize = "left";;
//set titleBG height and y
//set contets value
assoc.contents.ref.text = commentArray[id].comment_content;
assoc.contents.ref.autoSize = "left";
assoc.contents.ref.wordWrap = true;
assoc.contents.ref.multiline = true;
//set assoc.contentOfMask height
//assoc.contentOfMask.ref._y = assoc.contents.ref._y;
assoc.contentOfMask.ref._height = assoc.contents.ref._height;
//get contentBG height and y
assoc.contentBG.ref._height = assoc.contents.ref._height + 8;
//get the sep y
assoc.sep.ref._y = assoc.contentBG.ref._y + assoc.contentBG.ref._height;
//get the arrow y
assoc.arrows.ref._y = assoc.sep.ref._y + 8;
//get the authorip y
assoc.authorip.ref._y = assoc.arrows.ref._y -2;
assoc.authorip.ref.text = "作者: " + commentArray[id].comment_author + " IP: " + commentArray[id].comment_ip;
//get infoBG _y
assoc.infoBG.ref._y = assoc.contentBG.ref._y + assoc.contentBG.ref._height;
//get the templateBG height
assoc.templateBG.ref._height = assoc.infoBG.ref._y + assoc.infoBG.ref._height + 20;
//postion the comment
if (id == commentArray.length-1) {
postionComment();
}
id++
};
function postionComment(){
// Loop through each one
for (i=0; i<commentArray.length; i++) {
if (i == 0) {
this["comment"+i+"_mc"]._y = 0;
} else {
this["comment"+i+"_mc"]._y = this["comment"+(i-1)+"_mc"]._y+this["comment"+(i-1)+"_mc"]._height;
//trace(this["comment"+i+"_mc"]._y);
}
commentMC[i] = {obj:this["comment"+i+"_mc"]};
//trace(commentMC[i].obj);
//放入commentMC数组
}

_parent.refreshPane();

if(i==commentArray.length){
//trace("loopMC");
loopMC = setInterval(showContents,300,commentMC,i);
}

}
function get_page_info(page_infoNode){
for(p = 0; p < page_infoNode.length;p++){
//trace(page_infoNode[p].childNodes[0].nodeValue);
/*if(page_infoNode[p].nodeName=="page_current"){
page_current = page_infoNode[p].firstChild.nodeValue;
}*/
if(page_infoNode[p].nodeName=="page_total"){
page_total = page_infoNode[p].firstChild.nodeValue;
}
if(page_infoNode[p].nodeName=="page_comment"){
page_comment = page_infoNode[p].firstChild.nodeValue;
}
/*trace(page_infoNode[p].nodeName);
trace(page_infoNode[p].firstChild.nodeValue);
trace("=====");*/
}
_root.page_current_info.text = "第" + _root.page_current + "页";
_root.page_total = page_total;
_root.page_comment = page_comment;
_root.page_info.text = "当前有评论"+ page_comment + ",共" + page_total + "页";
}
function get_comment_info(comment_infoNode){
for(c=0; c<comment_infoNode.length; c++){
comment_id = comment_infoNode[c].attributes.id;
get_comment(comment_infoNode[c].childNodes,comment_id);
}
};
function get_comment(comment_node,comment_id){
comment_id_data = comment_id;
for(m=0;m<comment_node.length;m++){
if(comment_node[m].nodeName == "comment_title"){
//trace(comment_title_data);
comment_title_data = comment_node[m].firstChild.nodeValue;
}else if(comment_node[m].nodeName == "comment_author"){
comment_author_data = comment_node[m].firstChild.nodeValue;
}else if(comment_node[m].nodeName == "comment_content"){
comment_content_data = comment_node[m].firstChild.nodeValue;
}else if(comment_node[m].nodeName == "comment_time"){
comment_time_data = comment_node[m].firstChild.nodeValue;
}else if(comment_node[m].nodeName == "comment_ip"){
comment_ip_data = comment_node[m].firstChild.nodeValue;
}
}
commentArray.push({comment_id:comment_id_data,comment_title:comment_title_data,comment_author:comment_author_data,comment_content:comment_content_data,comment_ip:comment_ip_data,comment_time:comment_time_data});
//debug beign
ArrayID = comment_id_data - 1;
/*trace("A new Recore called Comment id"+ comment_id_data + " has been added to CommentArray");
trace(commentArray[ArrayID].comment_title);
trace(commentArray[ArrayID].comment_author);
trace(commentArray[ArrayID].comment_content);
trace(commentArray[ArrayID].comment_time);
trace(commentArray[ArrayID].comment_ip);*/
//debug end
loadTemplates();
//load template
};
loadTemplates = function () {
// Loop through all of the comments
for (i=0; i<commentArray.length; i++) {
// Create a new movie clip to hold the template
commentMC = this.createEmptyMovieClip("comment"+i + "_mc", depth++);
// Load in the template
commentMC.loadMovie("comment_template.swf");
//trace("loadTemplates");
}
};
function showTxt(textfield,txt,id){
//trace("this "+txt);
counterArrayid=counterArray[id];
if(counterArrayid < txt.length){
//trace();
textfield.text += txt.charAt(counterArrayid);
counterArrayid ++ ;
counterArray[id]=counterArrayid;
//trace("counterArray[ "+id+"] is"+textfield.text);
}else{
loop=loopTitles[id];
clearInterval(loop);
trace("finished"+loop);
if(id==(loopTitles.length-1)){
intervaling1 = false;
}
if(intervaling1==false&&intervaling2==false){
_root.intervaling = false;
}else{
_root.intervaling = true;
}
trace(_root.intervaling);
//检查是否读取数据
}
};
function showContents(commentMC,i){
if(s<i){
commentMC[s].obj.contentOfMask.gotoAndPlay("show");
trace("commentMC is"+commentMC[s].obj.contentOfMask);
}else{
clearInterval(loopMC);
intervaling2 = false;
if(intervaling1==false&&intervaling2==false){
_root.intervaling = false;
}else{
_root.intervaling = true;
}
trace(_root.intervaling);
//检查是否读取数据
s=0;
}
s++;
};
//set variables
var comment_id="";
var comment_title="";
var comment_author="";
var comment_content="";
var comment_ip="";
var comment_time="";
var commentArray:Array = new Array();
var depth = 1; //load movie clip into sepcific level
var id = 0; //getInfo id
//var counterArray:Array = new Array(0,0,0,0,0,0);//订制一页显示多少评论
var counterArray:Array = new Array();
var counterMC:Array = new Array();
var commentMC:Array = new Array();//把加载的mc加入mc
var loopTitles:Array = new Array();
var titlesArray:Array = new Array();//titles show
var s = 0;//for the showContents function
var intervaling1=true;//初始化判断是否数据在装载
var intervaling2=true;//初始化判断是否数据在装载

大家可别被这些代码吓者,其实很简单只要把他们分开来研究

 

1,看一下要读取的xml,这个xml总体来说可分为两部分

page_info 读取comment页面时候的属性

comment_info 里面每一个评论的属性

<?xml version="1.0"?>
<comment_list>
<page_info>
<page_total>3</page_total>
<page_current>2</page_current>
<page_comment>20</page_comment>
</page_info>
<comment_info>
<comment id="1">
<comment_title>电影评论vincentdiada</comment_title>
<comment_author>vincent</comment_author>
<comment_time>11111</comment_time>
<comment_ip>11111</comment_ip>
<comment_content>242424242</comment_content>
</comment>
</comment_info>
<comment_info>
<comment id="2">
<comment_title>23456</comment_title>
<comment_author>vincent</comment_author>
<comment_time>11111</comment_time>
<comment_ip>11111</comment_ip>
<comment_content>0d0a0</comment_content>
</comment>
</comment_info>
<comment_info>
<comment id="3">
<comment_title>电影评论vincentdiada</comment_title>
<comment_author>vincent</comment_author>
<comment_time>11111</comment_time>
<comment_ip>11111</comment_ip>
<comment_content>242424242</comment_content>
</comment>
</comment_info>
<comment_info>
<comment id="4">
<comment_title>23456</comment_title>
<comment_author>vincent</comment_author>
<comment_time>11111</comment_time>
<comment_ip>11111</comment_ip>
<comment_content>0d0a0</comment_content>
</comment>
</comment_info>
</comment_list>

comment_list.swf 就是显示页面了

 

同样也只有一帧



如图所示,其中里面的as

stop();
import mx.controls.Alert;
var page_current=1;
var UserID;
var UserIP;
var programID;
var catalogNumber;
var intervaling = true;
//set action
loadContent();
最前一页.emptyBtn.onRelease = function(){
if(intervaling == true){
errorShow("失败","正在读取数据中,请等待");
}else{
if((page_current-1)>0){
page_current=1;
page_current_info.text = "第" + page_current + "页";
loadContent();
}else{
errorShow("失败","已经是最前一页了");
}
}
}
前一页.emptyBtn.onRelease = function(){
if(intervaling == true){
errorShow("失败","正在读取数据中,请等待");
}else{
if((page_current-1)>0){
page_current--;
page_current_info.text = "第" + page_current + "页";
loadContent();
}else{
errorShow("失败","已经是最前一页了");
}
}
}
后一页.emptyBtn.onRelease = function(){
if(intervaling == true){
errorShow("失败","正在读取数据中,请等待");
}else{
if((page_total-page_current)>0){
page_current++;
page_current_info.text = "第" + page_current + "页";
loadContent();
}else{
errorShow("失败","已经是最后一页了");
}
}
}
最后一页.emptyBtn.onRelease = function(){
if(intervaling == true){
errorShow("失败","正在读取数据中,请等待");
}else{
if(page_total!=page_current){
page_current=page_total;
page_current_info.text = "第" + page_current + "页";
loadContent();
}else{
errorShow("失败","已经是最后一页了");
}
}
}
发表评论.emptyBtn.onRelease = function(){
var write_comment = "write_comment.html?UserID=" + UserID + "&UserIP=" + UserIP + "&programID=" + programID + "&catalogNumber=" + catalogNumber;
getURL(write_comment, "_self","POST");
}
//set styles
comment_list.setStyle("backgroundColor", 0xC9D1DA);
_global.style.setStyle("themeColor", 0x97A7B7);
//set function
function loadContent (){
comment_list.loadScrollContent("comment_markup.swf");
}
function errorShow(eventTextFrom,erroCode){
Alert.okLabel = eventTextFrom;
Alert.show(erroCode,"评论帮助" ,Alert.OK, this);
if(eventTextFrom=="成功"){
getURL("javascript :window.close();");
}
};
//set variables
nav.text = " 【新华社】-评论列表-查看评论";
var programID;
var catalogNumber;


write_comment.swf 发表评论页面



这个flash主要是传参数给后台,并且插入到数据库,不过之前要检查评论是否有非法字等

stop();
import mx.controls.Alert;
var programID;
var UserID;
var UserIP;
var catalogNumber;
//set action
我同意发表.emptyBtn.onRelease = function(){
checktextareaResult = checktextarea(textarea);
if(checktextareaResult){
comment.UserID = UserID;
comment.UserIP = UserIP;
comment.UserComment = textarea.text;
comment.UserCommentTitle = commentTitle.text;
comment.programID = programID;
comment.catalogNumber = catalogNumber;
comment.sendAndLoad("stream/sendMessageServlet.do",comment,"POST");
for(w in comment){
trace(w);
trace(comment[w]);
}
comment.onLoad = function(success){
if(success){
if(comment.resultSend == "true"){
errorShow("��功","发送成功,请等待审批");
}else{
errorShow("失败","网络发生错误,请稍后");
}
}else{
errorShow("失败","网络连接失败,请稍后");
}
};
}else{
errorShow("失败","发现非法字或者多于300字,请修改");
}
};
我不同意发表.emptyBtn.onRelease = function(){
getURL("javascript : window.close()");
//trace();
};
btn_read.onRollOver = function(){
_root.document.gotoAndPlay("begin");
};
btn_read.onRollOut = function(){
_root.document.gotoAndPlay("end");
};
//set styles
_global.style.setStyle("themeColor", "haloBlue");
//set function
function errorShow(eventTextFrom,erroCode){
Alert.okLabel = eventTextFrom;
Alert.show(erroCode,"评论帮助" ,Alert.OK, this);
if(eventTextFrom=="成功"){
getURL("javascript :window.close();");
}
};
function checktextarea(textarea){
if(textarea.text.length<3 ) {
return(false);
}else if(isIllegalString(textarea.text,"@#$%^&*()_+=-|\/><{}[]'~`")) {
return(false);
}else{
return(true);
}
};
function isIllegalString (InString,RefString){
if(InString.length == 0||InString.length > 300){
return (true);
}else{
for (Count=0; Count < InString.length; Count++) {
//TempChar= InString.substring (Count, Count+1);
commentArray[Count] = InString.substr (Count, 1);
if (RefString.indexOf(commentArray[Count])!=-1){
isIllegalStringResult = true;
Count = InString.length;
}else{
isIllegalStringResult = false;
}
}
if(isIllegalStringResult){
return (true);
trace("true");
}else{
return (false);
trace("false");
}
}
}
//set variables
nav.text = " 【新华社】-发表评论-提交评论";
var comment:LoadVars = new LoadVars();
var commentArray:Array = new Array();
Loading