jarmanje
10-22-2007, 07:20 PM
Hello, I am quite new to as3 and I really cannot figure out how to do this.
This script i made loads all of my images really nicely into my scrollpane
Then when i click on the image it returns:
undefined
[object Loader]
For every image, it has a unique materialID (from my database), so i want to make a textbox this value when i click on an image.
But the eventhandler does not seem to know which image it has clicked! I really don't understand why not, I thought it would store the current value of evt.target.data["Material"+i] when clicked
But it just returns [object Loader], instead of the value of evt.target.data["Material"+i] ("12345", for example)
Is it possible to pass a unique value for each image i load to the clickhandler function? so when i click on an image, the textbox on the scence changes to the name of that image..
Please give any advise..
var myLoader:URLLoader = new URLLoader()
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES
myLoader.load(new URLRequest("http://www.mydomain.com/materials.php"))
myLoader.addEventListener(Event.COMPLETE, onDataLoad)
myLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
myLoader.addEventListener(SecurityErrorEvent.SECUR ITY_ERROR, onSecurityError)
myLoader.addEventListener(HTTPStatusEvent.HTTP_STA TUS, onHTTPStatus)
var holder:Sprite = new Sprite();
var p:Array = new Array();
var hor = -100
var ver = 10
var rows = 0
var howmanyloaded = 0
var lasttoload = 0
function onDataLoad(evt:Event){
var thestage = 0.2
var count = evt.target.data.count
lasttoload = count - 2
trace(lasttoload)
var imagetoload = Math.round(lasttoload * thestage)
for(var i:uint=0; i<evt.target.data.count; i++){
p[i] = new Loader();
p[i].load(new URLRequest("http://www.mydomain.com/products/pics/"+evt.target.data["Material"+i]+".jpg"));
//trace(evt.target.data["Material"+i])
//preloading stuff, will work on fixing later today
//p[i].contentLoaderInfo.addEventListener(ProgressEvent. PROGRESS, loadProgress);
//p[i].contentLoaderInfo.addEventListener(Event.COMPLETE , loadComplete);
holder.addChild(p[i]);
p[i].addEventListener(MouseEvent.CLICK, clickHandler)
//my genius way to avoid having to do the maths. haha
if(rows==4){
rows = 0
hor = -100
ver = ver + 110
} else {
hor = hor + 110
rows++
//ver = ver + 0
}
p[i].x = hor
p[i].y = ver
function clickHandler(event:MouseEvent):void {
trace(evt.target.data["Material"+i]);
trace(event.target);
}
}
}
//error callbacks
function onIOError(evt:IOErrorEvent){
trace("IOError: "+evt.text)
}
function onHTTPStatus(evt:HTTPStatusEvent){
//trace("HTTPStatus: "+evt.status)
}
function onSecurityError(evt:SecurityErrorEvent){
trace("SecurityError: "+evt.text)
}
////i have just temporarily added a timer here, because the scrollpane does not scroll unless i add the mc when it's already filled
//will have to tie this into the preloading, attach mc to pane when it is done preloading.
var timer:Timer = new Timer(7000, 1);
timer.addEventListener(
TimerEvent.TIMER,
function(evt:TimerEvent):void {
pane.source = holder;
trace("attached")
}
);
timer.start();
This script i made loads all of my images really nicely into my scrollpane
Then when i click on the image it returns:
undefined
[object Loader]
For every image, it has a unique materialID (from my database), so i want to make a textbox this value when i click on an image.
But the eventhandler does not seem to know which image it has clicked! I really don't understand why not, I thought it would store the current value of evt.target.data["Material"+i] when clicked
But it just returns [object Loader], instead of the value of evt.target.data["Material"+i] ("12345", for example)
Is it possible to pass a unique value for each image i load to the clickhandler function? so when i click on an image, the textbox on the scence changes to the name of that image..
Please give any advise..
var myLoader:URLLoader = new URLLoader()
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES
myLoader.load(new URLRequest("http://www.mydomain.com/materials.php"))
myLoader.addEventListener(Event.COMPLETE, onDataLoad)
myLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError)
myLoader.addEventListener(SecurityErrorEvent.SECUR ITY_ERROR, onSecurityError)
myLoader.addEventListener(HTTPStatusEvent.HTTP_STA TUS, onHTTPStatus)
var holder:Sprite = new Sprite();
var p:Array = new Array();
var hor = -100
var ver = 10
var rows = 0
var howmanyloaded = 0
var lasttoload = 0
function onDataLoad(evt:Event){
var thestage = 0.2
var count = evt.target.data.count
lasttoload = count - 2
trace(lasttoload)
var imagetoload = Math.round(lasttoload * thestage)
for(var i:uint=0; i<evt.target.data.count; i++){
p[i] = new Loader();
p[i].load(new URLRequest("http://www.mydomain.com/products/pics/"+evt.target.data["Material"+i]+".jpg"));
//trace(evt.target.data["Material"+i])
//preloading stuff, will work on fixing later today
//p[i].contentLoaderInfo.addEventListener(ProgressEvent. PROGRESS, loadProgress);
//p[i].contentLoaderInfo.addEventListener(Event.COMPLETE , loadComplete);
holder.addChild(p[i]);
p[i].addEventListener(MouseEvent.CLICK, clickHandler)
//my genius way to avoid having to do the maths. haha
if(rows==4){
rows = 0
hor = -100
ver = ver + 110
} else {
hor = hor + 110
rows++
//ver = ver + 0
}
p[i].x = hor
p[i].y = ver
function clickHandler(event:MouseEvent):void {
trace(evt.target.data["Material"+i]);
trace(event.target);
}
}
}
//error callbacks
function onIOError(evt:IOErrorEvent){
trace("IOError: "+evt.text)
}
function onHTTPStatus(evt:HTTPStatusEvent){
//trace("HTTPStatus: "+evt.status)
}
function onSecurityError(evt:SecurityErrorEvent){
trace("SecurityError: "+evt.text)
}
////i have just temporarily added a timer here, because the scrollpane does not scroll unless i add the mc when it's already filled
//will have to tie this into the preloading, attach mc to pane when it is done preloading.
var timer:Timer = new Timer(7000, 1);
timer.addEventListener(
TimerEvent.TIMER,
function(evt:TimerEvent):void {
pane.source = holder;
trace("attached")
}
);
timer.start();