Roomlist Array and Flash List Component

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

Sfulk
Posts: 54
Joined: 06 Jul 2012, 17:02

Roomlist Array and Flash List Component

Postby Sfulk » 06 Mar 2015, 04:58

Hello everyone, I've been stuck on this for a little while so I figured I'd post here for help. I have been trying to get the room list array from SmartFoxServer and then display the data in the List component that comes with Flash.

Code: Select all

import fl.controls.List;
import fl.data.DataProvider;
         
var PropertyList:Array = sfs.roomManager.getRoomList()
Map.pList.dataProvider = new DataProvider(PropertyList);
trace(PropertyList);

The array of rooms is working as its supposed to but I cannot use the array with the list component. No matter what I do the output in flash shows a reference error: ReferenceError: Error #1069: Property label not found on com.smartfoxserver.v2.entities.MMORoom and there is no default value. I've tried to change the labelField that the list component uses but it doesn't make a difference. When I used PropertyList.push("Test"); it added a comma and the word test after the entire array: [Room: The Map, Id: 0, GroupId: default]. So it's like its treating everything in the [] as one piece of info instead of 3 difference pieces in one chunk. Anyway I have no idea what to do I've tried searching the forums and on Google but I cant find anyone asking the same question. I would use the smartfoxbits roomlist component but I do not want to use the connector component. Any help would be greatly appreciated :D
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomlist Array and Flash List Component

Postby Lapo » 06 Mar 2015, 09:45

Hi,
what you are trying to do is incorrect. You should just consult the documentation for the List component for the specific details.

The array provided by the SFS RoomManager contains instances of the Room class, but the List component requires a different type of objects. If I recall it correctly it requires an array of objects with two properties: label and data.

You will have to cycle through the RoomList and build the data provider as required by the List component.

Alternatively you can use the SmartFox Bits ---> http://bits.smartfoxserver.com/download
We also use them extensively in our Flash Examples, so you can take a look at them to learn more: http://www.smartfoxserver.com/download/sfs2x#p=examples

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Sfulk
Posts: 54
Joined: 06 Jul 2012, 17:02

Re: Roomlist Array and Flash List Component

Postby Sfulk » 07 Mar 2015, 23:46

Hey Lapo,
Thanks for the help, I’ve looked through the documentation for the list component but it only shows how to build the data provider with static data. I don’t understand how to break apart the data in the array that getRoomList () makes. I’d like to get the original array and then modify it so that the room name is the label and the room id is the description and then use the new array as the data provider. I would gladly use the Room List Component from SmartFox Bits but I can’t use any components independently they all require the connector component.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomlist Array and Flash List Component

Postby Lapo » 09 Mar 2015, 14:41

The data provider for visual component is a usually an Array or an implementation of ICollectionView, such as ArrayCollection.
Both objects can be populated dynamically at runtime and passed to the dataProvider getter/setter of the UI Component.

I would strongly recommend taking a look at Adobe's documentation:
http://help.adobe.com/en_US/flex/using/ ... -7b6b.html

You can find both static and dynamic setting of the data provider.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
Sfulk
Posts: 54
Joined: 06 Jul 2012, 17:02

Re: Roomlist Array and Flash List Component

Postby Sfulk » 12 Mar 2015, 02:38

Hey Lapo,
I am using Flash not Flex, I've read adobes documentation for flash my problem isn't that I don't know how to set a DataProvider. The examples with dynamic data are not anything like this scenario. I need to know how to take the data from the room list array and use it to populate a DataProvier. Is this possible?
Thanks,
Scott
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomlist Array and Flash List Component

Postby Lapo » 12 Mar 2015, 08:38

The room list is obtained via SmartFox.roomList property, after you have logged in successfully.
What you get is an Array of Room objects.

From there you loop through each element in the Array and populate the dataprovider with the properties you need to show in your component. (e.g. room name)
The documentation for the Room class is available here:
http://docs2x.smartfoxserver.com/api-docs/asdoc/

What is not clear in this process?
Lapo

--

gotoAndPlay()

...addicted to flash games
Sfulk
Posts: 54
Joined: 06 Jul 2012, 17:02

Re: Roomlist Array and Flash List Component

Postby Sfulk » 14 Mar 2015, 21:10

I don’t understand how to loop through each element in the array to get the room name. Anything that I try isn’t working and results in errors. I’m not sure is this is simple and I’m just not getting it or more complex than I thought, but anyway thank you for the help you’ve given me so far I appreciate it.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Roomlist Array and Flash List Component

Postby Lapo » 16 Mar 2015, 11:34

Are you familiar with looping through an array in Actionscript?
Something like:

Code: Select all

for each (var room:Room in sfs.roomList)
{
   trace("Room name: " + room.name)
}
Lapo

--

gotoAndPlay()

...addicted to flash games
Sfulk
Posts: 54
Joined: 06 Jul 2012, 17:02

Re: Roomlist Array and Flash List Component

Postby Sfulk » 18 Mar 2015, 01:55

I wasn’t familiar with doing that, I managed to get that to display one of the room names in the list but when there are more than one room it just displays the same room name. I looked up looping through an array in AS3 more in the docs but it only shows the examples using trace. This is the code that I’m using right now:

Code: Select all

      import fl.controls.List;
         import fl.data.DataProvider;
         
      
         var PropertyList:Array = sfs.roomManager.getRoomList();
         
         for each (var room:Room in PropertyList)
         {
         trace("label: " + room.name)
            var PropertyNames:Array = [{label: room.name},{label: room.name},{label: room.name}];
         Map.pList.dataProvider = new DataProvider(PropertyNames);
         }


I know that I’m not populating the dataProvider the right way, I looked through the flex advanced chat example and I found where the room list is populated in it:

Code: Select all

   var dataProvider:ArrayCollection = getRoomListFromGroup(room.groupId).dataProvider as ArrayCollection;
   for each (var r:Room in dataProvider)
   {
      if (r.id == room.id)
      {
         // Update object in dataprovider
         dataProvider.setItemAt(room, dataProvider.getItemIndex(r));
         break;
      }
   }


It uses setItemAt but I couldn’t get it to work in flash I’m not sure if that is only for flex. If it is only for flex is there a similar way to do this in flash?

Thanks
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Roomlist Array and Flash List Component

Postby Bax » 18 Mar 2015, 10:29

The way you are populating your dataprovider is wrong: at each loop you are creating a new data provider, adding the same room name three times in it.
You should at least change it like this:

Code: Select all

import fl.controls.List;
import fl.data.DataProvider;
         
var roomList:Array = sfs.roomManager.getRoomList();
var dp:DataProvider = new DataProvider();

for each (var room:Room in roomList)
{
       trace("label: " + room.name);
       
       var item:Object = {};
       item.id = room.id;
       item.label = room.name;

       dp.addItem(item);
}

Map.pList.dataProvider = dp;


I also added the room id to the data provider, because you will probably need it later.
Paolo Bax
The SmartFoxServer Team
Sfulk
Posts: 54
Joined: 06 Jul 2012, 17:02

Re: Roomlist Array and Flash List Component

Postby Sfulk » 19 Mar 2015, 21:44

Thanks for clearing that up Bax! The room list is being properly displayed and I was able to send a JoinRoomReqest using the id from the selected item on the list. :D

Return to “SFS2X ActionScript 3 API”

Who is online

Users browsing this forum: No registered users and 17 guests