七彩课堂[FLASH动画教程系列]
flash计算器的制作[上]
[学习目标]:掌握if语句的格式和功能,通过实际例子进一步的掌握变量、运送符、函数等概念,
在上个教程我给大家说说数据类型、变量、运送符、函数等几个概念,下面我再给大家介绍下if语句,然后就通过制作一个flash的计算器来更好的掌握这些概念。
有了前面大家的学习,我们可以看到as控制动画的表演都是按照时间轴的顺序,有序的播放的,as的语句都是从上至下运行的,当然我们可以让as有点人的逻辑思维,根据具体的情况判断下,再去播放。这就需要我们了解as语句的三种结构:顺序、选择、循环结构。
一、三种结构简说
对于顺序结构,很好理解,前面我们写的都是顺序的,按我们写的as语句的顺序,一行行的从上到下的执行。选择结构就是根据先判断条件,条件成立去做一系列的事情,不成立做另一系列的事情。循环就是反复去做某些事情了,要它停止下来,你必须给它停止的条件。
二、选择结构分2种类型
如果我们在创作动画时需要根据具体的情况来确定动画的播放,就要用到选择了,对as来说有2种类型的选择,一是就2种情况来选择的if语句,二是有多种情况可选择的switch语句。比如我们投硬币决定今天谁来请客,就是一 个判断,就2种情况,正面和反面,如果在饭馆我们谁点的菜就放在谁边,对菜就是多选择了。就用switch表示了。
三、如何写if语句
一般有下面3种情况:
1、最近简单的,一个选择。
if(表达式) {语句 ;\\如果表达式的值为真,就执行语句.否则什么也不做.}
2、标准格式
if(表达式){ 语句1}
else
{语句2;}\\如果表达式的值为真,就执行语句1,如果为假就执行语句2.
3、多条件嵌套
if(表达式1) {语句1;}
else if (表达式2) {语句2;}
else if (表达式3) {语句3;}
else if (表达式n) {语句n;}
else {语句 m;}
这个嵌套要注意的就是层次关系,不能套错了,在这里先理解吧。
比如上面的例子我们可以写成这样:
if ( 硬币的面的情况是正面?)
{我掏钱请你吃饭;}
else
{你掏钱请我吃了;}
下面我们就看个例子:
要求:做个简单的计算器
步骤:
1、新建flash文档。
2、在新建的图层calc_skin画个计算器的外壳样子,这个是自己的美术功底了,不画也可以的。
3、新建按扭元件Digit_btn。
4、新建图层calc_btn,从库中拖入21次按扭Digit_btn,然后按计算器的布局排好,分别在每个按扭上加个文本1、2、3、4、5、6、7、8、9、0、+、—、×、/、%、M+、MR、MC。
5、新建图层calc_txt,放入3个动态文本框,分别对应3个变量mem,display,symbol,其中,变量mem是用来显示中间计算结果存储情况的,display是用来显示输入数据和计算结果的,symbol是用来显示运算符的。
5、新建图层ActionScript,在第一关键桢加as:
// Calculator design and made by jinjun
fscommand("showmenu", false);//关闭右键菜单
fscommand("allowscale", false);//播放不进行缩放
fscommand("fullscreen", false);//不全屏幕显示
// ***Initializing the variables
display = "0";//显示输入和计算结果的变量
stop();// 取得操作数并显示的函数
function getdigit(digit) {
if (clear) {
clear = false;
decimal = false;
display = "0";
}
if (length(display)<13) {
if (display == "0" and digit != ".") {
display = digit;
} else {
display = display+digit;
}
}
}// 取得运算符并运算显示运算结果的函数
function getoperator(sign) {
if (operator == "+") {
display = Number(operand)+Number(display);
symbol = operator;
}
if (operator == "*") {
symbol = "x";
display = operand*display;
}
if (operator == "-") {
symbol = operator;
display = operand-display;
}
if (operator == "/") {
symbol = operator;
display = operand/display;
}
operator = "=";
clear = true;
symbol = " ";
decimal = "false";
if (sign != null) {
operator = sign;
if (operator == "*") {
symbol = "x";
} else {
symbol = operator;
}
operand = display;
}
}
// Calculator design and made by jinjun
fscommand("showmenu", false);//关闭右键菜单
fscommand("allowscale", false);//播放不进行缩放
fscommand("fullscreen", false);//不全屏幕显示
// ***Initializing the variables
display = "0";//显示输入和计算结果的变量
stop();// 取得操作数并显示的函数
function getdigit(digit) {
if (clear) {
clear = false;
decimal = false;
display = "0";
}
if (length(display)<13) {
if (display == "0" and digit != ".") {
display = digit;
} else {
display = display+digit;
}
}
}// 取得运算符并运算显示运算结果的函数
function getoperator(sign) {
if (operator == "+") {
display = Number(operand)+Number(display);
symbol = operator;
}
if (operator == "*") {
symbol = "x";
display = operand*display;
}
if (operator == "-") {
symbol = operator;
display = operand-display;
}
if (operator == "/") {
symbol = operator;
display = operand/display;
}
operator = "=";
clear = true;
symbol = " ";
decimal = "false";
if (sign != null) {
operator = sign;
if (operator == "*") {
symbol = "x";
} else {
symbol = operator;
}
operand = display;
}
}
// Calculator design and made by jinjun
fscommand("showmenu", false);//关闭右键菜单
fscommand("allowscale", false);//播放不进行缩放
fscommand("fullscreen", false);//不全屏幕显示
// ***Initializing the variables
display = "0";//显示输入和计算结果的变量
stop();// 取得操作数并显示的函数
function getdigit(digit) {
if (clear) {
clear = false;
decimal = false;
display = "0";
}
if (length(display)<13) {
if (display == "0" and digit != ".") {
display = digit;
} else {
display = display+digit;
}
}
}
// 取得运算符并运算显示运算结果的函数
function getoperator(sign) {
if (operator == "+") {
display = Number(operand)+Number(display);
symbol = operator;
}
if (operator == "*") {
symbol = "x";
display = operand*display;
}
if (operator == "-") {
symbol = operator;
display = operand-display;
}
if (operator == "/") {
symbol = operator;
display = operand/display;
}
operator = "=";
clear = true;
symbol = " ";
decimal = "false";
if (sign != null) {
operator = sign;
if (operator == "*") {
symbol = "x";
} else {
symbol = operator;
}
operand = display;
}
}
 
信息推荐
资讯中心 | 电子商务 | 搜索营销 | 设计学院 | 中医养生 | 养生保健 | 节日祝福 | 民俗文化 | 奇闻趣事
建站知识 | 人世百态 | 网站导航 | 传统节日 | 搜索热点 | 星座运势 | 趣闻轶事 | 祝福的话 | 短信大全
© 2023 QicaiSpace.Com