简体   繁体   English

从阶段访问文档类

[英]Accessing document class from stage

I could not find a specific solution to my problem. 我找不到解决我问题的具体解决方案。 I start my document class "Main.as". 我开始我的文档类“ Main.as”。 From that I play Frame 1, which is my GUI. 从那开始,我播放第1帧,这就是我的GUI。 After I press the "start" Button on my GUI, I want it to call the "Start()" function in my Main.as. 按下GUI上的“开始”按钮后,我希望它在Main.as中调用“ Start()”函数。

But it just won't work. 但这是行不通的。 I found out that I cannot create Objects from my document class, also I can't have more than 1 document class. 我发现无法从我的文档类创建对象,也不能超过一个文档类。 So what can I do? 那我该怎么办?

Main.as 维护

import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Keyboard;



public class Main extends MovieClip 
{
trace("Im the Main Class");
public var BG:MovieClip;
public var char:Char_Control;
public var enemy:Enemy;
var blockSlide:Boolean = true;
var coin_array:Array = new Array();
var char_array:Array = new Array();
var coin:item_collider;
var posx;
var posy;
var oCollider;
var main:Main = new Main();


    //Konstruktor
    public function Main() 
    {
    trace("Im the Main Class(Constructor)");
    this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
    this.stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
    this.addEventListener(Event.ENTER_FRAME, Gravity);
    this.addEventListener(Event.ENTER_FRAME,update);
    gotoAndPlay(1); // Starts GUI
    Start(); // Starts Everything else => The Game itself
    }



public function update(_event:Event)
{
    //Execute Base_Class
    //char.Move();

    //Execute Base_Class+Sub_Class
    enemy.Chase(char,enemy);
    //enemy.Move(true, "walk");
}




public function Start()
{
    // Adds Character to Stage
     char = new Char_Control(875,350);
     stage.addChild(char);

     enemy = new Enemy(500,450);
     stage.addChild(enemy);

     char_array[1]=char;
     char_array[0]=enemy;

    // Adds Objects to Stage
    for (var i2:int=1; i2<30; i2++)
     {
     posx = Math.round(Math.random()*(1400)+1);
     posy = Math.round(Math.random()*(800)+1);
     coin = new item_collider(posx,posy);// Öffnet Konstruktor
     stage.addChild(coin);
     trace("Add Coin (foreach)");
     coin_array.push(coin);
     }
}

stage: 阶段:

button.addEventListener(MouseEvent.CLICK,buttonClick);

var tform:TextFormat = new TextFormat();
tform.size = 20;
tform.font = "Arial";
tform.align = TextFormatAlign.CENTER;
tform.color = 0x000000;
tform.bold = true;

var tfield:TextField = new TextField();
tfield.width = checkbox.width;
tfield.width = button.width;
tfield.width = coin_number.width;
tfield.width = stepper_label.width;
tfield.x = 0;
tfield.y = 30;

var coin_amount:int = 0;
var create_enemy:Boolean = false;

tfield.setTextFormat(tform);
addChild(tfield);

checkbox.setStyle("textFormat",tform);
button.setStyle("textFormat",tform);
coin_number.setStyle("textFormat",tform);
stepper_label.setStyle("textFormat",tform);





stop();
function buttonClick(e:MouseEvent) 
        { 
        coin_amount = coin_number.value;
        create_enemy = checkbox.selected;
        nextFrame(); //Next Frame => the Level itself
    }

After downloading your code, I notice that there are a few compatibility issues with the way you've set up your timeline code and the code intended to be the Document Class. 下载代码后,我注意到设置时间轴代码的方式和打算用作文档类的代码存在一些兼容性问题。

Although you have "Automatically declare stage instances" unchecked in your AS3.0 settings, you appear to have a mix of implicitly-declared and explicitly-declared instances on the stage. 尽管在AS3.0设置中未选中 “自动声明阶段实例”,但似乎在舞台上混合了隐式声明的实例和显式声明的实例。 To resolve this, I would suggest leaving this property unchecked and declaring all stage instances in your Document Class (this includes anything you have on the stage in frame 1 as well: coin_number , checkbox , etc). 要解决此问题,我建议不要选中此属性,并在Document Class中声明所有舞台实例(这还包括第1帧中舞台上的所有内容: coin_numbercheckbox等)。

Doing this will also require you to import any classes you are using (ie NumericStepper , TextField , TextFormat , etc). 这样做还需要您导入正在使用的任何类(即NumericStepperTextFieldTextFormat等)。

Once you have done this, you'll want to move the game control event listeners (the ones you have in your Document Class constructor). 完成此操作后,您将需要移动游戏控制事件监听器(在Document Class构造函数中拥有的监听器)。 You can place these either in a separate method or add them to the end of your Start method. 您可以将它们放在单独的方法中,也可以将它们添加到Start方法的末尾。 If you leave them in the constructor, you'll be attempting to access something that does yet exist because your update method that is called on ENTER_FRAME relies on objects that get instantiated in the Start method. 如果将它们留在构造函数中,则将尝试访问尚不存在的内容,因为在ENTER_FRAME上调用的update方法依赖于在Start方法中实例化的对象。

The next step is to not call Start from your constructor. 下一步是不要从构造函数中调用Start Instead, place a call to Start on your second frame. 而是在第二个框架上拨打“ Start ”电话。

This should get your application to a runnable state using a mix of timeline code and a Document Class. 这应该使用时间轴代码和文档类的组合使您的应用程序进入可运行状态。

I would like to know why this is a requirement for a good grade as this is highly unorthodox and--for the most part--looked down upon in the ActionScript community. 我想知道为什么这是对好成绩的要求,因为这是高度非正统的,而且在大多数情况下,ActionScript社区对此视而不见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM