Whats the difference between new Array, [] and new Object?

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

User avatar
mistermind
Posts: 131
Joined: 15 Sep 2007, 01:33
Contact:

Whats the difference between new Array, [] and new Object?

Postby mistermind » 10 Jun 2009, 03:28

Hey there folks
I'm trying to convert my game to AS3 for a while now and haven't had any problems with SFS so far (basically because everything is so much clear on AS3). Well til now :)

When I got to the buddy variables I kept having this error during runtime:

Code: Select all

TypeError: Error #1034: Type Coercion failed: cannot convert Object@363dfb01 to Array
at ederon_tw_fla::MainTimeline/onJoinRoom()
   at flash.events::
.........


After using this which is very similar to what described on the online docs for AS3 (Which was also pretty similar to what I had on AS2):

Code: Select all

var bVars:Object = new Object()
bVars["chatRoom"] = "1"
bVars["inGame"] = "0"
smartFox.setBuddyVariables(bVars)


Then after some search I found this on the advancedBuddyList example:

Code: Select all

var vars:Array = []
vars[tf_varKey.text] = tf_varValue.text
sfs.setBuddyVariables( vars )


which converted to my needs looked like this:

Code: Select all

var sendBObj:Array = [];
sendBObj["chatRoom"] = "1";
sendBObj["inGame"] = "0";
sfs.setBuddyVariables(sendBObj);


Both advancedBuddyList and my game now works without triggering a SFS runtime error.
After that I kinda felt a bit lost on all my learning progress lol. What exactly is the difference between them, and following the same line of thought, whats the difference between
var myArr:Array = new Array()
and
var myArr:Array = []

And why does SFS only understands the second one?
SELECT * FROM users WHERE clue > 0
0 rows returned.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 11 Jun 2009, 13:18

There is an ambiguity between associative arrays and objects in Actionscript.
Arrays can take String keys as well as Number keys.
Objects also work like that, with string keys.

Until AS2 you could swap them around in AS3 this is not possible anymore because of strict type checking.

Initializing an array with new Array() or [] is the same thing.
The 2nd is a useful literal expression that allow to create a new array and put values in it one single line.
Example:

Code: Select all

var arr:Array = [1,2,3]
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Carl Lydon
Posts: 298
Joined: 12 Nov 2007, 16:15
Location: NYC
Contact:

Need to fix docs?

Postby Carl Lydon » 02 Oct 2010, 05:29

Maybe you ned to fix the documentation?

When I use the exact code for setBuddyVariables here:

http://www.smartfoxserver.com/docs/docP ... index.html

which is:


Code: Select all

var bVars:Object = new Object()
bVars["status"] = "away"
bVars["track"] = "One Of These Days"
bVars["$rank"] = "guru"

smartFox.setBuddyVariables(bVars)


I get the same "Type Coercion failed" error noted above.

When I change the first line to an array, it works. The docs tell us to use an object when the method wants and Array? Seems like docs are wrong?
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Postby Lapo » 02 Oct 2010, 05:49

Yes, what you say is correct.
We'll update the docs. Thank you
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 81 guests