欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AS基礎(chǔ)教程整理第8/13頁

 更新時間:2007年03月22日 00:00:00   作者:  

第九章 數(shù)組(arrays)
在下一個新版本的多選題里,我們將使用什么AS的特性,來讓它更好呢?
那就是數(shù)組。
數(shù)組就是一系列的數(shù)據(jù)(MOOCK又開始上課了,chocobo的英文和計算機都不算好,為免誤人子弟,概念性的東西盡量精簡)
例如這樣兩個變量儲存的數(shù)據(jù):
fruit1 = "oranges";
fruit2 = "apples";  
它們是互相獨立的,使用起來很不方便,我們需要的是數(shù)組,以下是數(shù)組的定義方法,用“[]”框住,用“,”分隔開每個元素:
fruitList = ["oranges", "apples"]; 
現(xiàn)在兩個數(shù)據(jù)是放到同一個數(shù)組里面了,我們開始詳細解說數(shù)組
數(shù)組里面每一個數(shù)據(jù)稱為元素(element)。
而每一個元素都有個獨立數(shù)字代表所處的位置,數(shù)字叫索引(index),注意! 第一個數(shù)據(jù)的索引是0,第二個才是1
要按索引來提出數(shù)據(jù),我們要用一個運算符[],例如使用fruitList第一個元素賦值給a:
a=fruitList[0];
又例如將a的值賦給fruitList第一個元素:
fruitList[0]=a;

當然[]里面也可以放表達式、變量:
var index = 3;
// Set numApples to 2
var a = fruitList[index];

下面是個使用表達式的例子:
// Create a myFrames array. Note the legal formatting. 建立一個記錄LABEL的數(shù)組
var myFrames = ["storyEnding1",
             "storyEnding2",
             "storyEnding3",
             "storyEnding4"];

// Set randomFrame to a randomly picked element of myFrames
// by calculating a random number between 0 and 3
// 隨機從數(shù)組中提取一個LABEL
var randomFrame = myFrames[Math.floor(Math.random() * 4)];

// Now go to the random frame
// 然后跳到該LABEL播放
gotoAndStop(randomFrame); 

而數(shù)組包含數(shù)據(jù)的個數(shù)稱為長度(length),例如fruitList.length 就等于2

對數(shù)組最常用的處理就是從數(shù)組中選出有用的數(shù)據(jù)了,來看一個運用循環(huán)的例子:
// Create an array 建立數(shù)組,里面放了一些歌的類型
var soundtracks = ["electronic", 
                "hip hop",
                "pop",
                "alternative",
                "classical"];

// Check each element to see if it contains "hip hop"
// 一個循環(huán),檢查每一個元素是否等于"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; // 跳出循環(huán),找到了就不用再找了
  }


關(guān)于數(shù)組的方法(method)
方法就是從屬于某一對象(object)的函數(shù),通常都是對該對象進行處理的函數(shù)
好象太抽象了?我們還沒講到什么是對象,其實數(shù)組是對象的一種,我們就暫且將數(shù)組的方法理解為一個專門處理數(shù)組內(nèi)數(shù)據(jù)的結(jié)構(gòu)和內(nèi)容的工具吧
例如一個叫push()的方法就是一個工具,用于為數(shù)組添加一個元素,并且加在該數(shù)組的最后
使用起來并不復(fù)雜,看例子就知:
// Create an array with 2 elements
var menuItems = ["home", "quit"];

// Add an element 加一個元素
// menuItems becomes ["home", "quit", "products"]
// 現(xiàn)在數(shù)組的結(jié)構(gòu)變成["home", "quit", "products"]
menuItems.push("products"); 

// Add two more elements 這次是加兩個
// menuItems becomes ["home", "quit", "products", "services", "contact"]
menuItems.push("services", "contact"); 

跟push()相反從最后彈出一個元素的方法是pop()
而跟push()類似,但是是將一個元素加到數(shù)組的開頭的方法是unshift(),與之相反的是shift()
方法sort和reverse,用于重新排列數(shù)組的元素
方法splice用于從數(shù)組中間刪除某元素
方法slice和concat可以在某些數(shù)組的基礎(chǔ)上生成另一個新的數(shù)組
方法toString和join可以將整個數(shù)組變成單一個字符串

以上方法都可以從AS字典里面查到

相關(guān)文章

最新評論