【问题标题】:Adding 1 object randomly to the screen在屏幕上随机添加 1 个对象
【发布时间】:2012-01-24 19:50:27
【问题描述】:

在我之前的帖子Adding a object randomly on the screen in as3 中,我解释了我的具体情况。但我会再过一遍。我有一个带有类的盒子(不是我的文档类。我确实有一个叫做 Main 但这只是一个引用我的盒子的 AS 类。)类名称是 Box,我的 MC 盒子导出为 Box。这是代码

这是我在主时间线上的主文件

addEventListener(Event.ENTER_FRAME, createbox);
var _box:Box = new Box;
var boxlimit:int = 2;
function createbox (event:Event):void{
_box = new Box;
_box.x = Math.random()*stage.stageWidth ;
_box.y = Math.random()*stage.stageHeight;
addChild(_box);
}

这是我的 Box 类

//package {
//  import flash.display.MovieClip;
//  import flash.events.Event;
//  import flash.events.MouseEvent;
//
//  public class Main extends MovieClip {
//      
//      public function Main() {
//          createBox();
//
//      }
//
//      private function createBox():void {
//
//           trace(Math.random()*stage.stageWidth)
//          _box.x = Math.random()*stage.stageWidth ;
//          _box.y = Math.random()*stage.stageHeight;
//          stage.addChild(_box);
//          
//      }
//  }
//}

这实际上是在我尝试上述内容之前课堂上的内容,但我宁愿将所有代码保留在课堂上。

有什么建议吗?

【问题讨论】:

  • 为了确定,您想创建一个类,该类将在舞台上随机添加您的Box 库项目的新实例。所以你只需要打电话给new Box() 就可以了,对吧?

标签: flash actionscript-3 actionscript flash-cs4


【解决方案1】:

这是一种解决方法: 当您为 MovieClip 设置类名(导出为动作脚本类)时,您可以选择为剪辑指定一个基类。你可以像这样在这个基类中添加你的随机位置代码:

public class BoxBase extends MovieClip
{
    public function BoxBase()
    {
        super();
        addEventListener(Event.ADDED_TO_STAGE, _onStaged);
    }

    public function _onStaged(event:Event):void
    {
        this.x = Math.random()*stage.stageWidth;
        this.y = Math.random()*stage.stageHeight;
    }
}

注意 ADDED_TO_STAGE 事件监听器。现在代码在您的 MC 中,您必须等到它被添加到显示列表(放置在舞台上或作为舞台上剪辑的子级)之后,才能引用“舞台”变量。

将 BoxBase 设置为 Box 的基类后,您可以通过在文档类中放置以下代码,在随机位置创建 Box 的新实例:

var b:Box = new Box();
addChild(b);

【讨论】:

    【解决方案2】:

    在这里,您的情况与上一个问题相反。您在 createBox() 函数外部声明了一个 Box 变量,因此在函数内部重新声明该变量不会创建更多的盒子。

      //It should be something like this, without the
      // need to declare the _box variable outside the
      // function.
      // As dugulous points out you have to make sure that 
      // stage is not null before calling this
      function createbox (event:Event):void
      {  
         if( stage != null )
         {
           var _box = new Box;
           _box.x = Math.random()*stage.stageWidth ;
           _box.y = Math.random()*stage.stageHeight;
           addChild(_box);
         }
      }
    

    在您的示例中,您使用 EnterFrame 事件添加框...您需要多少?计时器可以让您更好地控制盒子的数量,而且更容易停止。

      var delay:int = 500; // in milliseconds
      var numBoxes:int = 2000; 
    
      // you could add an optional limit
      var timer:Timer = new Timer( delay , numBoxes );
      // var timer:Timer = new Timer( delay ); would work too
    
      timer.addEventListener( TimerEvent.TIMER , createBox );
      timer.start();
    
      //Change createBox() accordingly...
      function createbox (event:TimerEvent):void
      {  
          var _box = new Box;
         _box.x = Math.random()*stage.stageWidth ;
         _box.y = Math.random()*stage.stageHeight;
         addChild(_box);
      }
    

    每当您对添加框感到厌烦时,您只需调用它

     timer.stop();
    

    【讨论】:

      【解决方案3】:
      package
      {   
        import flash.events.Event;
        import flash.display.MovieClip;
      
        public class Main extends MovieClip
        {
          private var _box:Box;
      
          public function Main()
          {
              addEventListener(Event.ENTER_FRAME, createbox);         
          }
      
          private function createbox (event:Event):void
          {
               _box= new Box();
              _box.x = Math.random()*stage.stageWidth ;
              _box.y = Math.random()*stage.stageHeight;
              addChild(_box);
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-21
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 1970-01-01
        • 2014-04-20
        • 1970-01-01
        • 2019-06-18
        • 2014-04-16
        相关资源
        最近更新 更多