参考影片剪辑后,将其添加到舞台上作为一个孩子 [英] Reference MovieClip After it is Added to Stage as a Child

查看:164
本文介绍了参考影片剪辑后,将其添加到舞台上作为一个孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有引用影片剪辑的孩子,我添加到舞台的问题文件类。基本上,当影片剪辑子将被添加到舞台文件类,我想一定影片剪辑已经在舞台上来引用它,一旦它在舞台

此外,如果有可能,我不希望影片剪辑引用的孩子已被添加到舞台有一个与文件类连接它的参数,因为我打算筑巢这个影片剪辑在另一个影片剪辑以后的未来。

下面是code为影片剪辑类,它被引用的孩子,一旦它被添加到舞台

 包com.gameEngine.assetHolders
{
    进口com.gameEngine.documentClass *。
    进口com.gameEngine.assetHolders *。
    进口com.gameEngine.assetHolders.Levels *。
    导入flash.display使用*。
    进口flash.events *。

    公共类FallingPlatform扩展影片剪辑
    {
        公共变种_document:文档;
        //尝试引用_player
        公共变种_player:玩家;
        公共变种fallState:布尔;
        公共变种platformYSpeed​​:数= 0;
        公共变种platformGravityPower:数= 0.75;

        公共职能FallingPlatform()
        {
            this.addEventListener(Event.ADDED_TO_STAGE,initFallingPlatform);
            //构造函数code
        }
        公共职能initFallingPlatform(事件:事件)
        {
            this.addEventListener(Event.ENTER_FRAME,dynamicFall);
            this.addEventListener(Event.ENTER_FRAME,则hitTest);
        }
        公共职能dynamicFall(事件:事件)
        {
            如果(this.fallState)
            {
                this.platformYSpeed​​ + = this.platformGravityPower;
                Y + = this.platformYSpeed​​;
            }
        }
        //尝试引用_player
        公共职能的hitTest(事件:事件)
        {
            如果(this.hitTestPoint(_player.x,_player.y + 1,真))
            {
                this.fallState = TRUE;
            }
        }
    }
}
 

解决方案

该播放器在文档类初始化,对不对?所以对我来说,最好的选择是要么通过玩家参考您的FallingPlatform类的构造函数像这样

 公共职能FallingPlatform(thePlayer:播放器){
      this._player = thePlayer
 }
 

或具有setter方法​​将它传递给它。通过这种方式,你不绑你的code结构

 公共功能集播放器(thePlayer:球员):无效{
       this._player = thePlayer
 }
 

希望它能帮助!

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;
            }
        }
    }
}

解决方案

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!

这篇关于参考影片剪辑后,将其添加到舞台上作为一个孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆