0

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

我有我的 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;
    }
}
}

当它全部在一个大文件中时,我只需要在类中设置变量并通过每个函数访问它,但现在我有点困惑。任何人都知道我怎么能做到这一点?

4

2 回答 2

1

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

主文件

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

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

于 2013-05-05T00:52:55.163 回答
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;
}
于 2013-05-05T00:50:47.870 回答