1

Flash ファイルの DocumentClass から変数「予算」をクラスに渡そうとしています。

現在私は持っています:

( DocumentClassv5 、これはプロパティ パネルのフラッシュ ファイルに添付されたコードです。一部のコードは省略されています)

package  
{

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.*;
import flash.ui.Keyboard;
import flash.display.Stage;
import flash.text.TextFieldType;
import flash.media.Sound;
import flash.media.SoundChannel;    
import miniGameOne;
import floorTileMC;


import flash.display.Loader;
import flash.net.URLRequest;



public class DocumentClassv5 extends MovieClip 
{
    /*#################################
    ## Defining Variables            ##
    #################################*/

    public var budget:int = 0;

    var gameOne:miniGameOne = new miniGameOne();
        /*#################################
    ## Constructor                   ##
    #################################*/     
    public function DocumentClassv5() 
    {
        /*#################################
        ## Adding Event Listeners        ##
        #################################*/


        trace("Document class loaded");
    }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /*###################################################
    ## Begins the mini game                            ##
    ###################################################*/           
    public function begin(evt: MouseEvent)
    {
        beginGame.visible = false;
        beginGame.removeEventListener(MouseEvent.CLICK, begin);
        budget = 500;

        cleanListeners();
        gameOne.loadGame();
        trace(gameOne.testVar);
        trace(floorTile.testVar2);

        /*#################################
        ## Adding Event Listeners        ##
        #################################*/         


        trace("Game started");
    }

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 
}

それから、今のところ何もしない miniGameOne クラス ファイルがあります。

tileFloorMC という別のクラス ファイルもあります。これはシンボルに添付されます。

package  
{

import flash.display.MovieClip;
import flash.events.MouseEvent;
import DocumentClassv5;


public class floorTileMC extends MovieClip 
{
    var propertyA:Number;
    //var hackerClass:DocumentClassv5 = new DocumentClassv5;
            public var testVar2:int = 50;

    public function floorTileMC() 
    {
        this.propertyA = randomRange(100, 500);

        this.addEventListener(MouseEvent.ROLL_OVER, manageMouseOver, false, 0, true);
        this.addEventListener(MouseEvent.ROLL_OUT, manageMouseOut, false, 0, true);
    }
    private function manageMouseOver(evt: MouseEvent)
    {
        this.gotoAndStop(2);
        //trace(mainClass.budget);
    }
    private function manageMouseOut(evt: MouseEvent)
    {
        this.gotoAndStop(1);
        //mainClass.budget += 1;
    }
    private function randomRange(minNum:Number, maxNum:Number):Number   
    {  
        return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);  
    }       

}

}

ここで、基本的に、予算を DocumentClassv5 から floorTileMC に渡し、次に DocumentClassv5 に戻すことができる必要があります。現時点では、floorTileMC からは何でも、miniGameOne からは何でも DocumentClassv5 に渡すことができますが、floorTileMC から DocumentClassv5 に渡そうとすると、Error #2136: The SWF file file:///yadayada/GameV5.swf contains invalid というエラーが発生します。データ。

より具体的には、//var hackerClass:DocumentClassv5 = new DocumentClassv5; のコメントを外すとすぐに、

どんな助けでも大歓迎です!

ありがとう、

ティファニー

4

1 に答える 1

1

サブクラスでドキュメント クラスをインスタンス化しようとしています:

var hackerClass:DocumentClassv5 = new DocumentClassv5();

新しいインスタンスを作成するのではなく、既存のインスタンスにアクセスするためのアクセスが必要です。

できることの 1 つは、ドキュメント クラスへの静的参照を作成することです。(以下のコードサンプルを参照)

ただし、静的参照は醜いものになる可能性があり、インスタンス化するときに、doc クラスの参照を他のクラスに渡したいだけかもしれません。

以下の両方の方法:

ドキュメント クラスで:

//instead of the line below:
var gameOne:miniGameOne = new miniGameOne(); //It's a bad idea to instantiate non primitive objects before the constructor of your document class runs.

//just declare it, and instantiate it in the constructor
var gameOne:miniGameOne;

//if you want to use a static reference:
public static var me:DocumentClassv5;

public function DocumentClassv5() 
{
    /*#################################
    ## Adding Event Listeners        ##
    #################################*/

    //if using the static var me, set it's value to this (the instance of the document class):
    me = this;


    trace("Document class loaded");
    gameOne = new miniGameOne(this); //pass a reference to the document class if NOT using the static var me


}

static var me を使用する場合は、任意のクラスから次のようにしてアクセスします。

DocumentClassV5.me.budget;

別のよりクリーンな代替手段 (アクセスする必要がある値が実際にはどのクラスにも直接関連付けられていない場合、たとえばグローバル設定) は、設定を保持するために静的な (インスタンス化されない) まったく新しいクラスを作成することです。

package {
    public class Global {

        public static var budget:Number = 50;

    }
}

次に、グローバルクラスをインポートして実行することで予算にアクセスしますGlobal.budget = 5

于 2012-09-26T23:25:08.267 に答える