【问题标题】:How to convert a Shape to MovieClip using ActionScript 3.0 only如何仅使用 ActionScript 3.0 将形状转换为 MovieClip
【发布时间】:2011-07-11 06:41:28
【问题描述】:
import flash.display.Shape;
import flash.display.Graphics;


stage.addEventListener(Event.ENTER_FRAME, startAnim);

function startAnim(e:Event):void
{
    var shape1:Shape = new Shape();
    shape1.graphics.beginFill(0x333333,1);
    shape1.graphics.drawRect(40,50,250,125);
    shape1.graphics.endFill();

    addChild(shape1); // this will add a shape of rectangle to stage

}

这是一个在舞台上创建矩形的非常简单的函数。好的,但问题是我如何才能将此 SHAPE 转换为 MOVIECLIP 仅使用 ActionScript,以便我可以将事件添加到相同的 (shape1)。

【问题讨论】:

    标签: actionscript-3 actionscript


    【解决方案1】:

    嗯,通过使用 MovieClip 而不是 Shape。 MovieClip 也有一个 Graphics 对象。

    import flash.display.MovieClip ; 
    //import flash.display.Graphics;//not needed
    
    //stage.addEventListener(Event.ENTER_FRAME, startAnim); //remove enterframe
    
    //function startAnim(e:Event):void { //no need for a handler
        var shape1:MovieClip = new MovieClip();
        shape1.graphics.beginFill(0x333333,1); 
        shape1.graphics.drawRect(40,50,250,125);     
        shape1.graphics.endFill();
    
        addChild(shape1); // this will add a MovieClip of rectangle to stage
    
        shape1.addEventListener(MouseEvent.MOUSE_DOWN, dragShape);
    
        function dragShape(E:MouseEvent)
        {
            shape1.startDrag()
        }
    
    shape1.addEventListener(MouseEvent.MOUSE_UP, dropShape);
    
        function dropShape(E:MouseEvent)
        {
            shape1.stopDrag()
        }
    //} no need for that either :)
    

    请注意,因此,您的函数在 ENTER_FRAME = 每秒 25 次或更多次上被调用,因此您将创建剪辑并将其添加到每秒 25 次或更多次 + 引用是在函数中本地创建的,因此一旦创建对象,您将无法从外部访问“shape1”。

    【讨论】:

    • 谢谢...但是我想拖放它,是否可以向这个 shape1 影片剪辑添加事件。
    • 现在我添加了一些脚本到这个 i:e shape_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragShape);函数 dragShape(E:MouseEvent) { shape_mc.startDrag() } shape_mc.addEventListener(MouseEvent.MOUSE_UP, dropShape);函数 dropShape(E:MouseEvent) { shape_mc.stopDrag() }
    • 但在此之后,它正在复制用于拖动的影片剪辑,并且原始形状仍位于同一位置(40、50)。是的,我可以将重复的形状拖动到任何我不知道发生了什么事情的地方
    • hmmm 需要您的代码来帮助:用您目前所拥有的内容更新您的主题 + 记得使用代码标签(文本编辑工具)使其更具可读性 :)
    • 这就是我向您解释的内容:您在 ENTER_FRAME 上调用该函数,从而每秒创建 25+ shape_mc。如果你删除这一行:stage.add[...] + 你的函数:function startAnim(e:Event):void { 和最后一个括号, } 你的代码将被执行一次,一切都会好起来的 :)
    【解决方案2】:

    我认为您不能将 Shape 转换为 MovieClip。您可以做的是创建一个MovieClip 类,并在构造函数中生成Shape 对象,并将其添加到MovieClip 中。

    public class Car extends MovieClip {
        private var shape1:Shape = new Shape();
        public function Car() {
            shape1.graphics.beginFill(0x333333,1);
            shape1.graphics.drawRect(40,50,250,125);
            shape1.graphics.endFill();
            addChild(shape1); // this will add a shape of rectangle to stage
        }
    }
    

    形状也有事件。

    • 激活
    • 已添加
    • 添加到舞台
    • 停用
    • 进入框架
    • 已移除
    • 从舞台中移除
    • 渲染

    但由于它不是从 InteractiveObject 扩展而来,因此您无法处理输入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-27
      • 2011-01-23
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多