ActionScript编程实战项目新闻发布系统.docx
《ActionScript编程实战项目新闻发布系统.docx》由会员分享,可在线阅读,更多相关《ActionScript编程实战项目新闻发布系统.docx(14页珍藏版)》请在冰豆网上搜索。
![ActionScript编程实战项目新闻发布系统.docx](https://file1.bdocx.com/fileroot1/2022-11/16/5055334f-56f8-46d4-bcd3-58f292c91f05/5055334f-56f8-46d4-bcd3-58f292c91f051.gif)
ActionScript编程实战项目新闻发布系统
ActionScript编程实战项目:
新闻发布系统
对于大多数的网站来说,新闻发布系统是必不可少的功能之一,通过该系统可以将指定的新闻以列表的形式显示在网页中。
在Flash网站中,同样可以实现新闻发布功能,只需要通过ActionScript与服务器端程序进行通信,以添加和显示新闻。
新闻发布系统如图26-1所示。
图26-1新闻发布系统
制作过程:
1.创建数据库及ASP页面
(1)在网站根目录的Database文件夹中,新建名称为News的Access2007数据库。
然后,在该数据库中新建一个名称为News的数据表,并创建字段名称和数据类型,如表26-1所示。
表26-1News数据表的字段
字段名称
数据类型
ID
自动编号
title
文本
type
文本
author
文本
from
文本
time
文本
content
备注
(2)在网站根目录中新建名称为conn的ASP页面,并在该页面中输入连接数据库的代码,并检查连接异常,如下所示。
<%
OnErrorResumeNext
'使程序在发生错误那条命令后继续执行
Dimdbpath,conn
'声明变量
dbpath="Database/News.accdb"
'数据库的相对路径
Setconn=Server.CreateObject("adodb.connection")
'创建connection对象,该对象用于对数据库进行连接操作
conn.Provider="Microsoft.ACE.oledb.12.0"
'定义数据库连接的驱动程序
conn.OpenServer.Mappath(dbpath)
'对指定的数据库进行连接操作
IfErr.Number<>0Then
'如果在连接数据库过程中出现错误
Response.Write("无法建立连接,请检查错误信息:
"&Err.Number&""&Err.Description)
'在网页中输出错误数目和错误描述
Err.Clear
'将错误清除
Response.End()
'结束Response对象输出
EndIf
'结束判断语句
%>
(3)在网站根目录下新建名称为NewsXML的ASP页面,该页面查询News数据表中的所有新闻,并以XML格式输出在网页中,如下所示。
--#includefile="conn.asp"-->
<%
Dimrs,sql,i
setrs=Server.CreateObject("Adodb.Recordset")
sql="select*fromNewsorderbyiddesc"
rs.Opensql,conn,1,1
'查询News数据表中的所有新闻信息
Ifrs.EofThen
Response.Write("未发布任何新闻!
")
'在网页中输出提示信息
Response.End()
'结束Respnose对象输出
Else
'将所有新闻以XML格式输出到网页中
i=0
Response.Write("
xmlversion='1.0'?
>")
Response.Write("")
dowhileiResponse.Write("")
rs.MoveNext
i=i+1
loop
EndIf
Response.Write("")
setrs=nothing
rs.close
setconn=nothing
conn.close
%>
(4)在网站根目录下新建名称为save的ASP页面,该页面将Flash表单中提交的新闻信息存储到数据库中,如下所示。
--#includefile="conn.asp"-->
<%
Dimtitle_post,type_post,author_post,from_post,time_post,content_post
Dimrs,sql
title_post=Trim(Request.Form("title"))
type_post=Trim(Request.Form("type"))
author_post=Trim(Request.Form("author"))
from_post=Trim(Request.Form("from"))
time_post=Trim(Request.Form("time"))
content_post=Trim(Request.Form("content"))
'从表单中获取提交的新闻信息,并存储到相应的变量中
if(title_post<>""&type_post<>""&author_post<>""&from_post<>""&time_post<>""&content_post<>"")then
setrs=Server.CreateObject("Adodb.Recordset")
sql="select*fromNews"
rs.Opensql,conn,1,3
'以可写的方式打开News数据表
rs.addNew
'创建一条新记录
rs("title")=title_post
rs("type")=type_post
rs("author")=author_post
rs("from")=from_post
rs("time")=time_post
rs("content")=content_post
'将新闻信息存储到数据表的相应字段中
rs.Update
'保存修改
setrs=nothing
rs.close
endif
setconn=nothing
conn.close
%>
2.创建Flash及ActionScript文件
(1)新建1003×600像素的空白文档,将所有素材图像导入到【库】面板,并将背景图像拖入到舞台,如图26-2所示
图26-2拖入背景图像
(2)在【库】面板中将“提交按钮”和“返回列表”元件导出为Submit和listBtn自定义类,如图26-3所示。
图26-3导出为自定义类
(3)在网站根目录中新建名称为News的ActionScript文件,在文件中使用import语句导入所需的类,以及创建包、New类和News()主函数,如下所示。
package{
importflash.display.Sprite;
importflash.text.TextField;
importflash.text.TextFieldType;
importflash.display.MovieClip;
importflash.events.MouseEvent;
importflash.events.Event;
import.URLLoader;
import.URLRequest;
import.URLVariables;
import.URLRequestMethod;
import.navigateToURL;
importflash.utils.setInterval;
importflash.utils.clearInterval;
importflash.text.TextFieldAutoSize;
importflash.utils.getTimer;
importflash.system.System;
publicclassNewsextendsSprite{
publicfunctionNews(){
//主函数体
}
}
}
(4)在News类中声明用于输入和显示新闻的文本字段、存储新闻信息的数组,以及用于加裁外部网页的对象,如下所示。
privatevartitleInput:
TextField;
privatevartypeInput:
TextField;
privatevarauthorInput:
TextField;
privatevarfromInput:
TextField;
privatevartimeInput:
TextField;
privatevarcontentInput:
TextField;
//添加新闻的输入文本框
privatevarInput_MC:
MovieClip;
privatevarrequest:
URLRequest;
privatevarloader:
URLLoader;
privatevarvariables:
URLVariables;
privatevarurl:
String;
privatevarnum:
int;
privatevarLoop:
int;
//限制显示的新闻条数
privatevarNews_MC:
MovieClip;
privatevartitleTxt:
TextField;
privatevartypeTxt:
TextField;
privatevarauthorTxt:
TextField;
privatevarfromTxt:
TextField;
privatevartimeTxt:
TextField;
privatevarcontentTxt:
TextField;
//显示新闻的文本框
privatevari:
int=0;
privatevarxml:
XML;
privatevartitleArr:
Array=newArray();
//存储留言标题的数组
privatevartypeArr:
Array=newArray();
//存储新闻类别的数组
privatevarauthorArr:
Array=newArray();
//存储作者姓名的数组
privatevarfromArr:
Array=newArray();
//存储新闻出处的数组
privatevartimeArr:
Array=newArray();
//存储发布时间的数组
privatevarcontentArr:
Array=newArray();
//存储新闻内容的数组
privatevarnewsURL:
String;
(5)创建名称为showNews()的函数,该函数创建用于存储新闻对象的窗口,及加载新闻,如下所示。
privatefunctionshowNews():
void{
News_MC=newMovieClip();
addChild(News_MC);
//用于存储新闻