使用来自另一个类as3的变量

时间:2013-05-05 00:08:16

标签: actionscript-3 flash class variables

我将一个大文件分成多个文件以清理它,现在我遇到了问题。

我有我的main.as,character.as,camera.as。

我要做的是从另一个类中访问一个变量,我稍后在该类中设置该变量。我会告诉你我的意思。

从我的main.as我加载每个类并将它们添加为子级,以便它显示在屏幕上。

    public function buildGame()
    {
        var loadMap:Sprite = new nf_MapBuilder();
        var xChar:Sprite = new nf_Character();
        var xCam:Sprite = new nf_Camera();
        var UserControl:nf_UserControl = new nf_UserControl();

        addChild(loadMap);
        addChild(xChar);
        addChild(xCam);
        addChild(UserControl);
    }

一切都在屏幕上显示,就像它需要的那样。然后它转到我的character.as:

package as3
{
import flash.display.Sprite;
import flash.events.Event;

public class nf_Character extends Sprite
{
    public var character_pos:Array = new Array();
    public var character_is_moving:Boolean = false;
    public var character_x_dir:int = 0;
    public var character_y_dir:int = 0;
    public var character:hero = new hero();

    public function nf_Character()
    {
        addEventListener(Event.ADDED_TO_STAGE,xCharLoad);
    }

    public function xCharLoad(e:Event)
    {
        character_pos = [2,2];
        character.x=64*(character_pos[1]);
        character.y=64*(character_pos[0]);
        addChild(character);
    }
}
}

有问题。我需要使用我在character.as中设置的变量来在我的camera.as中使用它:

package as3
{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.display.StageScaleMode;

import as3.nf_Character;

public class nf_Camera extends Sprite
{
    private var xChar:nf_Character = new nf_Character();

    //Camera variables
    var stageW2:Number;
    var stageH2:Number;
    var view:Rectangle;

    public function nf_Camera()
    {
        addEventListener(Event.ADDED_TO_STAGE,xCamGo);
    }

    public function xCamGo(e:Event):void
    {
        trace("Camera pos - " + xChar.x + " " + xChar.character.y);

        view = new Rectangle(0,0,stage.stageWidth,stage.stageHeight)
        stageW2 = stage.stageWidth / 2 - 32;
        stageH2 = stage.stageHeight / 2 - 32;

        addEventListener(Event.ENTER_FRAME,CamView);
    }
    public function CamView(e:Event):void
    {
        view.x = xChar.character.x - stageW2;
        view.y = xChar.character.y - stageH2;
        scrollRect = view;
    }
}
}

当它全部在一个大文件中时,我只需要在类中设置变量并通过每个函数接受它,但现在我有点困惑。谁知道我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

简而言之,我认为你应该订阅你的主要角色中你角色的一个事件,这个事件会在角色移动时被触发。在该事件的处理程序中,您可以在摄像机上调用一个方法,根据角色的当前位置设置它的位置。

<强> main.as

private var xChar:Sprite = new nf_Character();
private var xCam:Sprite = new nf_Camera();

public function buildGame()
{
    var loadMap:Sprite = new nf_MapBuilder();
    var UserControl:nf_UserControl = new nf_UserControl();

    // listen for when the character has moved
    xChar.addEventListener(MoveEvent.MOVED, characterMovedHandler);

    addChild(loadMap);
    addChild(xChar);
    addChild(xCam);
    addChild(UserControl);
}

private function characterMovedHandler(event:MoveEvent):void
{
    xCam.setPosition(xChar.x, xChar.y);
}

<强> nf_Character.as

public class nf_Character extends Sprite
{
    public var character_pos:Array = new Array();
    public var character_is_moving:Boolean = false;
    public var character_x_dir:int = 0;
    public var character_y_dir:int = 0;
    public var character:hero = new hero();

    public function nf_Character()
    {
        addEventListener(Event.ADDED_TO_STAGE,xCharLoad);
    }

    public function xCharLoad(e:Event)
    {
        character_pos = [2,2];
        character.x=64*(character_pos[1]);
        character.y=64*(character_pos[0]);
        addChild(character);
    }

    public function xCharMoved() 
    {
        // Dispatch a custom event when the character moves
        dispatchEvent(new MovedEvent(MovedEvent.MOVED)); 
    }
}

<强> nf_Camera.as

public class nf_Camera extends Sprite
{
    private var xChar:nf_Character = new nf_Character();

    //Camera variables
    var stageW2:Number;
    var stageH2:Number;
    var view:Rectangle;

    public function nf_Camera()
    {
        addEventListener(Event.ADDED_TO_STAGE,xCamGo);
    }

    public function xCamGo(e:Event):void
    {
        trace("Camera pos - " + xChar.x + " " + xChar.character.y);

        view = new Rectangle(0,0,stage.stageWidth,stage.stageHeight)
        stageW2 = stage.stageWidth / 2 - 32;
        stageH2 = stage.stageHeight / 2 - 32;

        // Probably only need one enterframe either in your character class or main
        //addEventListener(Event.ENTER_FRAME,CamView);
    }
    public function setPosition(x:Number, y:Number):void
    {
        view.x = xChar.character.x - stageW2;
        view.y = xChar.character.y - stageH2;
        scrollRect = view;
    }
}

出于兴趣,你是如何移动角色的?

答案 1 :(得分:0)

您可以将角色类实例作为构造函数的参数传递给相机类实例。然后,您将引用相机类中的字符,并可以访问它的变量

// Inside buildGame() in main.
var xChar:nf_Character = new nf_Character();
var xCam:nf_Camera = new nf_Camera(xChar);

// Inside nf_Camera
public function nf_Camera(char:nf_Character) {
    xChar = char;
}