Bien aki va.. :
import it.gotoandplay.smartfoxserver.*
stop()
//----------------------------------------------------------
// Setup components callback functions
//----------------------------------------------------------
roomList_lb.setChangeHandler("changeRoom")
//----------------------------------------------------------
// Setup textfields
//----------------------------------------------------------
userName_txt.text = "Logged as: " + _global.myName
input_txt.text = ""
//----------------------------------------------------------
// Setup global vars
//----------------------------------------------------------
[color=blue]var areaW:Number = 600 // width of the are where avatars can move
var areaH:Number = 335 // hieght of the are where avatars can move
var avatarW:Number = 40 // width of the avatar
var avatarH:Number = 40 // height of the avatar
var inited:Boolean = false // flag to see if the application has been initialized
var myAvatar:MovieClip // my Avatar symbol[/color]
//----------------------------------------------------------
// Setup Mouse Listener
//----------------------------------------------------------
[color=blue]var myMouse:Object = {}
myMouse.onMouseDown = function()
{
if (inited)
{
if (!_global.isBusy)
{
var px:Number = int(avatarMC._xmouse)
var py:Number = int(avatarMC._ymouse)
if ((px > avatarW/2) && (px < areaW - avatarW/2) && (py > avatarH/2) && (py < areaH - avatarH/2))
{
// save new variables
// Please note that init is set to false:
// this means that we're only moving somewhere and we don't need to init tha avatar
smartfox.setUserVariables({px:px, py:py, init:false})
// method derived from the [flashAPI].as
// moves the mc using the "Quint" equation, with "InOut" easying
// to the new px,py position in 100 milliseconds.
myAvatar.easingTo("Quint", "InOut", px, py, 100)
}
}
}
}[/color]
//----------------------------------------------------------
// Handles the RoomList response from server
//----------------------------------------------------------
// roomList is an array of room objects
// Each room objects has these methods:
//
// getId() = get room id
// getName() = get room name
// isPrivate() = is the room private ? (0 = false, 1 = true)
// isTemp() = is the room temporary ? (0 = false, 1 = true)
// isGame() = is the room holding a game ? (0 = false, 1 = true)
// getUserCount() = get # of users in the room
// getMaxUsers() = get capacity of the room
// getUser(id) = get the user Object from a known user id
// getUserList() = get the userList Object
// variables = a property Object containing all room variables
//----------------------------------------------------------
[color=blue]smartfox.onRoomListUpdate = function(roomList:Object)
{
roomList_lb.removeAll()
for (var i:String in roomList)
{
var room = roomList[i]
roomList_lb.addItem(room.getName() + " (" + room.getUserCount() + ")", room.getId())
}
roomList_lb.sortItemsBy("label", "ASC")
// Join the default room
this.autoJoin()
}
// Handle roomListUpdate events that occurred before the playhead was moved to this frame
if (evtQueue.length > 0)
{
smartfox.onRoomListUpdate(evtQueue[0])
delete evtQueue
}
[/color]
//----------------------------------------------------------
// Handles the JoinRoom response
// Upon joining a room the client receives the userList of
// that room. By calling the getUserList() method on the
// room Object you get the userList Object
//
// You can use these methods on every user Object:
//
// getId() = get user unique id
// getName() = get user nickname
// variables = a property Object containing all user vars
/[color=blue]/----------------------------------------------------------
smartfox.onJoinRoom = function(roomObj:Object)
{
cleanAvatars()
var roomId:Number = roomObj.getId()
var userList:Object = roomObj.getUserList()
resetRoomSelected(roomId)
_global.currentRoom = roomObj
// Update Room Name in the avatar area
currentRoom.htmlText = "Current room: <b>" + roomObj.getName() + "</b>"
// Clear current list
userList_lb.removeAll()
for (var i:String in userList)
{
var user:User = userList[i]
var uName:String = user.getName()
var uId:Number = user.getId()
userList_lb.addItem(uName, uId)
if (uName != _global.myName)
{
var uVars:Object = user.getVariables()
var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + uId, Number(uId))
mc._x = uVars.px
mc._y = uVars.py
mc.disc.gotoAndStop(uVars.col)
mc.name.text = uName
}
}
// Sort names
userList_lb.sortItemsBy("label", "ASC")
setupMyAvatar()
}[/color]
//----------------------------------------------------------
// Handles Join Room Errors
//----------------------------------------------------------
smartfox.onJoinRoomError = function(errorMsg)
{
var win:MovieClip = showWindow("errorWindow")
win.errorMsg.text = errorMsg
// Put the selected room in the combo box back to its old value
resetRoomSelected(smartfox.activeRoomId)
}
//----------------------------------------------------------
// Handles a new user entering the room
//----------------------------------------------------------
[color=blue]smartfox.onUserEnterRoom = function(fromRoom, user)
{
var userId:Number = user.getId()
var userName:String = user.getName()
// Add user to the userList listbox
userList_lb.addItem(userName, userId)
// Sort names
userList_lb.sortItemsBy("label", "ASC")
updateRoomStatus(fromRoom)
// Show the user avatar
var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + userId, userId)
mc._x = user.variables["px"]
mc._y = user.variables["py"]
mc.name.text = userName
mc.disc.gotoAndStop(user.variables["col"])
}[/color]
//----------------------------------------------------------
// Handles a new user leaving the room
//----------------------------------------------------------
smartfox.onUserLeaveRoom = function(fromRoom:Number, usrId:Number)
{
for (var i:Number = 0; i < userList_lb.getLength(); i++)
{
var item:Object = userList_lb.getItemAt(i)
if (item.data == usrId)
{
var usrName:String = item.label
userList_lb.removeItemAt(i)
break
}
}
// Destroy avatar from screen
avatarMC["avatar_" + usrId].removeMovieClip()
// Sort names
userList_lb.sortItemsBy("label", "ASC")
updateRoomStatus(fromRoom)
}
//----------------------------------------------------------
// Handles a change of variables in User
//----------------------------------------------------------
[color=blue]smartfox.onUserVariablesUpdate = function(user:User)
{
var currRoom:Number = this.getActiveRoom()
var id:Number = user.getId()
var uVars:Object = user.getVariables()
var mc:MovieClip
if (uVars.init)
{
mc = avatarMC.attachMovie("avatar", "avatar_" + id, Number(id))
mc._x = uVars.px
mc._y = uVars.py
mc.disc.gotoAndStop(uVars.col)
mc.name.text = user.getName()
}
else
{
mc = avatarMC["avatar_" + id]
mc.easingTo("Quint", "InOut", uVars.px, uVars.py, 100)
}
}
[/color]
//----------------------------------------------------------
// Handles a user count change in the room
//----------------------------------------------------------
smartfox.onUserCountChange = function(roomObj:Room)
{
updateRoomStatus(roomObj.getId())
}
//----------------------------------------------------------
// Handles a public message
//----------------------------------------------------------
smartfox.onPublicMessage = function(msg:String, user:User)
{
var mc:MovieClip = avatarMC["avatar_" + user.getId()]
chat_txt.htmlText += "<b>[ " + user.getName() + " ]:</b> " + msg
main_sb.setScrollPosition(chat_txt.maxscroll)
if (msg.length > 50)
msg = msg.substring(0,48) + "..."
mc.bubble._visible = true
mc.bubble.message.text = msg
mc.deactivate()
}
//----------------------------------------------------------
// Handles an admin message
//----------------------------------------------------------
smartfox.onAdminMessage = function(msg:String, user:User)
{
var mc:MovieClip = showWindow("adminWindow")
mc.errorMsg.text += msg + "\n"
}
//----------------------------------------------------------
// Handles a new room added to the zone
//----------------------------------------------------------
[color=blue]smartfox.onRoomAdded = function(roomObj:Object)
{
roomList_lb.addItem(roomObj.getName() + " (" + roomObj.getUserCount() + ")", roomObj.getId())
roomList_lb.sortItemsBy("label", "ASC")
}
[/color]
//----------------------------------------------------------
// Handles a new room deleted in the zone
//----------------------------------------------------------
smartfox.onRoomDeleted = function(roomObj:Object)
{
for (var i = 0; i < roomList_lb.getLength(); i++)
{
if (roomObj.getId() == roomList_lb.getItemAt(i).data)
{
roomList_lb.removeItemAt(i)
break;
}
}
}
//----------------------------------------------------------
// Sends a new public chat message to the other users
//----------------------------------------------------------
[color=blue]function sendChatMsg()
{
if (input_txt.text.length > 0)
{
smartfox.sendPublicMessage(input_txt.text)
input_txt.text = ""
}
}[/color]
//----------------------------------------------------------
// Handle changes in the roomList listbox
//----------------------------------------------------------
function changeRoom()
{
if(!_global.isBusy)
{
var item:Object = roomList_lb.getSelectedItem()
// new Room id
var newRoom:String = item.data
if (newRoom != smartfox.activeRoomId)
{
// Check if new room is password protected
var priv:Boolean = smartfox.getRoom(newRoom).isPrivate()
if (priv)
{
// Save newroom as _global for later use
_global.newRoom = newRoom
showWindow("passwordWindow")
}
else
{
// Pass the room id
smartfox.joinRoom(item.data)
}
}
}
}
//----------------------------------------------------------
// Create my Avatar
// Randomly generate the x,y position and a color
//
// Then initiliaze my user variables saving:
// px = mc._x position
// py = mc._y position
// col = avatar color
// init = a flag, that tells that the avatar is initializing
//----------------------------------------------------------
[color=blue]function setupMyAvatar()
{
if (!inited)
{
var col:Number = int(Math.random() * 8) + 1
myAvatar = avatarMC.attachMovie("avatar", "avatar_" + smartfox.myUserId, 99999)
myAvatar.disc.gotoAndStop(col)
myAvatar.name.text = _global.myName
var px:Number = int(Math.random() * (areaW - myAvatar._width / 2))
var py:Number = int(Math.random() * (areaH - myAvatar._height / 2))
myAvatar._x = px
myAvatar._y = py
// Store the avatar position on the server
smartfox.setUserVariables({px:px, py:py, col:col, init:true})
// Testing ONLY!!!
trace("Variables SET")
trace("---------------------------------------------")
trace("myID : " + smartfox.myUserId)
var self = smartfox.getRoom(smartfox.activeRoomId).getUser(smartfox.myUserId)
trace("User Obj: " + self)
trace("Vars: " + self.variables)
trace("px: " + self.variables["px"])
trace("py: " + self.variables["py"])
inited = true
Mouse.addListener(myMouse)
}
}
[/color]
//----------------------------------------------------------
// Clean all Avatars from screen, except mine
//----------------------------------------------------------
function cleanAvatars()
{
for (var mc:String in avatarMC)
{
if (avatarMC[mc] != myAvatar)
avatarMC[mc].removeMovieClip()
}
}
//----------------------------------------------------------
// Enter a password protected room
// This function is being called by the pwdWindow movieclip
//----------------------------------------------------------
function loginProtectedRoom(pwd:String)
{
hideWindow("passwordWindow")
smartfox.joinRoom(_global.newRoom, pwd)
}
//----------------------------------------------------------
// Reset the selected item in the RoomList listbox
// Used when a password protected login has failed
//----------------------------------------------------------
function resetRoomSelected(id:Number)
{
var status:Boolean = roomList_lb.getEnabled()
roomList_lb.setEnabled(true)
for (var i:Number = 0; i < roomList_lb.getLength(); i++)
{
var item:Object = roomList_lb.getItemAt(i)
if (item.data == id)
{
roomList_lb.setSelectedIndex(i)
break
}
}
roomList_lb.setEnabled(status)
}
//----------------------------------------------------------
// Update the label of a Room in the listbox
// Used when updating the # of users in the room
//----------------------------------------------------------
function updateRoomStatus(roomId:Number)
{
var room:Room = smartfox.roomList[roomId]
var newLabel:String = room.getName() + " (" + room.getUserCount() + ")"
for (var i:Number = 0; i < roomList_lb.getLength(); i++)
{
var item:Object = roomList_lb.getItemAt(i)
if (roomId == item.data)
{
roomList_lb.replaceItemAt(i, newLabel, item.data)
break;
}
}
}
//----------------------------------------------------------
// Log out, and shut down connection
//----------------------------------------------------------
function closeConnection()
{
smartfox.disconnect()
gotoAndStop("connect")
}
Necesito qeu alguien me ayude, especialmente con las primeras, es decir, donde se declara el usuario y su nombre apra que lo vean todos..
Gracias