开发者

Shared Whiteboard with AS3. Code

I am building a shared whiteboard and I cannot get the shared whiteboard to be shared. I have successfully created a shared method and I see the shared object in the Admin Console that it is there and connected but can't see the drawing on a different browser. I am posting the entire actionscript code. Please help me if you can. Will be greatly appreciated. I am really new to actionscript.

package
{
 import flash.display.Sprite;
 import flash.display.MovieClip;
 import flash.net.SharedObject;
 import flash.events.NetStatusEvent;
 import flash.events.SyncEvent;
 import flash.net.NetConnection;
 import flash.events.MouseEvent;

 public class PenSOmc extends Sprite
 {
  private var drawing_so:SharedObject;
  private var nc:NetConnection;
  private var good:Boolean;
  private var isDrawing:Boolean;
  private var penMC:MovieClip;
  private var rpenMC:MovieClip;
    // Mouse Coordinates
    private var startX:int;
    private var startY:int;
    private var endX:int;
    private var endY:int;

  // Remote mouse coordinates
    private var rstartX:int;
    private var rstartY:int;
    private var rendX:int;
    private var rendY:int;


  public function PenSOmc ()
  {
   penMC = new MovieClip();
   var rtmpNow:String="rtmp://localhost/whiteboard";
   nc=new NetConnection;
   nc.connect(rtmpNow);
   nc.addEventListener(NetStatusEvent.NET_STATUS,doSO);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
   stage.addEventListener(MouseEvent.MOUSE_MOVE, drawing);
   stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
   addChild(penMC);
  }


  private function doSO (e:NetStatusEvent):void
  {
   good=e.info.code == "NetConnection.Connect.Success";
   if (good) 
   {
    //Shared object
    drawing_so = SharedObject.getRemote("draw",nc.uri,false);
    drawing_so.connect(nc);
    drawing_so.addEventListener(SyncEvent.SYNC,doUpdate);
    trace("Connected");
   }
  }

  private function doUpdate (se:SyncEvent):void
  {

   for (var cl:int = 0; cl < se.changeList.length; cl++)
   {    


    trace(se.changeList[cl].code);

    switch(se.changeList[cl].code)
    {
     case "clear":
      break;
     case "success":
      break;
     case "change":
      //var points:String = sharedObject.data.points;


      var pointsArray:Array = drawing_so.data.points.split(/,/);
      var rstartX:int = int(pointsArray[0]);
      var rstartY:int = int(pointsArray[1]);
      var rendX:int = int(pointsArray[2]);
      var rendY:int = int(pointsArray[3]);   
      rpenMC = new MovieClip();
      rpenMC.graphics.moveTo(rstartX,rstartY);
      rpenMC.graphics.lineTo(rendX,rendY);
      addChild(rpenMC);
      break;
    }
   }
  }

  private function startDrawing (e:MouseEvent):void
  {
   trace("Start drawing");
   isDrawing = true;
   var penTipSize:uint = 1;
   var penColor:Number = 0x000000;
   penMC.graphics.lineStyle(penTipSize, penColor, 1.0);
   startX = mouseX;
   startY = mouseY;
   penMC.graphics.moveTo开发者_JS百科(startX, startY);
  }

  private function stopDrawing (e:MouseEvent):void
  {
   trace("Stop drawing");
   isDrawing = false;
  }

  private function drawing (e:MouseEvent):void
  {
   if (isDrawing)
   {
    trace("Drawing");
    endX = mouseX;
    endY = mouseY;
    drawing_so.setProperty("points",startX + "," + startY + "," + endX + "," + endY);
    penMC.graphics.lineTo(endX, endY);
   }
  }

 }

}


This works:

package
{
 import flash.display.Sprite;
 import flash.display.MovieClip;
 import flash.net.SharedObject;
 import flash.events.NetStatusEvent;
 import flash.events.SyncEvent;
 import flash.net.NetConnection;
 import flash.events.MouseEvent;

 public class PenSOmc extends Sprite
 {
  private var drawing_so:SharedObject;
  private var nc:NetConnection;
  private var good:Boolean;
  private var isDrawing:Boolean;
  private var penMC:MovieClip;
  private var rpenMC:MovieClip;
    // Mouse Coordinates
    private var startX:int;
    private var startY:int;
    private var endX:int;
    private var endY:int;

  // Remote mouse coordinates
    private var rstartX:int;
    private var rstartY:int;
    private var rendX:int;
    private var rendY:int;


  public function PenSOmc ()
  {
   penMC = new MovieClip();
   var rtmpNow:String="rtmp://localhost/whiteboard";
   nc=new NetConnection;
   nc.connect(rtmpNow);
   nc.addEventListener(NetStatusEvent.NET_STATUS,doSO);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
   stage.addEventListener(MouseEvent.MOUSE_MOVE, drawing);
   stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);
   addChild(penMC);
  }


  private function doSO (e:NetStatusEvent):void
  {
   good=e.info.code == "NetConnection.Connect.Success";
   if (good) 
   {
    //Shared object
    drawing_so = SharedObject.getRemote("draw",nc.uri,false);
    drawing_so.connect(nc);
    drawing_so.addEventListener(SyncEvent.SYNC,doUpdate);
    trace("Connected");
   }
  }

  private function doUpdate (se:SyncEvent):void
  {

   for (var cl:int = 0; cl < se.changeList.length; cl++)
   {    

    switch(se.changeList[cl].code)
    {
     case "clear":
      break;
     case "success":
      break;
     case "change":
      //var points:String = sharedObject.data.points;
      var pointsArray:Array = drawing_so.data.points.split(/,/);
      var rstartX:int = int(pointsArray[0]);
      var rstartY:int = int(pointsArray[1]);
      var rendX:int = int(pointsArray[2]);
      var rendY:int = int(pointsArray[3]);   
      //penMC.graphics.moveTo(rstartX,rstartY);
      penMC.graphics.lineTo(rendX,rendY);
      break;
    }
   }
  }

  private function startDrawing (e:MouseEvent):void
  {
   trace("Start drawing");
   isDrawing = true;
   var penTipSize:uint = 1;
   var penColor:Number = 0x000000;
   penMC.graphics.lineStyle(penTipSize, penColor, 1.0);
   startX = mouseX;
   startY = mouseY;
   penMC.graphics.moveTo(startX, startY);
  }

  private function stopDrawing (e:MouseEvent):void
  {
   trace("Stop drawing");
   isDrawing = false;
  }

  private function drawing (e:MouseEvent):void
  {
   if (isDrawing)
   {
    trace("Drawing");
    endX = mouseX;
    endY = mouseY;
    drawing_so.setProperty("points",startX + "," + startY + "," + endX + "," + endY);
    penMC.graphics.lineTo(endX, endY);
   }
  }

 }

}


try to change this:

drawing_so = SharedObject.getRemote("draw",nc.uri,false);

to

drawing_so = SharedObject.getRemote("draw",nc.uri,true);

Thank you. Eugene


I have now made it a shared whiteboard by adding the following correction:

  rpenMC = new MovieClip();
  var rpenTipSize:uint = 4;
  var rpenColor:Number = 0x000000;
  rpenMC.graphics.lineStyle(rpenTipSize, rpenColor, 1.0);

But there is still one problem. The sharedobject doesn't update fast enough for the lines drawn to really be lines. They are dotted lines instead because some data is skipped as the sharedobject is updated in intervals. I used drawing_so.fps = 0 to set the frame rate but it is still not fast enough from on the browser. It is only fast enough on the flash test client to the server. I'm in the process of trying to figure out a better way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜