【问题标题】:Get global coordinate of a dynamically created nested component?获取动态创建的嵌套组件的全局坐标?
【发布时间】:2013-09-10 09:00:59
【问题描述】:

在下面的代码中,我在一个精灵(noteholder)中创建了 4 个精灵(picn)。如何获取我创建的 picn 实例的绝对值?我知道 localToGlobal 函数,但我不知道在这种情况下如何使用它。在这一点上,我认为我需要容器,因为我需要能够在创建后移动精灵。任何帮助是极大的赞赏。谢谢!

package 
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import Main;


public class Notes extends Sprite
{
    private var speed:int = 14;
    [Embed(source="../lib/Dodgethis.jpg")]
    private var picn:Class;
    private var noteholder:Sprite = new Sprite();


    public function appear() {
        trace ("appear ran")
        var arr1:Array = new Array;
        var numnotes:Number = 4;
        Main.StageRef.addChild(noteholder);
        trace (noteholder.x, noteholder.y);


        for (var i = 0; i < numnotes; i++)
            {
            //trace (i);
                var nbm:Bitmap = new picn;
                noteholder.addChild(nbm);
                nbm.y = i * 50;
                arr1.push(nbm);

【问题讨论】:

  • 谢谢,但我无法引用我想使用 localToGlobal 方法的点,因为它不是在运行时创建的,我无法通过名称引用它(至少我可以'由于变量范围,t弄清楚如何按名称引用它们)
  • 鉴于您上面的代码,您可以通过两种方式引用这些位图——通过arr1[0] 之类的数组或从精灵noteholder.getChildAt(0) — 您可能应该更喜欢arr1,并且您可能应该将其范围对象并将其重命名为noteSprites 或更清晰的名称。你真的不需要为动态创建的显示对象命名。

标签: actionscript-3 nested sprite global private


【解决方案1】:

您需要保存对您动态创建的显示对象的引用——在 Flash IDE 中创建可视资产时,您不需要一个“名称”,它只是实例变量的抽象。

我已经更新了你的代码来展示这个概念:

package 
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Stage; // added this
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle; // added this (see below)
import Main;

public class Notes extends Sprite
{
    private var speed:int = 14;
    [Embed(source="../lib/Dodgethis.jpg")]
    private var NoteBMP:Class;
    private var display:Sprite = new Sprite();
    private var notes:Array = []; // make notes an instance variable, so it's accessible throughout the class

    // I renamed appear to 'build' because I'm a pedantic moron
    public function build():void
    {
        trace ("build ran");
        var noteCount:int = 4;
        Main.StageRef.addChild(display);

        for (var i:int = 0; i < noteCount; i++)
        {
            var nbm:Bitmap = new NoteBMP;
            display.addChild(nbm);
            nbm.y = i * 50;
            notes.push(nbm); // by adding the display objects to an array you can reference them later

稍后在 Notes 类中,您可以通过循环遍历 notes 数组来引用您的位图

// initialize variables outside the loop
var localPos:Point 
var globalPos:Point;
var bounds:Rectangle;
var n:DisplayObject;
var stage:Stage = Main.StageRef;

for (var i:int = 0; i < notes.length; i++)
{
    trace( 'note' + i );
    n = notes[i] as DisplayObject; // cast during assignment since array items have ambiguous type

    // getBounds() returns a Rectangle representing the bounding box of the display object
    bounds = n.getBounds(stage);
    trace(
        'getBounds:',
        bounds.top, bounds.left, bounds.bottom, bounds.right
    );

    // localToGlobal() returns a Point which is just the position
    localPos = new Point( n.x, n.y );
    globalPos = display.localToGlobal(localPos)
    trace(
        'localToGlobal:',
        globalPos.x, globalPos.y
    );
}

几点:

  1. 我重构了一些名称以使意图更加清晰
  2. 我想知道为什么你的Notes 类扩展了Sprite,因为它在舞台上添加了display(你称之为“noteholder”)。假设这是正确的,只需将 Notes 设为通用对象,即似乎不需要扩展另一个类。

【讨论】:

    【解决方案2】:

    你可以使用getBoundsabout getBounds

    // call this after added to stage
    if (stage) {
         var r:Rectangle = nbm.getBounds(stage);// r's x and y is the global pos
    }
    

    我认为 localToGlobal 应该适用于您的情况。移动精灵对 localToGlobal 没有影响

    【讨论】:

    • 抱歉,这对我不起作用。如果我完全按照您的方式输入,则什么也没有发生。该脚本从未运行。当我取出 if (stage) 时,它运行了,但是 y 值都是 0,这对我一点帮助都没有。知道还有什么可以尝试的吗?
    • 查看我上面的评论并尝试noteSprites[0].getBounds(Main.StageRef)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2018-05-06
    • 2019-12-04
    • 1970-01-01
    • 2011-01-29
    • 2014-04-13
    • 2011-03-27
    相关资源
    最近更新 更多