Reference MovieClip After it is Added to Stage as a Child

此生再无相见时 提交于 2019-12-11 07:28:43

问题


I am currently having problems referencing a MovieClip child which I add to the Stage from the Document Class. Basically when the MovieClip child is added to the Stage from the Document Class, I want a certain MovieClip already on the Stage to reference it once it is on the Stage.

Also, if it is possible, I don't want the MovieClip referencing the child being added to the Stage to have parameters linking it with the Document Class, because I plan on nesting this MovieClip within another MovieClip later on in the future.

Here is the code for the MovieClip class which is referencing the child once it is added to the Stage:

package com.gameEngine.assetHolders
{
    import com.gameEngine.documentClass.*;
    import com.gameEngine.assetHolders.*;
    import com.gameEngine.assetHolders.Levels.*;
    import flash.display.*;
    import flash.events.*;

    public class FallingPlatform extends MovieClip
    {
        public var _document:Document;
        // Trying to reference "_player"
        public var _player:Player;
        public var fallState:Boolean;
        public var platformYSpeed:Number = 0;
        public var platformGravityPower:Number = 0.75;

        public function FallingPlatform()
        {
            this.addEventListener(Event.ADDED_TO_STAGE, initFallingPlatform);
            // constructor code
        }
        public function initFallingPlatform(event:Event)
        {
            this.addEventListener(Event.ENTER_FRAME, dynamicFall);
            this.addEventListener(Event.ENTER_FRAME, hitTest);
        }
        public function dynamicFall(event:Event)
        {
            if (this.fallState)
            {
                this.platformYSpeed += this.platformGravityPower;
                y += this.platformYSpeed;
            }
        }
        // Trying to reference "_player"
        public function hitTest(event:Event)
        {
            if (this.hitTestPoint(_player.x, _player.y + 1, true))
            {
                this.fallState = true;
            }
        }
    }
}

回答1:


The player is initialized in the Document class, right? So for me, the best option is either passing the player reference in the constructor of your FallingPlatform class like this

 public function FallingPlatform (thePlayer:Player) {
      this._player = thePlayer
 }

or having a setter method to pass it to it. In this way, you're not tying the structure of your code

 public function set player (thePlayer:Player):void {
       this._player = thePlayer
 }

Hope it helps!




回答2:


If you set a document class for a fla file every movieclip on the stage can be accessed by it's instance name - just as you wold create a var with its name. Event more, you can do something like that:

If you place two movieclips on the stagefor example mc1 and mc2 you can add them as variables to the document class.

package{
    public class DocClass{

    public var mc1:MovieClip;
    public var mc2:MovieClip;

    [...]

    }
}

and than you can access those movieclips from your class with code hints form your IDE (flash or flashbuilder)

the opposite is also availible: define variables in your class and than access them in flash

! it works best when your document class extends a Sprite, I haven;t tested it on extending from a MovieClip but it should also work



来源:https://stackoverflow.com/questions/8233462/reference-movieclip-after-it-is-added-to-stage-as-a-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!