A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: AS3 scripts is not working

  1. #1
    Junior Member
    Join Date
    Apr 2015
    Posts
    8

    AS3 scripts is not working

    Hey guys! So I have a simple php + AS3 thingie here. Could you please take a look really quickly?

    AS3 code:
    Code:
    stop();
    
    import flash.net.URLVariables;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.Event;
    
    
    male_icon.addEventListener(MouseEvent.CLICK, maleChosen);
    female_icon.addEventListener(MouseEvent.CLICK, femaleChosen);
    cc_finish_btn.addEventListener(MouseEvent.CLICK, finishClicked);
    
    
    
    var PlayerGender:String = "";
    
    username_txt.text = MovieClip(this).userNameVar;
    
    
    function maleChosen(MouseEvent:Event):void {
    		MovieClip(this).PlayerGender = "Male";
    		gender_error_txt.text = "Male is chosen!";
    }
    
    function femaleChosen(MouseEvent:Event):void {
    		MovieClip(this).PlayerGender = "Female";
    		gender_error_txt.text = "Female is chosen!";
    }
    
    function finishClicked(MouseEvent:Event):void {
    		if(MovieClip(this).PlayerGender != "")
    		{
    			
    			var requestVars:URLVariables = new URLVariables();
    
    			//Something to send
    			var req:URLRequest = new URLRequest("scripts/create_character.php");
    			req.method = URLRequestMethod.POST;
    			req.data = requestVars;
    
    			//Something that sends
    			var urlLoader:URLLoader = new URLLoader;
    			urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    			urlLoader.addEventListener(Event.COMPLETE, onCompleteCC);
    			requestVars.player_gender = MovieClip(this).PlayerGender;
    			requestVars.player_path = MovieClip(this).PlayerPath;
    			requestVars.cc_request = "create_cc";
    
    			//Send data over to php file
    			urlLoader.load(req);
    			
    			
    		}
    		else
    		{
    			gender_error_txt.text = "Please pick your gender!";
    		}
    }
    
    var result_final:String = this.CC_result;
    
    //The handler itself on completion
    function onCompleteCC(event:Event):void{
    
    	var CC_result = event.target.data.cc_result;
    	
    	
    	switch(CC_result){
    				
    				case "couldnt_create":
    					gotoAndStop("cc_couldnt_create");
    				break;
    				case "pGpP_notset":
    					gotoAndStop("cc_pGpP_notset");
    				break;
    				case "not_exist":
    					gotoAndStop("cc_not_exist");
    				break;
    				case "char_created":
    					gotoAndPlay(15);
    				break;
    				default:
    					gotoAndPlay(5);
    				break;
    	}
    }

    create_character.php:
    PHP Code:
    <?php
    session_start
    ();
    include_once 
    './connect.php';

    if(
    $_POST['cc_request'] != null && $_POST['cc_request'] == "create_cc"
    {
    if(isset(
    $_POST['player_gender']) && isset($_POST['player_path']))
    {
        
    $PlayerGender $_POST['player_gender'];
        
    $PlayerGender mysqli_real_escape_string($link$PlayerGender);
        
    $PlayerPath $_POST['player_path'];
        
    $PlayerPath mysqli_real_escape_string($link$PlayerPath);
        
    $PlayerName $_SESSION['username'];
        
    $PlayerPassword $_SESSION['password'];
        
        
    $cc_query_sql "UPDATE players SET gender = '$PlayerGender', path = '$PlayerPath' WHERE username = '$PlayerName' AND password = '$PlayerPassword' LIMIT 1";
            
        
    $this_query mysqli_query($link$cc_query_sql) or die(mysqli_error($link));
        
        if(
    $this_query == false)
        {
            
    //$cc_result = "couldnt_create";
            
    echo "cc_request=couldnt_create";
            die();
        } else {
        
            
    //$cc_result = "char_created";
            
    echo "cc_request=charcreated";
        }
        
    } else
    {
        
    //$cc_result = "pGpP_notset";
        
    echo "cc_request=pGpP_notset";
        die();
    }
        
    } else
    {
        
    //$cc_result = "not_exist";
        
    echo "cc_request=not_exist";
        die();
    }

    ?>

  2. #2
    Junior Member
    Join Date
    Apr 2015
    Posts
    8
    The problem is that upon receiving the "charcreated" response, the variable CC_result in flash is undefined. It is actually undefined upon receiving any response. The php script is fine, it works. It sends cc_result=charcreated I checked it with firebug.
    Last edited by Micard; 07-25-2015 at 01:53 PM.

  3. #3
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Swap

    urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    for
    urlLoader.dataFormat = URLLoaderDataFormat.TEXT;

    and swap

    var CC_result = event.target.data.cc_result;
    for
    var CC_result = event.target.data;

  4. #4
    Junior Member
    Join Date
    Apr 2015
    Posts
    8
    Oh you're right. It would be type TEXT here, I forgot I was doing something else. So I tried to play around with sending both a variable and text - nope. CC_result is still undefined.

  5. #5
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    Well it worked with my set up, however I had to change the set up as I don't have your files to see it and where you are getting your datas from.

  6. #6
    Junior Member
    Join Date
    Apr 2015
    Posts
    8
    This is my setup. I also have the following code on frame 1, layer AS3 SESSION (maybe that kind of interacts with the current code, no idea)

    Screenshot:
    flash_scr.png

    Code:
    stop();
    import flash.net.URLVariables;
    import flash.net.URLRequest;
    import flash.net.URLLoader;
    import flash.events.Event;
    
    var variables:URLVariables = new URLVariables();
    
    var userNameVar:String;
    
    //Something to send
    var varSend:URLRequest = new URLRequest("scripts/getsessionvars.php");
    varSend.method = URLRequestMethod.POST;
    varSend.data = variables;
    
    //Something that sends
    var varLoader:URLLoader = new URLLoader;
    varLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
    varLoader.addEventListener(Event.COMPLETE, completeHandler);
    variables.myRequest = "bringit";
    
    //Send data over to php file
    varLoader.load(varSend);
    
    //The handler itself on completion
    function completeHandler(event:Event):void{
    	
    	
    	var idVar = event.target.data.id_var;
    	MovieClip(this).userNameVar = event.target.data.uname_var;
    	var passVar = event.target.data.upass_var;
    	var resultStatus = event.target.data.my_result;
    	
    	if(resultStatus == "no_session"){
    			gotoAndStop("no_session");
    		} else if (resultStatus == "no_exist"){
    				gotoAndStop("no_exist");
    			} else if (resultStatus == "all_good"){
    					userid_txt.text = idVar;
    					username_txt.text = userNameVar;
    					password_txt.text = passVar;
    					gotoAndPlay(10);
    				}
    	}

  7. #7
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    That doesn't really help ( well it doesn't help me anyway ), need to attach your files, or a slimmed down version.

    Are you not getting errors
    Last edited by fruitbeard; 07-26-2015 at 12:36 PM.

  8. #8
    Junior Member
    Join Date
    Apr 2015
    Posts
    8
    Nope, no errors. Just that the variable is undefined.

    Here's the .fla file
    Code:
    http://www.filehosting.org/file/details/500134/gor.fla
    And thanks for looking it into for me! Much appreciate it!

  9. #9
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    You should sort the order of your clip TheGameMC so that it goes to frames after the the send and load data, no going back on yourself.
    It needs to be urlLoader.dataFormat = URLLoaderDataFormat.TEXT; and var CC_result = event.target.data; as stated before hand.

    You have some discrepencies with your data in the flash and php files.

    PHP Code:
    case "char_created" :
        
    gotoAndPlay(15);
        break; 
    should be charcreated.

    it would help to have seen you php files too, as again I had to guess as to how you created your data.

  10. #10
    Junior Member
    Join Date
    Apr 2015
    Posts
    8
    Thanks but as I said I was playing around with these, changing both my php and as3 scripts. And yes, I did change it to what you said and it still would send me to frame 6 and say that the variable is undefined. However, the first script works just fine. Here's the getsessionvars.php file:

    PHP Code:
    <?php
    session_start
    ();
    include_once 
    './connect.php';

    if(
    $_POST['myRequest'] != null && $_POST['myRequest'] == "bringit")
       {
           if(!isset(
    $_SESSION['username']))
           {
               echo 
    "my_result=no_session&id_var=false&uname_var=false&upass_var=false";
               die();
           }else
           {
               
    $id $_SESSION['id'];
               
    $username $_SESSION['username'];
               
    $password $_SESSION['password'];
           }
           
           
    $sql mysqli_query($link"SELECT * FROM players WHERE username = '$username' AND password = '$password' LIMIT 1");
           
    $num_rows mysqli_num_rows($sql);
           
           if(
    $num_rows == 0)
           {
               echo 
    "my_result=no_exist&id_var=false&uname_var=false&upass_var=false";
               die();
           } else
           {
               echo 
    "my_result=all_good&id_var=$id&uname_var=$username&upass_var=$password";
           }
           
       }
    ?>
    So this php script + AS3 SESSION script work just fine. I don't see the difference and why wouldn't the other one work as well.
    Last edited by Micard; 07-26-2015 at 03:41 PM.

  11. #11

  12. #12
    Junior Member
    Join Date
    Apr 2015
    Posts
    8
    Hey, thanks for your response! I haven't been able to check the thing out yet - busy week, but will do it as soon as I can! Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center