访问数组中的项目 - AS3

时间:2017-02-22 04:16:45

标签: arrays actionscript-3

我有一个对象item.movieimage,其中包含从我的数据库中检索的一些文本(item:Object)。文本每周都会自动更改。

如果我trace(item.movieimage)output就是这样:

text1 
text2
text3 
text4

当代码从我的数据库中获取时,单词会发生变化。数据库每周都会更改单词。

现在,我希望我的AS3代码显示item.movieimage的第三个元素。

我试图这样做:

var my_str:String = item.movieimage+",";
var ary:Array = my_str.split(",");
    trace(ary[2]);

但它没有用。输出为" undefined"。

您知道如何访问我创建的阵列中的特定项目吗?或者我如何访问item.movieimage的第三项?

如果我trace(ary);,则输出为:

text1, 
text2,
text3,
text4,

编辑:

对于信息: trace(typeof(item.movieimage))trace(typeof(ary))是:

typeof(item.movieimage)=string
typeof(ary)=object

编辑2:

这里是item.movi​​eimage的屏幕截图

Screen Cap of item.movieimage

编辑3

这是我的代码,以了解" item.movi​​eimage"是如何工作的

 //Variables for downloading content from my database to my AS3 code

var urlReqSearchAll: URLRequest = new URLRequest("http://www.myWebSite/searchMovie4.php");
    var loader5:URLLoader = new URLLoader();


//downloading content 

        function searchAll():void { 
            if (contains(list)){
                list.removeChildren();  
            }
            urlReqSearchAll.method = URLRequestMethod.POST;  
                loader5.load(urlReqSearchAll);
                loader5.addEventListener(Event.COMPLETE,complete);

            var variables:URLVariables = new URLVariables();    
}

//Content Downloaded. 

        function complete(e:Event):void {

    addChild(list);
    products = JSON.parse(loader5.data) as Array;
hourSaved.data.saved=loader5.data;
     products.reverse();
    for(var i:int = 0; i < products.length; i++){
        createListItem(i, products[i]);
}
    displayPage(0);
    showList();
}


// If too much items --> creates multiple page

const itemsPerPage:uint = 7;
var currentPageIndex:int = 0;

function displayPage(pageIndex:int):void {
    list.removeChildren();
    currentPageIndex = pageIndex;
    var firstItemIndex:int = pageIndex * itemsPerPage;
    var j:int = 0;
    var lastItemIndex: int = firstItemIndex + 7; // as lastItemIndex should be 10 more
 if (lastItemIndex > products.length) // if lastindex is greater than products length
 lastItemIndex = products.length;
 for(var i:int = firstItemIndex; i< lastItemIndex; i++){
 createListItem( j, products[i]); // j control the position and i points to particular element of array..
 j++;
 }
    next.visible = lastItemIndex < products.length - 1;
        if(currentPageIndex==0){
        previous.visible=false;
    }
}


// Display the information downloded from my database

function createListItem(index:int, item:Object):void {

var listItem:TextField = new TextField();
    var myFormat:TextFormat = new TextFormat();
    myFormat.size = item.title.length > 13 ? 22 : 26
    listItem.multiline = true;
    listItem.wordWrap = true;
    myFormat.align = TextFormatAlign.CENTER;
    myFormat.color = 0xA2947C;
    myFormat.font = "Ebrima";
    myFormat.bold = true;
    listItem.defaultTextFormat = myFormat;  
    listItem.x = 135;
    listItem.y = 123+ index * 84;
    listItem.width = 200;
    listItem.height = 80;
 listItem.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void {
        showDetails(item);
    });
    list.addChild(listItem);
    str = item.title;


}

我的php文件&#34; SearchMovie4.php&#34;是这样的:

$products = array();


while ($row = mysql_fetch_array($sql_result)) {
    $products[] = array(
"title" => $row["theTitle"],
"movieimage" => $row["movieimage"],
"movielength" => $row["movielength"],
"story" => $row["story"],
    );
} 
echo json_encode($products);

因此,如果我在AS3代码中执行trace(item.movieimage),它将显示我的数据库的行movieimage中的所有项目。

如果我在AS3代码中执行trace(item.title),它将显示我数据库的行标题中的所有项目。

我希望能够在我的AS3代码trace(item.movieimage[2])中为我显示行中的第三项&#34; movieimage&#34;。

2 个答案:

答案 0 :(得分:0)

您提供给movieimage对象的item属性的文本 delimiter 不一样正如你提供给split()方法的那样;在你的情况下逗号的字符!

<小时/> 我怀疑你有分隔符,是“space”字符或new-line字符,如 carriage retrun 的字符。
在以下代码段中,我使用“space”作为分隔符:

var item:Object = new Object();
item.movieimage = "text1 text2 text3 text4";
var my_str:String = item.movieimage;
var ary:Array = my_str.split(" ");
trace(ary[2]); // text3

答案 1 :(得分:0)

正如@someOne所说,输出显示列表项每个都在一个新行上,因此您需要split()对换行。其中一个应该有效:

// if server uses "\n" newline char
var movies:Array = item.movieimage.split("\n");

// if server uses "\r" carriage return char
var movies:Array = item.movieimage.split("\r");

// if you aren't sure or there's a mixture of newline chars
var movies:Array = item.movieimage.split(/[\r|\n]+/); 

另请注意,如果您对服务器如何返回这些值有任何控制权,那么您应该只返回一个JSON数组(因为我在您的屏幕截图中看到您已经在解码JSON),那么您不必再做任何字符串分裂。

相关问题