语录提交--登陆--注册--论坛交流--站长博客

Flash编程无基础入门教程

[作者:来自网络][日期:2007-12-27][导航:Flash教程 >> ActionScript2 >> Flash编程无基础入门教程]
   数组就是一系列的数据(MOOCK又开始上课了,chocobo的英文和计算机都不算好,为免误人子弟,概念性的东西尽量精简)
例如这样两个变量储存的数据:
fruit1 = "oranges";
fruit2 = "apples";
它们是互相独立的,使用起来很不方便,我们需要的是数组,以下是数组的定义方法,用“&#;”框住,用“,”分隔开每个元素:
fruitList = ["oranges", "apples"];
现在两个数据是放到同一个数组里面了,我们开始详细解说数组
数组里面每一个数据称为元素(element)。
而每一个元素都有个独立数字代表所处的位置,数字叫索引(index),注意! 第一个数据的索引是0,第二个才是1
要按索引来提出数据,我们要用一个运算符&#;,例如使用fruitList第一个元素赋值给a:
a=fruitList�;
又例如将a的值赋给fruitList第一个元素:
fruitList�=a;
当然&#;里面也可以放表达式、变量:
var index = 3;
// Set numApples to 2
var a = fruitList[index];
下面是个使用表达式的例子:
// Create a myFrames array. Note the legal formatting. 建立一个记录LABEL的数组
var myFrames = ["storyEnding1",
"storyEnding2",
"storyEnding3",
"storyEnding4"];
// Set randomFrame to a randomly picked element of myFrames
// by calculating a random number between 0 and 3
// 随机从数组中提取一个LABEL
var randomFrame = myFrames[Math.floor(Math.random() * 4)];
// Now go to the random frame
// 然后跳到该LABEL播放
gotoAndStop(randomFrame);
而数组包含数据的个数称为长度(length),例如fruitList.length 就等于2
对数组最常用的处理就是从数组中选出有用的数据了,来看一个运用循环的例子:
// Create an array 建立数组,里面放了一些歌的类型
var soundtracks = ["electronic",
"hip hop",
"pop",
"alternative",
"classical"];
// Check each element to see if it contains "hip hop"
// 一个循环,检查每一个元素是否等于"hip hop"这个类型
// 另外,请留意此处MOOCK对FOR的写法,J=0之前有一个VAR,这好象可有可无,其实是一个好习惯!
for (var j = 0; j < soundtracks.length; j++) {
trace("now examining element: " + j);
if (soundtracks[j] == "hip hop") {
trace("the location of 'hip hop' is index: " + j);
break; // 跳出循环,找到了就不用再找了
}
}
关于数组的方法(method)
方法就是从属于某一对象(object)的函数,通常都是对该对象进行处理的函数

[文章热度:]


123456789101112131415161718192021

上一页:Flash 物理效果收藏

下一页:Flash As编码习惯

最新话题

网站导航

搜索

网站公告


Copyright 2007 51as.com. Some Rights Reserved.
鄂ICP备07003189号

Powered by: KingCMS 5.0.1.0217