Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.46 KB | None | 0 0
  1. import it.gotoandplay.smartfoxserver.*;
  2. stop();
  3.  
  4. //----------------------------------------------------------
  5. // Setup components callback functions
  6. //----------------------------------------------------------
  7. roomList_lb.setChangeHandler("changeRoom");
  8.  
  9.  
  10.  
  11. //----------------------------------------------------------
  12. // Setup textfields
  13. //----------------------------------------------------------
  14. userName_txt.text = "Logged as: " + _global.myName;
  15. input_txt.text = "";
  16.  
  17.  
  18.  
  19. //----------------------------------------------------------
  20. // Setup global vars
  21. //----------------------------------------------------------
  22. var areaW:Number = 600;// width of the are where avatars can move
  23. var areaH:Number = 335;// hieght of the are where avatars can move
  24. var avatarW:Number = 40;// width of the avatar
  25. var avatarH:Number = 40;// height of the avatar
  26.  
  27. var inited:Boolean = false;// flag to see if the application has been initialized
  28.  
  29. var myAvatar:MovieClip;// my Avatar symbol
  30.  
  31.  
  32. //----------------------------------------------------------
  33. // Setup Mouse Listener
  34. //----------------------------------------------------------
  35. var myMouse:Object = {};
  36. myMouse.onMouseDown = function()
  37. {
  38. if (inited)
  39. {
  40. if (!_global.isBusy)
  41. {
  42. var px:Number = int(avatarMC._xmouse);
  43. var py:Number = int(avatarMC._ymouse);
  44. if ((px > avatarW / 2) && (px < areaW - avatarW / 2) && (py > avatarH / 2) && (py < areaH - avatarH / 2))
  45. {
  46. // save new variables
  47. // Please note that init is set to false:
  48. // this means that we're only moving somewhere and we don't need to init tha avatar
  49. smartfox.setUserVariables({px:px, py:py, init:false});
  50. // method derived from the [flashAPI].as
  51. // moves the mc using the "Quint" equation, with "InOut" easying
  52. // to the new px,py position in 100 milliseconds.
  53. myAvatar.easingTo("Quint","InOut",px,py,100);
  54. }
  55. }
  56. }
  57. };
  58.  
  59.  
  60.  
  61. //----------------------------------------------------------
  62. // Handles the RoomList response from server
  63. //----------------------------------------------------------
  64. // roomList is an array of room objects
  65. // Each room objects has these methods:
  66. //
  67. // getId()= get room id
  68. // getName()= get room name
  69. // isPrivate()= is the room private ? (0 = false, 1 = true)
  70. // isTemp()= is the room temporary ? (0 = false, 1 = true)
  71. // isGame()= is the room holding a game ? (0 = false, 1 = true)
  72. // getUserCount()= get # of users in the room
  73. // getMaxUsers()= get capacity of the room
  74. // getUser(id)= get the user Object from a known user id
  75. // getUserList()= get the userList Object
  76. // variables= a property Object containing all room variables
  77. //----------------------------------------------------------
  78. smartfox.onRoomListUpdate = function(roomList:Object)
  79. {
  80. roomList_lb.removeAll();
  81. for (var i:String in roomList);
  82. {
  83. var room = roomList[i];
  84. roomList_lb.addItem(room.getName() + " (" + room.getUserCount() + ")",room.getId());
  85. }
  86. roomList_lb.sortItemsBy("label","ASC");
  87. // Join the default room
  88. this.autoJoin();
  89. };
  90.  
  91. // Handle roomListUpdate events that occurred before the playhead was moved to this frame
  92. if (evtQueue.length > 0)
  93. {
  94. smartfox.onRoomListUpdate(evtQueue[0]);
  95. delete evtQueue;
  96. }
  97. //----------------------------------------------------------
  98. // Handles the JoinRoom response
  99. // Upon joining a room the client receives the userList of
  100. // that room. By calling the getUserList() method on the
  101. // room Object you get the userList Object
  102. //
  103. // You can use these methods on every user Object:
  104. //
  105. // getId()= get user unique id
  106. // getName()= get user nickname
  107. // variables= a property Object containing all user vars
  108. //----------------------------------------------------------
  109. smartfox.onJoinRoom = function(roomObj:Object)
  110. {
  111. cleanAvatars();
  112. var roomId:Number = roomObj.getId();
  113. var userList:Object = roomObj.getUserList();
  114. resetRoomSelected(roomId);
  115. _global.currentRoom = roomObj;
  116. // Update Room Name in the avatar area
  117. currentRoom.htmlText = "Current room: <b>" + roomObj.getName() + "</b>";
  118. // Clear current list
  119. userList_lb.removeAll();
  120. for (var i:String in userList);
  121. {
  122. var user:User = userList[i];
  123. var uName:String = user.getName();
  124. var uId:Number = user.getId();
  125. userList_lb.addItem(uName,uId);
  126. if (uName != _global.myName)
  127. {
  128. var uVars:Object = user.getVariables();
  129. var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + uId, Number(uId));
  130. mc._x = uVars.px;
  131. mc._y = uVars.py;
  132. mc.disc.gotoAndStop(uVars.col);
  133. mc.name.text = uName;
  134. }
  135. }
  136.  
  137. // Sort names
  138. userList_lb.sortItemsBy("label","ASC");
  139. setupMyAvatar();
  140. };
  141.  
  142.  
  143.  
  144. //----------------------------------------------------------
  145. // Handles Join Room Errors
  146. //----------------------------------------------------------
  147. smartfox.onJoinRoomError = function(errorMsg)
  148. {
  149. var win:MovieClip = showWindow("errorWindow");
  150. win.errorMsg.text = errorMsg;
  151. // Put the selected room in the combo box back to its old value
  152. resetRoomSelected(smartfox.activeRoomId);
  153. };
  154.  
  155.  
  156.  
  157. //----------------------------------------------------------
  158. // Handles a new user entering the room
  159. //----------------------------------------------------------
  160. smartfox.onUserEnterRoom = function(fromRoom, user)
  161. {
  162. var userId:Number = user.getId();
  163. var userName:String = user.getName();
  164. // Add user to the userList listbox
  165. userList_lb.addItem(userName,userId);
  166. // Sort names
  167. userList_lb.sortItemsBy("label","ASC");
  168. updateRoomStatus(fromRoom);
  169. // Show the user avatar
  170. var mc:MovieClip = avatarMC.attachMovie("avatar", "avatar_" + userId, userId);
  171.  
  172. mc._x = user.variables["px"];
  173. mc._y = user.variables["py"];
  174. mc.name.text = userName;
  175.  
  176. mc.disc.gotoAndStop(user.variables["col"]);
  177. };
  178.  
  179.  
  180.  
  181. //----------------------------------------------------------
  182. // Handles a new user leaving the room
  183. //----------------------------------------------------------
  184. smartfox.onUserLeaveRoom = function(fromRoom:Number, usrId:Number)
  185. {
  186. for (var i:Number = 0; i < userList_lb.getLength(); i++)
  187. {
  188. for (var i:Number = 0; i < userList_lb.getLength(); i++)
  189. {
  190. var item:Object = userList_lb.getItemAt(i);
  191. if (item.data == usrId)
  192.  
  193. var usrName:String = item.label;
  194. userList_lb.removeItemAt(i);
  195. break;
  196. }
  197. }
  198.  
  199. // Destroy avatar from screen
  200. avatarMC["avatar_" + usrId].removeMovieClip();
  201. // Sort names
  202. userList_lb.sortItemsBy("label","ASC");
  203. updateRoomStatus(fromRoom);
  204.  
  205. };
  206.  
  207.  
  208.  
  209. //----------------------------------------------------------
  210. // Handles a change of variables in User
  211. //----------------------------------------------------------
  212. smartfox.onUserVariablesUpdate = function(user:User)
  213. {
  214. var currRoom:Number = this.getActiveRoom();
  215. var id:Number = user.getId();
  216. var uVars:Object = user.getVariables();
  217. var mc:MovieClip;
  218. if (uVars.init)
  219. {
  220. mc = avatarMC.attachMovie("avatar", "avatar_" + id, Number(id));
  221. mc._x = uVars.px;
  222. mc._y = uVars.py;
  223. mc.disc.gotoAndStop(uVars.col);
  224. mc.name.text = user.getName();
  225. }
  226. else
  227. {
  228. mc = avatarMC["avatar_" + id];
  229. mc.easingTo("Quint","InOut",uVars.px,uVars.py,100);
  230. }
  231.  
  232. };
  233.  
  234.  
  235.  
  236. //----------------------------------------------------------
  237. // Handles a user count change in the room
  238. //----------------------------------------------------------
  239. smartfox.onUserCountChange = function(roomObj:Room)
  240. {
  241. updateRoomStatus(roomObj.getId());
  242. };
  243.  
  244.  
  245.  
  246. //----------------------------------------------------------
  247. // Handles a public message
  248. //----------------------------------------------------------
  249. smartfox.onPublicMessage = function(msg:String, user:User)
  250. {
  251. var mc:MovieClip = avatarMC["avatar_" + user.getId()];
  252. //TalkingFeatures
  253. //ChatLog
  254. if(user.getName() == "Adam"){
  255. _root.chatLog.chatText.htmlText = _root.chatLog.chatText.htmlText + " <font color='#990000'>[STAFF] " + (user.getName() + ": " + msg + "</font>" + "\n");
  256. } else {
  257. _root.chatLog.chatText.htmlText = _root.chatLog.chatText.htmlText + ("" + user.getName() + " said... " + msg + "\n");
  258. }
  259. _root.chatLog.main_sb.setScrollPosition(_root.chatLog.chatText.maxscroll);
  260. //Bubbles
  261. if (user.getName() == "Adam"){
  262. mc.bubble.gotoAndStop("mod");
  263. }
  264. else
  265. {
  266. mc.bubble.gotoAndStop("normal");
  267. }
  268. //Emocations
  269. if (msg == ":P")
  270. {
  271. mc.bubble.gotoAndStop("a");
  272. }
  273. if (msg == ":|"){
  274. mc.bubble.gotoAndStop("b");
  275. }
  276. if (msg == ":)"){
  277. mc.bubble.gotoAndStop("c");
  278. }
  279. else
  280. if (msg == ":("){
  281. mc.bubble.gotoAndStop("d");
  282. }
  283. if (msg == ":?"){
  284. mc.bubble.gotoAndStop("e");
  285. }
  286. else
  287. if(msg == "xD"){
  288. mc.bubble.gotoAndStop("f");
  289. }
  290. if(msg == "Music"){
  291. mc.bubble.gotoAndStop("g");
  292. }
  293. //ClearChatlog
  294. function mods()
  295. {
  296. var myMenu:ContextMenu = new ContextMenu();
  297. myMenu.hideBuiltInItems();
  298. myMenu.hideBuiltInItems();
  299. var clearChatLog:ContextMenuItem = new ContextMenuItem("Clear Chat Log", clearChatText);
  300. myMenu.customItems.push(clearChatLog);
  301. var logOut:ContextMenuItem = new ContextMenuItem("Logout", logOutNow);
  302. myMenu.customItems.push(logOut);
  303. _root.menu = myMenu;
  304. mp._visible=true;
  305.  
  306. //2nd Part
  307.  
  308.  
  309.  
  310. function clearChatText()
  311. {
  312. _root.chatLog.chatText.htmlText = "Chat Log Has Been Cleared by a moderator.";
  313. }
  314. function logOutNow()
  315. {
  316. if (!_global.isBusy)
  317. {
  318. closeConnection();
  319. }
  320.  
  321. }
  322. }
  323.  
  324. if (msg.length > 70)
  325. {
  326. msg = msg.substring(0, 69) + "...";
  327. }
  328. mc.bubble._visible = true;
  329. mc.bubble.message.text = msg;
  330. mc.deactivate();
  331. };
  332.  
  333.  
  334.  
  335. //----------------------------------------------------------
  336. // Handles an admin message
  337. //----------------------------------------------------------
  338. smartfox.onAdminMessage = function(msg:String, user:User)
  339. {
  340. var mc:MovieClip = showWindow("adminWindow");
  341. mc.errorMsg.text += msg + "\n";
  342. };
  343.  
  344.  
  345. //----------------------------------------------------------
  346. // Handles a new room added to the zone
  347. //----------------------------------------------------------
  348. smartfox.onRoomAdded = function(roomObj:Object)
  349. {
  350. roomList_lb.addItem(roomObj.getName() + " (" + roomObj.getUserCount() + ")",roomObj.getId());
  351. roomList_lb.sortItemsBy("label","ASC");
  352. };
  353.  
  354.  
  355.  
  356. //----------------------------------------------------------
  357. // Handles a new room deleted in the zone
  358. //----------------------------------------------------------
  359. smartfox.onRoomDeleted = function(roomObj:Object)
  360. {
  361. for (var i = 0; i < roomList_lb.getLength(); i++)
  362. {
  363. if (roomObj.getId() == roomList_lb.getItemAt(i).data)
  364. {
  365. roomList_lb.removeItemAt(i);
  366. break;
  367. }
  368. }
  369. };
  370.  
  371.  
  372.  
  373. //----------------------------------------------------------
  374. // Sends a new public chat message to the other users
  375. //----------------------------------------------------------
  376. function sendChatMsg()
  377. {
  378. if (input_txt.text.length > 0)
  379. {
  380. smartfox.sendPublicMessage(input_txt.text);
  381. input_txt.text = "";
  382. }
  383. }
  384.  
  385.  
  386.  
  387. //----------------------------------------------------------
  388. // Handle changes in the roomList listbox
  389. //----------------------------------------------------------
  390. function changeRoom()
  391. {
  392. if (!_global.isBusy)
  393. {
  394. var item:Object = roomList_lb.getSelectedItem();
  395. // new Room id
  396. var newRoom:String = item.data;
  397. if (newRoom != smartfox.activeRoomId)
  398. {
  399. // Check if new room is password protected
  400. var priv:Boolean = smartfox.getRoom(newRoom).isPrivate();
  401. if (priv)
  402. {
  403. // Save newroom as _global for later use
  404. _global.newRoom = newRoom;
  405. showWindow("passwordWindow");
  406. }
  407. else
  408. {
  409. // Pass the room id
  410. smartfox.joinRoom(item.data);
  411. }
  412. }
  413. }
  414. }
  415.  
  416.  
  417.  
  418. //----------------------------------------------------------
  419. // Create my Avatar
  420. // Randomly generate the x,y position and a color
  421. //
  422. // Then initiliaze my user variables saving:
  423. // px = mc._x position
  424. // py = mc._y position
  425. // col = avatar color
  426. // init = a flag, that tells that the avatar is initializing
  427. //----------------------------------------------------------
  428. function setupMyAvatar()
  429. {
  430. if (!inited)
  431. {
  432. var col:Number = int(Math.random() * 8) + 1;
  433. myAvatar = avatarMC.attachMovie("avatar", "avatar_" + smartfox.myUserId, 99999);
  434. myAvatar.disc.gotoAndStop(col);
  435. myAvatar.name.text = _global.myName;
  436. var px:Number = int(Math.random() * (areaW - myAvatar._width / 2));
  437. var py:Number = int(Math.random() * (areaH - myAvatar._height / 2));
  438. myAvatar._x = px;
  439. myAvatar._y = py;
  440. // Store the avatar position on the server
  441. smartfox.setUserVariables({px:px, py:py, col:col, init:true});
  442. // Testing ONLY!!!
  443. trace("Variables SET");
  444. trace("---------------------------------------------");
  445. trace("myID : " + smartfox.myUserId);
  446. var self = smartfox.getRoom(smartfox.activeRoomId).getUser(smartfox.myUserId);
  447. trace("User Obj: " + self);
  448. trace("Vars: " + self.variables);
  449. trace("px: " + self.variables["px"]);
  450. trace("py: " + self.variables["py"]);
  451. inited = true;
  452. Mouse.addListener(myMouse);
  453. }
  454. }
  455.  
  456.  
  457.  
  458. //----------------------------------------------------------
  459. // Clean all Avatars from screen, except mine
  460. //----------------------------------------------------------
  461. function cleanAvatars()
  462. {
  463. for (var mc:String in avatarMC);
  464. {
  465. if (avatarMC[mc] != myAvatar)
  466. {
  467. avatarMC[mc].removeMovieClip();
  468. }
  469. }
  470. }
  471.  
  472.  
  473.  
  474. //----------------------------------------------------------
  475. // Enter a password protected room
  476. // This function is being called by the pwdWindow movieclip
  477. //----------------------------------------------------------
  478. function loginProtectedRoom(pwd:String)
  479. {
  480. hideWindow("passwordWindow");
  481. smartfox.joinRoom(_global.newRoom,pwd);
  482. }
  483.  
  484.  
  485.  
  486. //----------------------------------------------------------
  487. // Reset the selected item in the RoomList listbox
  488. // Used when a password protected login has failed
  489. //----------------------------------------------------------
  490. function resetRoomSelected(id:Number)
  491. {
  492. var status:Boolean = roomList_lb.getEnabled();
  493.  
  494. roomList_lb.setEnabled(true);
  495. for (var i:Number = 0; i < roomList_lb.getLength(); i++)
  496. {
  497. var item:Object = roomList_lb.getItemAt(i);
  498. if (item.data == id)
  499. {
  500. roomList_lb.setSelectedIndex(i);
  501. break;
  502. }
  503. }
  504.  
  505. roomList_lb.setEnabled(status);
  506. }
  507.  
  508.  
  509.  
  510. //----------------------------------------------------------
  511. // Update the label of a Room in the listbox
  512. // Used when updating the # of users in the room
  513. //----------------------------------------------------------
  514. function updateRoomStatus(roomId:Number)
  515. {
  516. var room:Room = smartfox.roomList[roomId];
  517. var newLabel:String = room.getName() + " (" + room.getUserCount() + ")";
  518. for (var i:Number = 0; i < roomList_lb.getLength(); i++)
  519. {
  520. var item:Object = roomList_lb.getItemAt(i);
  521. if (roomId == item.data)
  522. {
  523. roomList_lb.replaceItemAt(i,newLabel,item.data);
  524. break;
  525. }
  526. }
  527. }
  528.  
  529.  
  530.  
  531. //----------------------------------------------------------
  532. // Log out, and shut down connection
  533. //----------------------------------------------------------
  534. function closeConnection()
  535. {
  536. smartfox.disconnect();
  537. gotoAndStop("connect");
  538. }
  539.  
  540. //----------------------------------------------------------
  541. //Shows Playercard
  542. //----------------------------------------------------------
  543. function showPlayerCard()
  544. {
  545. loadMovieNum("playercard.swf", 3);
  546. }
  547. //handles Buddy List
  548. var buddyListLoaded:Boolean = false;
  549.  
  550. //----------------------------------------------------------
  551. // Handles the loading of the buddyList from server
  552. //----------------------------------------------------------
  553. smartfox.onBuddyList = function(bList:Array)
  554. {
  555. buddyList_lb.removeAll();
  556. for (var i in bList)
  557. {
  558. var label:String = bList[i].name + " [ " + (bList[i].isOnline ? "On" : "Off") + " ]";
  559. buddyList_lb.addItem(label,bList[i]);
  560. }
  561.  
  562. buddyList_lb.sortItemsBy("label","ASC");
  563. };
  564.  
  565.  
  566.  
  567. //----------------------------------------------------------
  568. // Handles errors in loading the buddyList from server
  569. //----------------------------------------------------------
  570. smartfox.onBuddyListError = function(errorMsg:String)
  571. {
  572. var win:MovieClip = showWindow("errorWindow");
  573. win.errorMsg.text = errorMsg;
  574. };
  575.  
  576.  
  577.  
  578. //----------------------------------------------------------
  579. // A Buddy has changed status
  580. //----------------------------------------------------------
  581. smartfox.onBuddyListUpdate = function(buddy:Object)
  582. {
  583. var label = buddy.name + " [ " + (buddy.isOnline ? "Online" : "Offline") + " ]";
  584.  
  585. for (var i:Number = 0; i < buddyList_lb.getLength(); i++)
  586. {
  587. var item:Object = buddyList_lb.getItemAt(i);
  588.  
  589. if (item.data.name == buddy.name)
  590. {
  591. buddyList_lb.replaceItemAt(i,label,buddy);
  592. break;
  593. }
  594. }
  595. };
  596.  
  597.  
  598.  
  599. //----------------------------------------------------------
  600. // The server responds sending the room(s) where the user is
  601. // currently located. You can then use this id to join
  602. // the user.
  603. //----------------------------------------------------------
  604. smartfox.onBuddyRoom = function(list:Array)
  605. {
  606. var roomId:Number = list[0];
  607. if (roomId != smartfox.activeRoomId)
  608. {
  609. // Check if new room is password protected
  610. var priv:Boolean = smartfox.getRoom(roomId).isPrivate();
  611. if (priv)
  612. {
  613. // Save newroom as _global for later use
  614. _global.newRoom = roomId;
  615. showWindow("passwordWindow");
  616. }
  617. else
  618. {
  619. // Pass the room id
  620. smartfox.joinRoom(roomId);
  621. }
  622. }
  623. };
  624.  
  625.  
  626.  
  627. //----------------------------------------------------------
  628. // Handle changes in the roomList listbox
  629. //----------------------------------------------------------
  630. function changeRoom()
  631. {
  632. if (!_global.isBusy)
  633. {
  634. var item:Object = roomList_lb.getSelectedItem();
  635. // new Room id
  636. var newRoom:Number = item.data;
  637. if (newRoom != smartfox.activeRoomId)
  638. {
  639. // Check if new room is password protected
  640. var priv:Boolean = smartfox.getRoom(newRoom).isPrivate();
  641. if (priv)
  642. {
  643. // Save newroom as _global for later use
  644. _global.newRoom = newRoom;
  645. showWindow("passwordWindow");
  646. }
  647. else
  648. {
  649. // Pass the room id
  650. smartfox.joinRoom(item.data);
  651. }
  652. }
  653. }
  654. }
  655.  
  656.  
  657.  
  658. //----------------------------------------------------------
  659. // Enter a password protected room
  660. // This function is being called by the pwdWindow movieclip
  661. //----------------------------------------------------------
  662. function loginProtectedRoom(pwd:String)
  663. {
  664. hideWindow("passwordWindow");
  665. smartfox.joinRoom(_global.newRoom,pwd);
  666. }
  667.  
  668.  
  669.  
  670.  
  671.  
  672. //----------------------------------------------------------
  673. // Add the currently selected user to the buddy list
  674. //----------------------------------------------------------
  675. function addBuddy()
  676. {
  677. var item:Object = userList_lb.getSelectedItem();
  678. if (item != undefined)
  679. {
  680. smartfox.addBuddy(item.label);
  681. }
  682. }
  683.  
  684.  
  685. //----------------------------------------------------------
  686. // Remove the currently selected buddy from the buddy list
  687. //----------------------------------------------------------
  688. function removeBuddy()
  689. {
  690. var item:Object = buddyList_lb.getSelectedItem();
  691. if (item != undefined)
  692. {
  693. smartfox.removeBuddy(item.data.name);
  694. }
  695. }
  696.  
  697.  
  698.  
  699.  
  700. //----------------------------------------------------------
  701. // Removes all buddies from the buddy list
  702. //----------------------------------------------------------
  703. function clearBuddyList()
  704. {
  705. smartfox.clearBuddyList();
  706. }
  707.  
  708.  
  709.  
  710. //----------------------------------------------------------
  711. // In order to join the buddy we have to ask where he's
  712. // currently located
  713. //----------------------------------------------------------
  714. function joinBuddy()
  715. {
  716. var item:Object = buddyList_lb.getSelectedItem();
  717. if (item != undefined)
  718. {
  719. smartfox.getBuddyRoom(item.data);
  720. }
  721. }
  722.  
  723. if (!buddyListLoaded)
  724. {
  725. buddyListLoaded = true;
  726. smartfox.loadBuddyList();
  727. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement