与AS3共享白板。码

时间:2010-08-18 23:07:15

标签: actionscript-3 graphics shared-objects whiteboard

我正在构建共享白板,但我无法共享共享白板。我已经成功创建了一个共享方法,我在管理控制台中看到它在那里并且已连接但在另一个浏览器上看不到图形的共享对象。我发布了整个动作脚本代码。如果可以,请你帮助我。将不胜感激。我是动作脚本的新手。

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(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);
   }
  }

 }

}

3 个答案:

答案 0 :(得分:1)

这有效:

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);
   }
  }

 }

}

答案 1 :(得分:0)

尝试改变这个:

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

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

谢谢。 尤金

答案 2 :(得分:0)

我现在通过添加以下更正使其成为共享白板:

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

但仍有一个问题。共享对象的更新速度不足以使绘制成真正线条的线条。它们是虚线,因为在间隔更新共享对象时会跳过某些数据。我使用drawing_so.fps = 0来设置帧速率,但在浏览器上仍然不够快。它在Flash测试客户端上对服务器来说足够快。我正在试图想出一个更好的方法。