• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

problem with arrays

Participant ,
Jan 18, 2013 Jan 18, 2013

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

TOPICS
ActionScript

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 18, 2013 Jan 18, 2013

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").

Votes

Translate

Translate
Community Expert ,
Jan 18, 2013 Jan 18, 2013

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);

}

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 18, 2013 Jan 18, 2013

Copy link to clipboard

Copied

???

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 18, 2013 Jan 18, 2013

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 18, 2013 Jan 18, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2013 Jan 18, 2013

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 18, 2013 Jan 18, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2013 Jan 18, 2013

Copy link to clipboard

Copied

because additemsvar is a string, not an object.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 18, 2013 Jan 18, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 18, 2013 Jan 18, 2013

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").

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2013 Jan 19, 2013

Copy link to clipboard

Copied

ohh ok

thank you mr.kglad

sorry because i lated in the replay

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 19, 2013 Jan 19, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 19, 2013 Jan 19, 2013

Copy link to clipboard

Copied

check your additemsvar and addpricevar values if the trace output is unexpected.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 20, 2013 Jan 20, 2013

Copy link to clipboard

Copied

still .. thank you anyway

i will try to do that in another way

thank you again for your help

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2013 Jan 20, 2013

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines