【发布时间】: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