0

在下面的代码中,我在一个精灵(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);
4

2 回答 2

0

您可以使用 getBounds 关于 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 没有影响

于 2013-09-09T02:05:33.337 回答
0

您需要保存对您动态创建的显示对象的引用——在 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 设为通用对象,即似乎不需要扩展另一个类。
于 2013-09-10T00:10:58.743 回答