8/3/2010 3:11:38 AM
Title:
Load image base on the paths in multiple XML files error
I wan to write AS3 program that load image base on the paths in multi XML files (user choose which xml file they want by button), the first time it worked properly but in 2nd, 3rd... time, it have an error:
TypeError: Error #1010: A term is undefined and has no properties.
at Main/processXML()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
Here is my Main class:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.navigateToURL;
import flash.external.ExternalInterface;
/**
* ...
* @author Tuan
*/
public class Main extends MovieClip
{
//Declare member variables here
var urlLoader:URLLoader;
var xml:XML;
var xmlPath:URLRequest;
var imgPath:URLRequest;
var x_counter:Number = 0;
var y_Counter:Number = 0;
var initX:Number = 180;
var initY:Number = 250;
var thumbnail_Width:Number = 200;
var thumbnail_Height:Number = 150;
var spaceW:Number = 10;
var spaceH:Number = 12;
var offset:int = 0;
var swfLoader:Loader;
var totalGame:int = 0;
var currentPage:int = 0;
var totalPage:int;
//Constructor here
public function Main()
{
//Load xml file
loadXML();
btnSearch.addEventListener(MouseEvent.CLICK, searchClick);
next.buttonMode = true;
prev.buttonMode = true;
btnSearch.buttonMode = true;
//add event listener for next and previous button
next.addEventListener(MouseEvent.CLICK, nextClick);
prev.addEventListener(MouseEvent.CLICK, prevClick);
}
//Load xml file
public function loadXML()
{
switch(currentPage)
{
case 1:
xmlPath = new URLRequest("Demo1.xml");
break;
case 2:
xmlPath = new URLRequest("Demo2.xml");
break;
case 3:
xmlPath = new URLRequest("Demo3.xml");
break;
default:
xmlPath = new URLRequest("Demo1.xml");
break;
}
urlLoader = new URLLoader();
urlLoader.load(xmlPath);
urlLoader.addEventListener(Event.COMPLETE, processXML);
}
//Process loaded xml file
public function processXML(e:Event):void
{
xml = XML(urlLoader.data);
currentPage = xml.@page;
totalGame = xml.@total;
totalPage = Math.ceil(totalGame / 9);
//paging file paths
paging();
if (xml != null)
{
loadThumb(xml.game[offset].@thumb);
trace("xml not null " + xml.game[0].@id);
}
else
{
trace("xml is null");
}
}
//Load thumbnails here
public function loadThumb(path:String)
{
var loader:Loader = new Loader();
//loader.name = path;
imgPath = new URLRequest(path);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
loader.load(imgPath);
}
//Process loaded thumbnails here
public function thumbLoaded(e:Event):void
{
trace("==========xml=========" + xml.game[0].@id);
var my_thumb:Loader = Loader(e.target.loader);
var container:mcContainer = new mcContainer();
addChild(container);
container.addChild(my_thumb);
container.x = (thumbnail_Width + spaceW) * x_counter + initX;
container.y = (thumbnail_Height +spaceH) * y_Counter + initY;
container.buttonMode = true;
container.addEventListener(MouseEvent.CLICK, containerClick);
if (x_counter + 1 < 3)
{
x_counter++;
}
else
{
x_counter = 0;
y_Counter++;
}
container.name = "container" + offset;
offset++;
trace(xml.game.length());
container.txtID.text = xml.game[offset - 1].@id;
if (offset < xml.game.length())
{
loadThumb(xml.game[offset].@thumb);
}
}
//The event occured when user click the search button
public function searchClick(e:MouseEvent):void
{
//push query to server
}
//Container click event
public function containerClick(e:Event):void
{
var myContainer:mcContainer = e.currentTarget as mcContainer;
navigateToURL(new URLRequest(myContainer.txtID.text), "_self");
}
//paging
public function paging():void
{
if (xml != null)
{
txtPaging.text = "Trang " + currentPage + "/" + totalPage;
if (currentPage == 1)
{
prev.visible = false;
}
else if(currentPage > 1 && currentPage < totalPage)
{
prev.visible = true;
next.visible = true;
}
else
{
next.visible = false;
}
//trace(xml);
}
}
//next button click
public function nextClick(e:Event):void
{
if (currentPage < totalPage)
{
currentPage++;
removeContainer();
loadXML();
}
trace("=======currentPage=======" + currentPage);
}
//previous button click
public function prevClick(e:Event):void
{
if (currentPage > 1)
{
currentPage--;
removeContainer();
loadXML();
}
}
public function removeContainer():void
{
for (var i:int = 0; i < xml.game.length(); i++)
{
var myContainer:mcContainer = this.getChildByName("container" + i) as mcContainer;
if (myContainer != null)
{
myContainer.removeEventListener(MouseEvent.CLICK, containerClick);
removeChild(myContainer);
myContainer = null;
}
}
}
}
}
8/3/2010 3:13:41 AM
My XML file's format as follow:
<?xml version="1.0" encoding="utf-8" ?>
<gameList total="26" page="1">
<game id="45" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000027/icon/tho-ngoc-ghep-hinh2.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000027/flash/Tho ngoc ghep hinh.swf"
/>
<game id="35" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000017/icon/02.-sieu-nhan-bong-bong.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000017/flash/Sieu nhan bong bong.swf"
/>
<game id="33" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000015/icon/01.-123-oan-tu-ti.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000015/flash/01. 123 - oan tu ti.swf"
/>
<game id="36" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000018/icon/03.-nha-tao-mau-nhi.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000018/flash/03. Nha tao mau nhi.swf"
/>
<game id="37" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000019/icon/04.-nhay-day-cung-pucca.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000019/flash/04. Nhay day cung Pucca.swf"
/>
<game id="39" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000021/icon/06.-bau-troi-cua-bong.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000021/flash/06. bau troi cua Bong.swf"
/>
<game id="38" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000020/icon/05.-nguoi-hung-tu-ti.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000020/flash/05. Nguoi hung` Tuti.swf"
/>
<game id="40" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000022/icon/07.-truy-tim-kho-bau.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000022/flash/07. Truy tim kho bau.swf"
/>
<game id="44" thumb="http://www.socnhi.com/socnhiAdmin/images.game/GM000026/icon/08.-toa-lau-dai-bi-an.jpg"
scr="http://www.socnhi.com/socnhiAdmin/images.game/GM000026/flash/Toa lau dai bi an.swf"
/>
</gameList>
Davis
Points: 780
Posts:0
8/3/2010 9:52:46 AM
it seems you have removed some thing which is required later also , so its throwing undefined error. To get more precise error details , use the debug version of flash player. That will show you the exact line number where error is occurring.