我有一个对象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,
编辑:
in Infos: trace(typeof(item.movieimage))
和trace(typeof(ary))
是:
typeof(item.movieimage)=string
typeof(ary)=object
编辑2:
以下是项目的屏幕捕获。
项目的屏幕帽。
编辑3
这是我的代码,以了解" item.MovieImage"如何工作
//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文件" searchmovie4.php"就是这样:
$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)
,它将在我的数据库的行电影中显示所有项目。
如果我在AS3代码中执行trace(item.title)
,它将在数据库的行标题中显示所有项目。
我想在我的AS3代码中可以做到的trace(item.movieimage[2])
,以便向我展示" MovoryImage"中的第三个项目。
好吧,您已提供给item
对象的movieimage
属性的 text 与您所拥有的 dem> dem> dem> dem> dem> delimiter 提供给split()
方法;在您的情况下,逗号的角色!
我怀疑您拥有的定界符是" space" 字符或一个新字符,例如 carragiare retrun retroun 。
的字符在以下代码片段中,我使用" 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
正如@someone所说的,输出显示列表项目各个在新行上,因此您需要针对newlines进行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]+/);
还要注意,如果您对服务器如何返回这些值有任何控制分裂。