【问题标题】:Using an instance more than once when creating a MovieClip through composition通过合成创建影片剪辑时多次使用实例
【发布时间】:2016-04-18 19:49:18
【问题描述】:

进一步编辑:添加了第二个参数,以阐明 TreeGenerator 使用的是通过参数传递的 Sprite 中预先创建的部分,而不是生成它们。

编辑:我试图将代码更改为不再使用“形状”和“MovieClips”,因为这有点令人困惑和模糊我遇到的问题。

我正在尝试使用其他 Sprite 的那部分来创建 Sprite。我发布了一些代码来说明我正在尝试做的事情:

public class TreeGenerator extends Sprite 
{
    private var _leaf:Sprite
    private var _branch:Sprite
    private var _trunk:Sprite
    //these are separately drawn and instantiated in other sprites,
    //one of which will be passed through in the parameters

    public var thumbnail:Sprite

    public function TreeGenerator($preCreatedTreeOne:Sprite, $preCreatedTreeTwo:Sprite) 
    {
        _leaf = $preCreatedTreeOne.leaf;
        _branch = $preCreatedTreeTwo.branch;
        _trunk = $preCreatedTreeOne.trunk;

        thumbnail = $preCreatedTreeOne.leaf;
        //just uses the leaf for this example

        this.addchild(_leaf);
        _leaf.y = 30;
        this.addchild(_branch);
        _branch.y = 20;
        this.addchild(_trunk);
        _trunk.y = 10;
        //this "puts together" the tree image (though very
        //simply with just y for example purposes)

        this.addchild(thumbnail);
        thumbnail.y = 40;
        //this thumbnail is supposed to be a separate object that can also
        //be interacted with but this example neglects the event listeners.
        //the important thing here is the setting of the y to 40, which 
        //overwrites the y of 30 for _leaf.
    }
}

我正在通过已经实例化为 $preCreatedTreeOne 和 $preCreatedTreeTwo 的两个 Sprite,它们是通过各种可供选择的树部分(大绿叶、小红叶、细枝、粗枝等)创建的。这些精灵是绘制的图像,而不是在代码中生成的图像(比如来自 .swc 库)。当用户在舞台上单击两棵树后单击按钮时,TreeGenerator 将创建另一棵树的图像,包括叶子、树枝和树干,但这次使用的是两棵预先创建的树的部分(它将是动态,单击将生成一棵树,一棵树有绿叶,二棵树有粗树枝,一棵树有一根细树干,选择两棵树的组合为“一”或“二”)。还有一个单独的缩略图可以独立交互(尽管在示例中省略了该代码)。

但是,当我运行代码时,我看到 _leaf 的 y 坐标值被缩略图覆盖,因为我现在意识到两者现在都是 $preCreatedTreeOne.leaf。

如何从 $preCreatedTreeOne 中“获取”额外的 $preCreatedTreeOne.leaf 实例(或副本),以便在将它们存储在不同变量中时可以独立操作它们?

【问题讨论】:

  • 为什么要传入一个MovieClip作为参数?这些形状的集合是从哪里来的?你说用户选择它们:他是怎么做到的?
  • 您在任何地方都使用 MovieClip,但哪些是真正的 MovieClip?例如,NewShape 绝对不是一个,那么还有什么不是 MovieClip? (没有时间表)
  • 我猜你可以用“Sprite”代替。我不是 Actionscript 3(或一般编程)的高级作家。但是,即使使用精灵,您仍然会遇到同样的问题。更改缩略图的 x、y、scaleX 等也会影响 _circle(注意:“形状”并不重要,更一般地说,我正在尝试使用绘制的图像,然后将这些图像放在此类中作为一个图像,动态的,比如说每当用户点击一个按钮时)。我需要能够分别操作 _circle 和 thumbnail。

标签: actionscript-3


【解决方案1】:

IGraphicsData

您可以创建对应于对绘图 API 方法的调用的对象。它们都实现了上述接口。然后,您可以将它们分组到 Vector.<IGraphicsData> 中并将它们推入 drawGraphicsData() 以将您的东西绘制在 Graphics 对象中。

你不是在传递食物,而是传递它的食谱。然后,您可以根据食谱烹制任意数量的饭菜。

从 FP 11.6 开始,您还可以通过 readGraphicsData()Graphics 对象查询 Vector.<IGraphicsData>

免责声明:我在 wonder.fl 中修改了您的示例代码的修改版本,因为我没有安装编译器,请使用单独的类文件而不是内部类正确实现它。

我的 Flash Player 来自那个年代,当时它还很酷,因此有点旧 (11.2),这意味着我实际上无法使用 readGraphicsData() 测试代码,这就是我通过所有 3 个参数的三角形列表。

package 
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.IGraphicsData;
    import flash.display.GraphicsPath;
    import flash.display.GraphicsSolidFill;
    
    import flash.display.GraphicsEndFill;
    
    public class Main extends Sprite 
    {
        public function Main() 
        {
            
            // assemble Vector.<IGraphicsData> manually for triangle
            var triangle:GraphicsPath = new GraphicsPath();
            var triangleHeight:uint = 30; 
            triangle.moveTo(triangleHeight / 2, 0); 
            triangle.lineTo(triangleHeight, triangleHeight); 
            triangle.lineTo(0, triangleHeight); 
            triangle.lineTo(triangleHeight / 2, 0);
            
            var commands:Vector.<IGraphicsData> = new <IGraphicsData>[new GraphicsSolidFill(0xff0000), triangle, new GraphicsEndFill()];

            addChild(new ShapeSet(commands, commands, commands));
            
            // since FP 11.6 the following is also possible
            
            // assemble Vector.<IGraphicsData> from existing Graphics object in Shape
            var square:Shape = new Shape();
            square.graphics.beginFill(0xff00);
            square.graphics.drawRect(0, 0, 30, 30);
            square.graphics.endFill();
            
             addChild(new ShapeSet(square.graphics.readGraphicsData(), commands, commands));
        }
    }
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.IGraphicsData;

internal class ShapeSet extends Sprite
{
    private var _square:Shape;
    private var _triangle:Shape;    
    private var _thumbnail:Shape;

    public function ShapeSet(square:Vector.<IGraphicsData>, triangle:Vector.<IGraphicsData>,  thumbnail:Vector.<IGraphicsData>) 
    {
        _square = new Shape();
        _triangle = new Shape();
        _thumbnail = new Shape();

        addChild(_square);
        _square.x = 10;
        addChild(_triangle);
        _triangle.x = 60;
        addChild(_thumbnail);
        _thumbnail.x = 90;
        
        _square.graphics.drawGraphicsData(square);
        _triangle.graphics.drawGraphicsData(triangle);
        _thumbnail.graphics.drawGraphicsData(thumbnail);
    }
}

如果你只想将三角形​​传递给三角形,你应该编写一个三角形类,封装Vector.&lt;IGraphicsData&gt;,并确保它只包含表示三角形的数据。如何做到这一点超出了这个问题的范围。

【讨论】:

  • 我想使用“形状”的选择对我来说是一个糟糕的例子。我应该更清楚:我发布的代码被修改为使用假的“形状”电影剪辑,因为我只想专注于我遇到问题的概念。这个概念基本上是从一个作为参数传递的对象创建两个不同的实例。为了在我的示例中更清楚,您可以用树的碎片(叶子、树枝、树干等)替换形状,并且每个都有不同的 MovieClips(绿色大叶、小红叶等)。NewShape 将只需建一棵树。
  • TreeGenerator (NewShape) 只是我在用户执行需要将树添加到场景中的操作时动态创建不同树的一种方式。但是,我还想创建树的缩略图(也可以选择和单独修改,但这超出了这里的权限)。问题是,如果我要使用传递给树上的叶子和缩略图的 MovieClip 中的“叶子”对象(实际上是单独绘制的图像,而不是在代码中生成),它们将相互覆盖(x , y, scaleX 等)。
  • @CE.GOV:问题还是一样。你有没有费心尝试我的答案?请解释为什么它不能解决您的问题。
  • 我实际上正在旅行,但我当前的计算机没有安装我的 FlashDevelop。但是,根据我从您的代码中可以确定的内容(我承认,我不是高级程序员),ShapeSet(对应于 TreeGenerator)在 Main (当在参数中给出创建形状的命令时)。但是,对于我的问题,TreeGenerator 应该使用之前生成的 Sprite 中的部分(可能也早先添加到阶段),这就是它通过参数传递的原因。所以 TreeGenerator 不能使用“new”。
  • 所以在我编辑的版本中,首先,TreeOne、TreeTwo(可能是 TreeThree、Tree...)将使用 Sprites 创建(可能来自 .swc 库,例如 TreeOne 创建一个带有“new LargeGreenLeaf();",树枝和树干类似)。其次,当用户点击舞台上的两棵树,然后点击一个按钮时,这两个树精灵会被传入两个参数中。然后 TreeGenerator 将从 TreeOne 获取叶子和树干精灵,从 TreeTwo 获取分支精灵,然后生成一棵新树。但是,我遇到的问题是我不能同时使用叶子作为叶子和缩略图。
猜你喜欢
  • 1970-01-01
  • 2010-11-07
  • 1970-01-01
  • 2016-02-26
  • 1970-01-01
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 2017-06-24
相关资源
最近更新 更多