0

以下のコードでは、スプライト (メモホルダー) 内に 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

動的に作成する表示オブジェクトへの参照を保持する必要があります。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追加するため、クラスがスプライトを拡張するのはなぜだろうか。displayそれが正しいと仮定すると、ノーツを汎用オブジェクトにするだけです。つまり、別のクラスを拡張する必要はないようです。
于 2013-09-10T00:10:58.743 に答える