Sie sind nicht angemeldet.

1

Donnerstag, 5. März 2009, 16:15

Array in AS3

I am at the end of my wisdom and would appreciate a quick help with the createarray() command:

Could not find anything in your forum..


- I successfully created an array and filled it in xml
- can read separate entries directly in as3 - krpano.get("plugin[thumbs:panos].thumb[3].url")
- but can't bring the accessible array into an swf plugin

here are the snippets:
xml:

<action name="load_pano_thumbnails">
set(plugin[thumbs:panos].thumb[2].url,1.jpg);
set(plugin[thumbs:panos].thumb[3].url,2.jpg);
set(plugin[thumbs:panos].thumb[4].url,3.jpg);
set(plugin[thumbs:panos].thumb[testy].url,4.jpg);
set(plugin[thumbs:panos].alpha,0);
trace(has loaded);
</action>

And the as3 part:
//note: array is initialized in as3:

plugin_object.createarray("thumb")

function updateEvent(dataevent:DataEvent):void

{
krpano.trace(0, krpano.get("plugin[thumbs:panos].thumb[3].url") ); //this works like a charm
try{
var temp:* = krpano.get("plugin[thumbs:panos].thumb" ); //this doesn't return the array, but an 'object Kinterface_array' that I cannot access the regular array way :-)

krpano.trace(0, temp[3].url ); //hence, this doesn't run
}catch(e:Error){
krpano.trace(0, e );
}
}
//end

Is there any documentation on how to use the object kinterface_array()? or another way to be able to treat the xml generated array as an array in as3?

Thanks for any response in advance.

2

Donnerstag, 5. März 2009, 17:33

Hi,

first some notes for array elements:
  • array elements can be accessed by their name or a numeric index
  • when the first character of the indexname is a number then this number is interpreted as index (0 based)
  • otherwise the element is accessed by the given name
  • try to avoid mixing of numeric and names as index when defining array items in the xml!

here a example - how to get the name of each array item with the krpano interface: (AS3 code / JS similar)

Quellcode

1
2
3
4
5
6
7
8
9
var count:int = int( krpano.get("plugin.count") );
var i:int;

for (i=0; i<count; i++)
{
   var itemname:String = krpano.get("plugin[" + i + "].name");
   // ...
   // all other attributes can be accessed by the same way
}


this is the standard way via the krpano interface, this works identically also in javascript!

but with AS3 - there is also a more advanced and faster access possible
an Array in krpano is of the internal type "Kinterface_array",
this Array can by accessed as "Object" in AS3,

how to use it:

first get the internal array object via the krpano interface:

Quellcode

1
var krpanoarray:Object = krpano.get("plugin");


this Kinterface_array object has the following attributes and interface functions:

attributes:
  • count
    • the number of elements in this array
    • could be changed by setting it, but warning - only reducing the size is possible (e.g. set to 0 to clear the array), increasing the count would have a undefined behavior!

functions:
  • createItem(name:String):*
    • creates a new array item with the given name
    • returns the new item object
  • getItem(name:String):*
    • returns the item object or null if not found
    • when name is a numeric value - the name is interpreted as index (0 ... count-1)
  • getItemIndex(index:int):*
    • direct index access (faster than getItem)
    • returns the item object or null if not found
  • getArray():Array
    • returns a AS3 Array object of all array items
    • note - this Array object can be used for reading/modify items, but the Array item shouldn't be changed!
  • renameItem(oldname,newname):void
    • changes the name of a array item
    • note - the name is a special attribute of a item, it can't be changed direct!
  • removeItem(name):*
    • removes the item from the array
    • and returns the removed item

here a example how to parse a array in AS3 in the fastest way: (AS3 code)

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
var plugins:Object = krpano.get("plugin");

var pluginsarray:Array = plugins.getArray();

var count:int = pluginsarray.length;
var i:int;

for (i=0; i<count; i++)
{
    var item:Object = pluginsarray[i];
    trace(item.name);
}



I hope I could clarify some of the krpano internals to help using it

best regards,
Klaus

3

Donnerstag, 5. März 2009, 19:19

first 2 array items empty

Hi Klaus,

Thanks for the info. That got me one big step further. I noticed though, that the array has 2 empty(?) entries [0] & [1], which are not created anywhere. I generate the array in AS3:

Quellcode

1
plugin_object.createarray("thumb");

fill it in xml:

Quellcode

1
2
3
4
set(plugin[thumbs:panos].thumb[0].url,1.jpg);
set(plugin[thumbs:panos].thumb[1].url,2.jpg);
set(plugin[thumbs:panos].thumb[2].url,3.jpg);
set(plugin[thumbs:panos].thumb[3].url,4.jpg);

and read it out in AS3:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function updateEvent(dataevent:DataEvent):void
{
	// get all current values from krpano
	thumb = plugin_object.thumb.getArray();
	
	var count:int = plugin_object.thumb.count;
	var i:int;
	
	for (i=0; i<count; i++)
	{
		try{
			var item:Object = thumb[i];
			krpano.trace(0, item.url);
		}catch(e:Error){
			krpano.trace(0, e );
		}
	}

}


this returns 6 entries??? as follows in the krpano trace:

DEBUG: TypeError: Error #1009
DEBUG: TypeError: Error #1009
DEBUG: 1.jpg
DEBUG: 2.jpg
DEBUG: 3.jpg
DEBUG: 4.jpg

Any idea where these mysterious entries come from? Any help is greatly appreciated!

Thanks,
P

4

Donnerstag, 5. März 2009, 19:39

that's really strange!
do you have a example online or can you post the full xml?

5

Donnerstag, 5. März 2009, 19:53

all sorted

You know what's even stranger!? I didn't change the xml file, neither the as3 fire... and it works. Might have been something in the browser cache or so. Thanks. I'll post more if it re-appears :-)

P

6

Donnerstag, 5. März 2009, 20:06

yes, that's strange
I have analyzed now the code again and again and didn't found now a situation where this could be happen...
but good that it is working now

Ähnliche Themen