2016-04-10 9 views
0

Ich habe diesen Code für ein Login-Tutorial, dass ich folgte.Flash AS3 Stack-Überlauf

package { 

    /* 
    always extend a class using movieclip instead of sprite when using flash. 
    */ 

    import flash.display.MovieClip; 
    import flash.events.*; 
    import flash.net.*; 
    import flash.text.*; 


    /* 
    create our class 
    */ 



public class login extends MovieClip { 

; 


public function login():void { 

      var login_form:login = new login(); 
      addChild(login_form); 
      var username:TextField = new TextField(); 
      addChild(username); 
      var password:TextField = new TextField(); 
      addChild(password); 
      var login_button:MovieClip = new MovieClip(); 
      addChild(login_button); 

      /* 
      buttonMode gives the submit button a rollover 
      */ 

      login_button.buttonMode = true; 

      /* 
      what this says is that when our button is pressed, the checkLogin function will run 
      */ 

      login_button.addEventListener(MouseEvent.MOUSE_DOWN, checkLogin); 

      /* 
      set the initial textfield values 
      */ 

      username.text = ""; 
      password.text = ""; 

     } 

public function checkLogin (e:MouseEvent):void { 

      var username:TextField = new TextField(); 
      addChild(username); 
      var password:TextField = new TextField(); 
      addChild(password); 

    /* 
    check fields before sending request to php 
    */ 

    if (username.text == "" || password.text == "") { 

     /* 
     if username or password fields are empty set error messages 
     */ 

     if (username.text == "") { 

     username.text = "Enter your username"; 

     } 

     if (password.text == "") { 

     password.text = "Enter your password"; 

     } 

    } else { 

     /* 
     init function to process login 
     */ 

     processLogin(); 

    } 

} 

public function processLogin():void { 

      var username:TextField = new TextField(); 
      addChild(username); 
      var password:TextField = new TextField(); 
      addChild(password); 

    /* 
    variables that we send to the php file 
    */ 

    var phpVars:URLVariables = new URLVariables(); 

    /* 
    we create a URLRequest variable. This gets the php file path. 
    */ 

    var phpFileRequest:URLRequest = new URLRequest("php/controlpanel.php"); 

    /* 
    this allows us to use the post function in php 
    */ 

    phpFileRequest.method = URLRequestMethod.POST; 

    /* 
    attach the php variables to the URLRequest 
    */ 

    phpFileRequest.data = phpVars; 

    /* 
    create a new loader to load and send our urlrequest 
    */ 

    var phpLoader:URLLoader = new URLLoader(); 
    phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES; 
    phpLoader.addEventListener(Event.COMPLETE, showResult); 

    /* 
    now lets create the variables to send to the php file 
    */ 

    phpVars.systemCall = "checkLogin"; 
    phpVars.username = username.text; 
    phpVars.password = password.text; 

    /* 
    this will start the communication between flash and php 
    */ 

    phpLoader.load(phpFileRequest); 

} 

    public function showResult (event:Event):void { 


        var result_text:TextField = new TextField(); 
      addChild(result_text); 


    /* 

    this autosizes the text field 

    ***** You will need to import flash's text classes. You can do this by adding: 

    import flash.text.*; 

    ...to your list of import statements 

    */ 

    result_text.autoSize = TextFieldAutoSize.LEFT; 

    /* 
    this gets the output and displays it in the result text field 
    */ 

    result_text.text = "" + event.target.data.systemResult; 

} 

} 
} 

Ich bin mir nicht sicher, wo genau diese Instanzen alle für meine Taste MovieClip- und Textfeld zu setzen, aber wenn ich mein Programm lief, habe ich diesen Fehler.

Error: Error #1023: Stack overflow occurred. 
    at flash.display::DisplayObject() 
    at flash.display::InteractiveObject() 
    at flash.text::TextField() 
    at flash.display::Sprite/constructChildren() 
    at flash.display::Sprite() 
    at flash.display::MovieClip() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 
    at login() 

Ich habe versucht, Antworten zu suchen, die ich verstehen konnte, aber ohne Erfolg. Jede Hilfe wäre willkommen, danke.

Antwort

0

Sie scheinen eine Instanz der login-Klasse zu erstellen und fügen sie zu sich selbst hinzu, was die unendliche Rekursion im Login-Konstruktor verursacht.

var login_form:login = new login();

Sie sollten die Anmelde Klasse von einem anderen Objekt initialisieren, und fügen Sie es nur einmal auf die Bühne.

2

Sie haben die Stack overflow Fehler, weil Sie eine Endlosschleife von Instanziierung Ihrer login-Klasse var login_form:login = new login(); in seinem Konstruktor erstellen, so dass jede Instanz dieser Klasse wird eine andere Instanz erstellen.

Sie sollten Ihre Klasse nur dann instanziieren, wenn/wo Sie sie einfügen möchten, z. B. in die Hauptzeitleiste oder die Dokumentklasse, ... oder einfach direkt in die Bühne.

Bevor Sie jedoch die Klasse login instanziieren, sollten Sie sie korrekt erstellen. Dafür können Sie Ihre Objekte (Buttons, Movieclips, Textfelder, ...) manuell in Ihrer IDE, indem sie auf die Bühne Ihres MovieClip- fallen, oder mithilfe von Code beispielsweise hinzufügen:

var username:TextField = new TextField(); 
addChild(username); 

var password:TextField = new TextField(); 
addChild(password); 

// LoginButton here is the AS linkage of a MovieClip/Button in the library 
var login_button:LoginButton = new LoginButton(); 
addChild(login_button); 

diese Operation in der Regel einmal getan, dann können Sie Ihre Objekte wie dies zum Beispiel verwenden:

var user_name:String = username.text; 

if(password.text != ''){ /* ... */ } 

login_button.x = 30; 

wie Sie Anfänger sind, können Sie Learning ActionScript 3 here starten.

Hoffe, dass kann helfen.