Copy link to clipboard
Copied
hi how are you guys ? i hope you all fine ..
i have little problem with the arrays contents ..
the problem is , when i try to trace the array items it shows the arrays items as objects like this
[object Object],[object Object],[object Object]
here is the source code (i lighted the trace message and the arrays )
package {
import flash.display.MovieClip;
import flash.events.MouseEvent
import flash.events.Event;
public class MAIN extends MovieClip {
var additemsvar:String
var addpricevar:int
var removeitemvar:int
var itemsnp:Array = new Array (1000000)
var za:int = 0
public function MAIN () {
addbutton.addEventListener(MouseEvent.CLICK ,addbutton1)
removebutton.addEventListener(MouseEvent.CLICK ,removebutton1)
addEventListener(Event.ENTER_FRAME , onEnterFrame)
}
public function onEnterFrame (event:Event){
if(listbox1.selectedIndex != -1){
itemname11.text = String({label:listbox1.getItemAt(listbox1.selectedIndex)})
}
}
public function addbutton1 (event:MouseEvent){
additemsvar = additems.text
addpricevar = int (addprice.text)
listbox1.addItem ({label:itemsnp[za]})
listbox2.addItem ({label:itemsnp[za]})
itemsnp.push({additemsvar:addpricevar});
za += 1
trace (itemsnp.toString())
}
public function removebutton1 (event:MouseEvent){
trace (listbox1.selectedIndex)
listbox1.removeItemAt(listbox1.selectedIndex)
}
}
}
thank you
var additemsvar:String = "whatever";
1: itemsnp.push({additemsvar:addpricevar});
itemsnp elements are objects, not primitives like strings or numbers. trace(itemsnp) is expected to show [object Object] itemsnp.length number of times.
2. itemsnp.push(additemsvar);
itemsnp elements are strings. trace(itemsnp) is expected to show each string element (eg, "whatever").
Copy link to clipboard
Copied
call traceItemsnpF to trace that:
function traceItemsnpF(a:Array):void{
for(var i:int=0;i<a.length;i++){
for(var s:string in a){
trace(s,a);
}
}
}
Copy link to clipboard
Copied
???
Copy link to clipboard
Copied
Looks like you don't have a problem with Arrays, but with objects!
The code kglad gave you should be used to trace your content so instead of
trace(itemsnp)
use
traceItemsnpF(itemsnp)
Copy link to clipboard
Copied
ohh , but i realy dont want to fix the problem before knowing why is that happen...
you know, if i change the red lines to
itemsnp.push(additemsvar);
trace (itemsnp.toString())
i will get the array items , but only one array not multi array like
itemsnp.push({additemsvar:addpricevar});
and iam sure the problem from these 2 bracers {} ,, and i realy dont know why
so iam asking
1- why these 2 bracers because i have book in as2 and the book doesn,t use them.
2- why the message displays the objects instead the items .
sorry my english is bad but i hope you can understand me
thank you for your replay
Copy link to clipboard
Copied
your array (itemsnp) contains objects (or associative arrays). when you use trace(itemsnp), flash will trace the arrays elements. each element is an object (=associative array) and flash is tracing that accurately.
if you want to trace the objects contents, use the code i showed.
Copy link to clipboard
Copied
ohh , but it works with 1 array like this
itemsnp.push(additemsvar);
trace (itemsnp.toString())
why is that? i mean that i can see the items in this way not the objects
thank you for your replay
Copy link to clipboard
Copied
because additemsvar is a string, not an object.
Copy link to clipboard
Copied
but
itemsnp.push({additemsvar:addpricevar});
additemsvar and addpricevar are also strings variables
sorry because i asked you a lot but realy this thing confuse me
thank you
Copy link to clipboard
Copied
var additemsvar:String = "whatever";
1: itemsnp.push({additemsvar:addpricevar});
itemsnp elements are objects, not primitives like strings or numbers. trace(itemsnp) is expected to show [object Object] itemsnp.length number of times.
2. itemsnp.push(additemsvar);
itemsnp elements are strings. trace(itemsnp) is expected to show each string element (eg, "whatever").
Copy link to clipboard
Copied
ohh ok
thank you mr.kglad
sorry because i lated in the replay
Copy link to clipboard
Copied
ohh sorry
look this the whole code
package {
import flash.display.MovieClip;
import flash.events.MouseEvent
import flash.events.Event;
public class MAIN extends MovieClip {
var additemsvar:String
var addpricevar:int
var removeitemvar:int
var itemsnp:Array = new Array (1000000)
var za:int = 0
public function MAIN () {
addbutton.addEventListener(MouseEvent.CLICK ,addbutton1)
removebutton.addEventListener(MouseEvent.CLICK ,removebutton1)
addEventListener(Event.ENTER_FRAME , onEnterFrame)
}
public function onEnterFrame (event:Event){
if(listbox1.selectedIndex != -1){
itemname11.text = String({label:listbox1.getItemAt(listbox1.selectedIndex)})
}
traceItemsnpF(itemsnp)
}
public function addbutton1 (event:MouseEvent){
additemsvar = additems.text
addpricevar = int (addprice.text)
listbox1.addItem ({label:itemsnp[za]})
listbox2.addItem ({label:itemsnp[za]})
itemsnp.push({additemsvar:addpricevar});
za += 1
}
public function removebutton1 (event:MouseEvent){
trace (listbox1.selectedIndex)
listbox1.removeItemAt(listbox1.selectedIndex)
}
function traceItemsnpF(a:Array):void{
for(var i:int=0;i<a.length;i++){
for(var s:String in a){
trace(s,a);
}
}
}
}
}
and the result
additemsvar 0
additemsvar 0
additemsvar 0
sorry but maybe i set the function in wrong place
Copy link to clipboard
Copied
check your additemsvar and addpricevar values if the trace output is unexpected.
Copy link to clipboard
Copied
still .. thank you anyway
i will try to do that in another way
thank you again for your help
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now