Quick (dirty) buttons for your Starling app

Categories Actionscript, games, Starling

Sometimes you need to control parts of you app for testing so a a quick way to build some buttons in Starling is very useful. In my case, I needed to test different parts of MathSumRun and adding an external library for this purpose only is an overkill.

buttons controll

Here is the dirty code for this:

ActionScript

  1. package com.cosmindolha.mathsumrun
  2. {
  3.     //import com.cosmindolha.mathsumrun.DataDispatcher;
  4.     import com.utils.Delay;
  5.     import starling.display.Canvas;
  6.     import starling.display.Sprite;
  7.     import starling.events.Touch;
  8.     import starling.events.TouchEvent;
  9.     import starling.events.TouchPhase;
  10.     import starling.text.TextField;
  11.     /**
  12.      * ...
  13.      * @author Cosmin Dolha
  14.      */
  15.     public class AppControl extends Sprite
  16.     {
  17.         //private var disp:DataDispatcher;
  18.        
  19.         public function AppControl()
  20.         {
  21.             x = 1150;
  22.             y = 50;
  23.             //disp = DataDispatcher.getDisp();
  24.            
  25.             button("Game Over", onGameOver, 0, 0, 75);
  26.             button("Level Done", onLevelFinished, 0, 30, 75);
  27.             button("End Game", onGameEnded, 0, 60, 75);
  28.             button("Time Up!", onTimeUp, 0, 90, 75);
  29.            
  30.             incrButtons("Stage", onPrevStage, onNextStage, 120);
  31.             incrButtons("Level", onPrevLevel, onNextLevel, 150);
  32.             incrButtons("Qstn", onPrevQuestion, onNextQuestion, 180);
  33.  
  34.            
  35.         }
  36.         private function onPrevQuestion():void
  37.         {
  38.            
  39.         }
  40.         private function onNextQuestion():void
  41.         {
  42.            
  43.         }      
  44.         private function onPrevLevel():void
  45.         {
  46.            
  47.         }
  48.         private function onNextLevel():void
  49.         {
  50.            
  51.         }  
  52.         private function onPrevStage():void
  53.         {
  54.            
  55.         }
  56.         private function onNextStage():void
  57.         {
  58.            
  59.         }
  60.         private function onTimeUp():void
  61.         {
  62.             trace("on time up");
  63.         }      
  64.         private function onGameEnded():void
  65.         {
  66.             trace("on game end");
  67.         }
  68.         private function onLevelFinished():void
  69.         {
  70.             trace("level finished press");
  71.         }      
  72.         private function onGameOver():void
  73.         {
  74.             trace("game over press");
  75.         }
  76.         private function incrButtons(str:String, prevFunc:Function, nextFunc:Function, y:Number):void
  77.         {
  78.             button("< -", prevFunc, 0, y, 35);
  79.             var labelField:TextField = new TextField(35, 20, str, "Verdana", 9, 0xffffff, false);
  80.             labelField.touchable = false;
  81.             addChild(labelField);
  82.             labelField.x = 20;
  83.             labelField.y = y;
  84.             button("->", nextFunc, 40, y, 35);
  85.         }
  86.         private function button(str:String, func:Function, x:Number, y:Number, w:Number):void
  87.         {
  88.             var sp:Sprite = new Sprite();
  89.             sp.x = x;
  90.             sp.y = y;
  91.            
  92.             var canvas:Canvas = new Canvas();
  93.             canvas.beginFill(0xffffff);
  94.            
  95.             canvas.drawRectangle(0, 0, w, 25);
  96.             canvas.endFill();
  97.            
  98.             var txt:TextField = new TextField(w, 20, str, "Verdana", 9, 0xffffff, false);
  99.             txt.touchable = false;
  100.             canvas.alpha = .2;
  101.            
  102.             sp.addChild(canvas);
  103.             sp.addChild(txt);
  104.             sp.addEventListener(TouchEvent.TOUCH, onTouch);
  105.             function onTouch(e:TouchEvent):void
  106.             {  
  107.                 var touch:Touch = e.getTouch(stage, TouchPhase.BEGAN);
  108.                 if (touch == null) return
  109.                 func();
  110.                 canvas.alpha = .5;
  111.                 var returnDelay:Delay = new Delay(returnAlpha, 100);
  112.                 function returnAlpha():void
  113.                 {
  114.                     canvas.alpha = .2;
  115.                 }
  116.             }
  117.             addChild(sp);
  118.            
  119.         }
  120.     }
  121.  
  122. }

And the Delay class:

ActionScript

  1. package com.utils
  2. {
  3.     import flash.events.TimerEvent;
  4.     import flash.utils.Timer;
  5.     public class Delay
  6.     {
  7.         private var timer:Timer;   
  8.         private var delayedFunction:Function;
  9.         public function Delay(functionToDelay:Function, delayMilisec:Number)
  10.         {
  11.             this.delayedFunction = functionToDelay;
  12.             timer = new Timer(delayMilisec, 1);
  13.             timer.addEventListener(TimerEvent.TIMER_COMPLETE, callDelayedFunction);
  14.             timer.start();
  15.         }  
  16.         private function callDelayedFunction(event:TimerEvent):void
  17.         {
  18.             timer.removeEventListener(TimerEvent.TIMER_COMPLETE, callDelayedFunction);
  19.             delayedFunction();
  20.         }
  21.     }
  22. }
Share this
Facebooktwitter

Dynamic access your xml data

Categories Actionscript

Let’s say you have this xml data in an external file data.xml:

ActionScript

  1. <app>
  2.   <usa>
  3.    <Alabama pic="somepic.jpg" info="some info" />
  4.    <Alaska pic="somepic.jpg" info="some info" />
  5.    <Arizona pic="somepic.jpg" info="some info" />
  6.   </usa>
  7.  </app>

If you want to access your Alaska info data in actionscript, using just the name Alaska in a variable you can do:

ActionScript

  1. // assuming you loaded your external xml in xmlData
  2.  
  3. var country:String = "Alaska"
  4.  
  5. xmlData.usa.children().(name() == country).@info
Share this
Facebooktwitter

Multiuser flash Whiteboard

Categories Flash apps

To view the app you will need flash player 10.1.

The Flash p2p Whiteboard, let’s you:

  • Broadcast an image
  • Simple drawing
  • Text annotation
  • Simple group chat
  • Image, drawing and text annotations X, Y screen position sharing

Video:

How it works:

The whiteboard uses the new p2p flash player 10.1 NetGroup Class to connect app users and netGroup.post(message) to broadcast a message to the group.

Here is some of the code it uses:

ActionScript

  1. const StratusAddress:String = "rtmfp://stratus.rtmfp.net/your-stratus-key/";
  2. var nc:NetConnection;
  3. var netGroup:NetGroup;
  4. var user:String;
  5. var messageIncrement:int;
  6.  
  7. var myGroupName:String = "your.app.group.name";
  8.  
  9. function connect():void
  10. {
  11.     nc = new NetConnection();
  12.     nc.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
  13.     nc.connect(StratusAddress);
  14.  
  15. }
  16. function setupGroup():void
  17. {
  18.     var groupspec:GroupSpecifier = new GroupSpecifier(myGroupName);
  19.     groupspec.serverChannelEnabled = true;
  20.     groupspec.postingEnabled = true;
  21.  
  22.     netGroup = new NetGroup(nc,groupspec.groupspecWithAuthorizations());
  23.  
  24.     netGroup.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
  25.  
  26.  
  27.     user = "user" + Math.round(Math.random() * 10000); // we need unique usernames
  28.  
  29.     userNameText.text = user;
  30.  
  31.     createDrawLayer(user); // creates the drawing layer based on the username
  32.  
  33.  
  34. }
  35. function netStatus(event:NetStatusEvent):void
  36. {
  37.     //trace(event.info.code);
  38.  
  39.     switch (event.info.code)
  40.     {
  41.  
  42.         case "NetConnection.Connect.Success" :
  43.             setupGroup();
  44.             break;
  45.         case "NetGroup.Connect.Success" :
  46.             txtChatMessages.text = "connected to group";
  47.             break;
  48.         case "NetGroup.Posting.Notify" : // fires each time a message is posted in the group
  49.             receiveMessage(event.info.message);
  50.             break;
  51.         case "NetGroup.Neighbor.Connect" :
  52.    
  53.             break;
  54.         case "NetGroup.Neighbor.Disconnect" :
  55.  
  56.             break;
  57.     }
  58. }
  59. function sendMessage():void
  60. {
  61.     messageIncrement++;
  62.  
  63.     var message:Object = new Object();
  64.  
  65.     message.sender = netGroup.convertPeerIDToGroupAddress(nc.nearID);
  66.     message.user = user;
  67.     message.rnd = messageIncrement;
  68.     message.text = txtMessage.text;
  69.     message.action = "postChat";
  70.  
  71.     netGroup.post(message);
  72.     incomingMessage(message);
  73.     txtMessage.text = "";
  74. }
  75.  
  76. function receiveMessage(message:Object):void
  77. {
  78.  
  79.     if (message.user != user)
  80.     {
  81.         if (message.action != null)
  82.         {
  83.             switch (message.action)
  84.             {
  85.                 case "postChat" :
  86.                     incomingMessage(message);
  87.                     break;
  88.                 case "clear" : // delete your own drawing across all users connected
  89.                     clearMc(message.user);
  90.                     break;
  91.                 case "postImage" :
  92.                     showGroupImage(message.image);
  93.                     break;
  94.                 case "postImagePozition" : // changes the position of the broadcasted image
  95.                     showGroupImagePoz(message.imageX, message.imageY);
  96.                     break;
  97.                 case "postTextField" : // creates the text annotation for all users in the group
  98.                     recreateTextField(message.txtObj);
  99.                     break;
  100.                 case "postTextFieldPozition" : // changes the position of  a text annotation
  101.                     pozitionTextField(message.txtMc, message.px, message.py);
  102.                     break;
  103.                 case "editTextField" : // function that edits the changes made to a text annotation
  104.                     editedTextField(message.txtName, message.txtContent);
  105.                     break;
  106.  
  107.             }
  108.         }
  109.     }
  110.  
  111. }
  112. connect(); // start the application
Share this
Facebooktwitter