访问未定义的问题和其他错误(AS3)

时间:2015-07-18 01:44:58

标签: actionscript-3 actionscript

好吧所以我正在努力使我的脚本外部.as文件。 我把它们放在时间轴上并且工作正常但是当我把它们放在as文件中时它们根本不工作。 这是我现在的代码

所以我得到了外部工作的脚本非常感谢你:) 我只需要将stage添加到舞台上。我用

import Scripts.resources.main;

var Main:main = new main;

addChild(Main);

在我的外部脚本中,我现在将addchild添加到主函数中。

package Scripts.resources
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.net.URLLoader;
import flash.net.URLRequestMethod;
import flash.events.OutputProgressEvent;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.EventDispatcher;


public class main extends EventDispatcher{

    private var _stage:Stage;
    var url:String = "php/data_user.php";
    //declare our graphic vars
    var stmBar:resourceBar;
    var tenBar:resourceBar;
    var potBar:resourceBar;
    var spdBar:resourceBar;
    var crtBar:resourceBar;
    var awrBar:resourceBar;
    var resBar:resourceBar;
    var statUp:Sprite;

    //This Returns the number of milliseconds since midnight January 1, 1970
    //Tacking the time variable at the end of your php file locations will ensure their is no caching
    var now:Date = new Date();
    var time:Number = now.getTime();

    /*
    *  These should point to your PHP files. I recommend NOT using an http path because accessing
    *  the site via 'www.yoursite.com' versus 'yoursite.com' makes a difference and will cause undefined
    *  issues. Instead use a relative path like: var registerLocation:String = "membership/php/register.php";
    */

    var registerLocation:String = "php/register.php" + "?" + time;
    var loginLocation:String = "php/login.php" + "?" + time;
    var editProfileLocation:String = "php/editprofile.php" + "?" + time;
    var forgotPWLocation:String = "php/forgotpw.php" + "?" + time;
    var statsLocation:String = "php/user_stats.php" + "?" + time;
    var statsGylph:String = "php/data_user.php" + "?" + time;

    //userInfo is the Object that gets populated that stores all of the users information.
    var userInfo:Object = new Object();

    //The minimum length of a password.
    var passwordLength:Number = 4;

    /*
    *  var cookie refers to swfObjects addVariable. If you look at membership.php, where swf
    *  object is defined, addVariable shows a var called usercookie.
    *  This help decide if the program should remember the users username or not.
    */
    //cookieUserName would be the username that gets put into the user field if the cookie has been set

public function main(stage:Stage)
{
        _stage = stage;
        var _loader:URLLoader = new URLLoader();
        var _request:URLRequest = new URLRequest(url + "?id=" + userInfo.id);
        _request.method = URLRequestMethod.GET;
        _loader.addEventListener(Event.COMPLETE, onLoadData);
        _loader.load(_request);
        _stage.addChild(stmBar);
        stmBar.x = -50;
        stmBar.y = 200;
    }
     public function MyClass(){
        this.addEventListener(Event.ADDED_TO_STAGE, addedToStage);


    }

    private function addedToStage(e:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, addedToStage);

        //this code will run after you've added this object to the stage
        //it is the equivalent of when timeline code runs
    }
        function onLoadData(e:Event):void {
            var str:String =  e.target.data;

            var array:Array =str.split("-");
            for(var i:int;i < array.length; i++)
               output(i+1,array[i]);
        }

     public function output(field:Number,i:int):void 
        {
            if (field == 5)
            {
            stmBar = new resourceBar(0, 0, i, "Stamina", 0x990000);
            stmBar.x = -200;
            stmBar.y = 50;
            }
            else if (field == 6)
            {
            tenBar = new resourceBar(0, 0, i, "Tenacity", 0xFF9900);
            tenBar.x = 200;
            tenBar.y = 50;
            }
            else if (field == 7)
            {
            potBar = new resourceBar(0, 0, i, "Potency", 0xCC3399);
            potBar.x = -200;
            potBar.y = 100;
            }
            else if (field == 8)
            {
            spdBar = new resourceBar(0, 0, i, "Speed", 0x00CC00);
            spdBar.x = 200;
            spdBar.y = 100;
            }
            else if (field == 9)
            {
            crtBar = new resourceBar(0, 0, i, "Crit", 0x009999);
            crtBar.x = -200;
            crtBar.y = 150;
            }
            else if (field == 10)
            {
            awrBar = new resourceBar(0, 0, i, "Awareness", 0x3399CC);
            awrBar.x = 200;
            awrBar.y = 150;
            }
            else if (field == 11)
            {
            resBar = new resourceBar(0, 0, i, "Resistance", 0x999999);
            resBar.x = -200;
            resBar.y = 200;

            addEventListener(Event.ENTER_FRAME, onLoop);
            }
        function onLoop(e:Event):void
        {       
            stmBar.change(1);
            tenBar.change(1);
            potBar.change(1);
            spdBar.change(1);
            crtBar.change(1);
            awrBar.change(1);
            resBar.change(1);
        }
    }
}

}

我的问题是当我添加main的主要功能时,它不会在主函数中添加子项。我可以在里面加一个有孩子的孩子吗?

1 个答案:

答案 0 :(得分:0)

在类文件中,所有代码都需要包装在函数中。所以你在哪里:

        _loader = new URLLoader();
        _request = new URLRequest(url + "?id=" + (STAGE.parent as MovieClip).userInfo.id);
        _request.method = URLRequestMethod.GET;
        _loader.addEventListener(Event.COMPLETE, onLoadData);
        _loader.load(_request);

您需要将其放在函数中才能使代码有效。如果您希望立即运行该位代码,则可以将其放在构造函数中(即使用new关键字实例化时运行的类中的函数,例如new main()。如果{ {1}}是您的文档类,然后构造函数是在应用程序中运行的第一个代码。

构造函数是main函数,其具有public的确切名称。

Class

需要记住的是,在显示对象的构造函数中, public function main(){ _loader = new URLLoader(); _request = new URLRequest(url + "?id=" + (STAGE.parent as MovieClip).userInfo.id); _request.method = URLRequestMethod.GET; _loader.addEventListener(Event.COMPLETE, onLoadData); _loader.load(_request); } 关键字将没有任何值,因为一旦将显示对象添加到显示中,就会填充这些值。因此,使用这种方法很常见:

stage/parent/root

此外,现在您正在编写类文件的提示 - 遵循使类名以大写字母开头的惯例,类的实例以小写字母开头。