AS3 Form Inquiry - actionscript-3

I am relatively new to AS3. I am having a few code issues at the moment i am hoping for some advice on this forum.
Basically, i am creating a form for my website. I have one combobox and I want The user enter their email address and password into the fields.
Then click on the submit button to sign in and to validate what they entered is correct. When i run my code in real-time i get an error
1120: Access of undefined property status_Txt. I dont know what status_Txt means.
I copied a large section of this code from a tutorial i read. My code is below: Can someone please tell me how i can resolve this problem.
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.MouseEvent;
import fl.data.DataProvider;
import flash.events.Event;
//Building the variables
var phpVars:URLVariables = new URLVariables();
//Requesting the php file
var phpFileRequest:URLRequest = new URLRequest("http://www.example.com");
phpFileRequest.method = URLRequestMethod.POST;
phpFileRequest.data = phpVars;
//Building the loader
var phpLoader:URLLoader = new URLLoader();
phpLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
phpLoader.addEventListener(Event.COMPLETE, showResult);
//Variables ready for processing
phpVars.email = txt_input.text;
phpVars.p_swd = pass.text;
phpLoader.load(phpFileRequest);
btn_one.addEventListener(MouseEvent.CLICK, btnHandler);
function btnHandler(event:MouseEvent):void{
trace("Login success");
}
//Validate form fileds
function showResult(event:Event):void{
status_Txt.text = "" + event.target.data.systemResult;
trace(event.target.data.systemResult);
}
var cbox:Array = new Array();
boy[0] = "Male";
girl[1] = "Female";
c_one.dataProvider = new DataProvider(cbox);
c_one.addEventListener(Event.CHANGE, dataHandler);
function dataHandler(e:Event):void{
trace(e.target.value);
}
}

You're attempting to access an object that doesn't exist. status_Txt is being referenced in:
function showResult(event:Event):void{
status_Txt.text = "" + event.target.data.systemResult; //Either remove this or add a textfield to the stage with the instance name status_Txt
trace(event.target.data.systemResult);
}

Related

Actionscript Firebase POSTing, Error #2032

I am trying to POST to my firebase account, but I can't seem to figure out why it's not letting me. I can switch the POST to a GET and trace out the data I'm trying to get, but when I POST I get en error.
Code:
package
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.net.navigateToURL;
import flash.net.URLLoaderDataFormat;
//import flash.system.Security;
/**
* ...
* #author M
*/
public class FireBaseConnector
{
public function FireBaseConnector()
{
//Security.loadPolicyFile("https://demo.firebaseio-demo.com/crossdomain.xml");
trace("trying to post");
var url:String = 'https://[myURL].firebaseio.com/userData.json';
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();
variables.name = "alan"
req.data = variables;
req.method = URLRequestMethod.POST;
var l:URLLoader = new URLLoader()
//l.dataFormat = URLLoaderDataFormat.TEXT;
l.addEventListener(IOErrorEvent.IO_ERROR, onError);
l.addEventListener(Event.COMPLETE, handleResults);
l.load(req);
}
public function onError(e:IOErrorEvent):void {
trace("Error! " + e.text);
}
public function handleResults(e:Event):void {
trace("response" + e.target.data as String);
}
}
}
My output is:
[Starting debug session with FDB]
running
trying to post
Error! Error #2032: Stream Error. URL: https://resplendent-fire-2464.firebaseio.com/userData/.json
closing time!
closing time!
If i switch to GET my output is:
[Starting debug session with FDB]
running
trying to post
response{"dave":{"id":"678","name":"dave"},"mike":{"name":"mike"},"miles":{"id":"456","name":"miles"}}
closing time!
closing time!
That tells me I have the url right, but I have some permissions problem or something when trying to post. Maybe I have the variables sending improperly? I've made sure my Flash debugger is on the accepted app list in my Firewall.
I'm running Windows 10 and FlashDevelop JRE 1.6 Flex 4.6 Air 18
To send a POST request to the Firebase REST API you need to send the data JSON encoded:
var urlVars:Object = new Object();
urlVars.name = nameTxt.text;
urlVars.lastname = lastNameTxt.text;
var request:URLRequest = new URLRequest(FIREBASEURL);
request.data = JSON.stringify(urlVars);
request.method = URLRequestMethod.POST;
var firebaseLoader:URLLoader = new URLLoader();
firebaseLoader.addEventListener(flash.events.Event.COMPLETE, function():void
{
trace(firebaseLoader.data);
});
firebaseLoader.load(request);
I try this code can work
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var urlVars:Object = new Object();
urlVars.name = "111111";
var url:URLLoader=new URLLoader();
var hdr:URLRequestHeader = new URLRequestHeader("Content-type", "application/json");
var req:URLRequest = new URLRequest( "https://[myURL].firebaseio.com/userData.json" );
req.method = URLRequestMethod.POST;
req.data = JSON.stringify(urlVars);
url.load(req);
url.addEventListener(Event.COMPLETE,urlCompelte)
function urlCompelte(e:Event){
trace(e.target.data);//{"name":"-Ka6PGZc_hY-sfJJqeVS"}
}

Incorrect calling of external mp3 file

I am getting Error:#2044, so I assumed that my code is wrong in calling the Sound functions but I can't seem to find where I am making the error.
package {
import flash.display.MovieClip;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.net.URLRequest;
public class Tile extends MovieClip{
public function GetAndSwitchKey():String {
//Some Code//
//Create sounds
//------------> Start here
var bSound:Sound = new Sound();
var bReq:URLRequest = new URLRequest("B.mp3");
var oSound:Sound = new Sound();
var oReq:URLRequest = new URLRequest("O.mp3");
var mSound:Sound = new Sound();
var mReq:URLRequest = new URLRequest("M.mp3");
//Load sounds
bSound.load(bReq);
oSound.load(oReq);
mSound.load(mReq);
//<-------- End here
//Some code//
switch (temp) {
case "B": bSound.play(); break;
case "O": oSound.play(); break;
case "M": mSound.play(); break;
}
//Some code//
}
}
}
The way I added the files is by placing them inside the same file as the Action script 3 file. I also then changed them in the propperties to be exportable for action script. But as far as I am aware I don't have to specify the directory since a copy is made in the SWF.
Replacing the code indicated between the "start here" and "end here". This will resolve the error. And will call the sound file properly.
var mySoundHolder = new mySound();

Locale.loadLanguageXML() Won't work in external script

I have a script for multi-language-use working when added to the first frame of a timeline. However, when I try to adapt it to work in an external .as script, it throws a TypeError and I can't figure out why.
Here is the code that works in a timeline:
import fl.data.DataProvider;
import fl.text.TLFTextField;
import flash.text.Font;
import flashx.textLayout.elements.*;
import flashx.textLayout.formats.*;
//------------------
// CREATE TLFTEXTFIELD:
var field_001:TLFTextField = new TLFTextField();
field_001.x = 20;
field_001.y = 50;
field_001.width = 342
field_001.height = 54;
field_001.background = true;
addChild(field_001);
// Create text format
var format:TextLayoutFormat = new TextLayoutFormat();
format.fontFamily = "Arial";
format.fontSize = 36;
format.color = 0x666666;
// Apply the format
var textFlow:TextFlow = field_001.textFlow;
textFlow.hostFormat = format;
//------------------
// SETUP LOCALE OBJECT:
var languages:Object = new Object(); // Stores flags for loaded languages
var localeDefault:String = "ar"; // Default language
var locale:String = "ar"; // Current language selected in combobox
// Event handler for Locale object
function localeLoadedHandler(success:Boolean):void
{
if( success )
{
// Mark language as loaded and show localized string
languages[locale] = true;
field_001.text = Locale.loadStringEx("IDS_FIRSTFIELD", locale);
// field_002 is a field already on stage
field_002.text = Locale.loadStringEx("IDS_SECONDFIELD", locale);
}
}
// Load the default language...
Locale.setDefaultLang(localeDefault);
Locale.setLoadCallback(localeLoadedHandler);
trace("Locale.getDefaultLang() is: " + Locale.getDefaultLang());
Locale.loadLanguageXML(Locale.getDefaultLang());
Here is my adaptation to an external script and set up as the class for a standalone swf called "tempchild.swf" I want to open inside a parent swf at a later time:
package com.marsinc {
import fl.text.TLFTextField;
import flash.text.Font;
import flashx.textLayout.elements.*;
import flashx.textLayout.formats.*;
import flash.display.MovieClip;
import fl.lang.Locale;
public class tempchild extends MovieClip {
//------------------
// SETUP LOCALE OBJECT:
private var languages:Object; // Stores flags for loaded languages
private var localeDefault:String; // Default language
private var locale:String; // Current language selected in combobox
private var field_001:TLFTextField;
private var format:TextLayoutFormat;
public function tempchild()
{
// constructor code
languages = new Object();
localeDefault = "es";
locale = "es";
//------------------
// CREATE TLFTEXTFIELD:
field_001 = new TLFTextField();
field_001.x = 20;
field_001.y = 50;
field_001.width = 342
field_001.height = 54;
field_001.background = true;
addChild(field_001);
// Create text format
format = new TextLayoutFormat();
format.fontFamily = "Arial";
format.fontSize = 36;
format.color = 0x666666;
// Apply the format
var textFlow:TextFlow = field_001.textFlow;
textFlow.hostFormat = format;
// Load the default language...
Locale.setDefaultLang(localeDefault);
Locale.setLoadCallback(localeLoadedHandler);
trace("Locale.getDefaultLang() is: " + Locale.getDefaultLang()); // displays "es"
Locale.loadLanguageXML(Locale.getDefaultLang()); // this line returns an error
}
// Event handler for Locale object
private function localeLoadedHandler(success:Boolean):void
{
trace("running the loaded handler");
if( success )
{
// Mark language as loaded and show localized string
languages[locale] = true;
field_001.text = Locale.loadStringEx("IDS_FIRSTFIELD", locale);
//field_002.text = Locale.loadStringEx("IDS_SECONDFIELD", locale);
}
}
}
And this is the error in the output window:
TypeError: Error #1010: A term is undefined and has no properties.
at fl.lang::Locale$/loadXML()
at fl.lang::Locale$/loadLanguageXML()
at com.marsinc::tempchild()
Been digging around for an answer for a couple days now and I am stuck. Any help is greatly appreciated. Thanks!
--Kevin
You can make it .as file in one of (at least) two ways
1) copy paste all exactly as it is to .as file and do:
include "[path]filename.as"
2) change the code to be a class
- make field_001, format, textFlow, languages, localeDefault, locale as public vars
- insert all code in a function named "init"
- add the "localeLoadedHandler" as a function
- click on your stage and change in the properties panel the stage's class to the new class
Good Luck!!

Flash as3 Turkish Character encding

I have developed a Facebook application using Flash as3. In this application when ever the user finished the test, a certificate URL is posted on user wall. the url is encode with Turkish character user first name and user last name.In certificate swf file it reads the url and display the user name on the jpg. This work great with English users but the problem arrives when the user name is in Turkish. I am attaching the jpg of the main problem, you can clearly see the url is looking good, but when i copy past the url i get the same result as display on the picture.
My Certificate Swf code is
package {
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.external.ExternalInterface;
import com.adobe.images.JPGEncoder;
public class main extends MovieClip {
public function main() {
valuePairs = new Array();
nextValuePare = new Array();
var search:String = ExternalInterface.call("window.location.href.toString");
//var vars:URLVariables = new URLVariables(search);
valuePairs = search.substring(search.indexOf("?")+1).split("&");
trace(valuePairs);
var map:Object = new Object();
for (var i:int = 0; i<valuePairs.length; i++)
{
nextValuePare = valuePairs[i].split("=");
trace(nextValuePare);
map[nextValuePare[0]] = nextValuePare[1];
}
picture.fbookName.text = String(map["fName"]) +" "+ String(map["lName"]);
}
private var _urlLoader:URLLoader;
private var _urlVariable:URLVariables;
private var valuePairs:Array;
private var nextValuePare:Array
}
}
You should URL encode your URL with the top level function encodeURI(). Then in the swf that extracts the names from the URL use decodeURI().

Flash AS3 FileReference - Select and Upload Multiple Files one at a time

I currently have an swf that allows you to select and display a file and upload it to the server using FileReference. This works great but i need to be able to select and display multiple (up to 25 in some cases) and then upload them all at the end.
I know you can use the FileReferenceList to select multiple files at the same time in the dialog popup but my issue is that the user needs to select one at a time, do stuff to that image, then select another, do stuff and so on... then at the end when they press upload it upload them all onto the server.
Is it possible or is there a way so i can maybe add every new selected file into an array, then at the end it uploads all the files in the array either in one go or loops through each until all objects in the array are complete?
Can anyone help? I'll post the full code for my working single file upload below.
Please, please help, i'm limited with flash and have only been learning for 3-4 months and i've been stuck for over a week now :(
Lauren
as3 Code:
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import fl.controls.ProgressBarMode;
import flash.display.Bitmap;
import flash.display.BitmapData;
progressBar.visible=false;
UploadprogressBar.visible=false;
// Create FileReference.
var imageFile:FileReference;
// Create Loader to hold image content
var image_loader:Loader = new Loader();
var image_content:Sprite = new Sprite();
// Get Extension Function.
var imageExtension;
function getExtension($url:String):String {
var extension:String = $url.substring($url.lastIndexOf(".")+1, $url.length);
return extension;
}
// Random Number Function to create new filename on server.
function randomNum(low:Number=0, high:Number=1):Number {
return Math.floor(Math.random() * (1+high-low)) + low;
}
var myNumber:Number= randomNum(1000,9999);
var myString:String= String(myNumber);
var RandomNumbers = myString;
var imageFilePath = (RandomNumbers);
// Select Button Function.
select_btn.addEventListener(MouseEvent.CLICK, onselect_btnClicked);
function onselect_btnClicked(event:MouseEvent):void {
imageFile=new FileReference();
imageFile.addEventListener(Event.SELECT, onFileSelected);
var imageTypeFilter:FileFilter = new FileFilter("JPG/PNG Files","*.jpeg; *.jpg;*.gif;*.png");
imageFile.browse([imageTypeFilter]);
}
// File Selected Function.
function onFileSelected(event:Event):void {
imageFile.addEventListener(Event.COMPLETE, onFileLoaded);
imageFile.addEventListener(ProgressEvent.PROGRESS, onProgress);
var imageFileName = imageFile.name;
imageExtension = getExtension(imageFileName);
imageFile.load();
progressBar.visible=true;
progressBar.mode=ProgressBarMode.MANUAL;
progressBar.minimum=0;
progressBar.maximum=100;
UploadprogressBar.mode=ProgressBarMode.MANUAL;
UploadprogressBar.minimum=0;
UploadprogressBar.maximum=100;
}
// File Progress Function.
function onProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
progressBar.setProgress(percentLoaded, 100);
}
// File Loaded Function.
function onFileLoaded(event:Event):void {
var fileReference:FileReference=event.target as FileReference;
var data:ByteArray=fileReference["data"];
var movieClipLoader:Loader=new Loader();
movieClipLoader.loadBytes(data);
movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieClipLoaderComplete);
imageFile.removeEventListener(Event.COMPLETE, onFileLoaded);
imageFile.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
// Load Image onto Stage Function.
function onMovieClipLoaderComplete(event:Event):void {
var loadedContent:DisplayObject=event.target.content;
image_loader =event.target.loader as Loader;
var scaleWidth:Number=345/image_loader.width;
image_loader.scaleX=image_loader.scaleY=scaleWidth;
image_loader.height=200;
image_loader.scaleX=image_loader.scaleY;
image_loader.x=10;
image_loader.y=10;
image_content.buttonMode=true;
image_content.addChild(image_loader);
addChild(image_content);
}
// Upload Button Function.
upload_btn.addEventListener(MouseEvent.CLICK, onupload_btnClicked);
function onupload_btnClicked(event:MouseEvent):void {
var filename:String=imageFile.name;
var urlRequest:URLRequest = new URLRequest("http://www.xxxxx.com/xxxxx/file-reference.php");
var variables:URLVariables = new URLVariables();
urlRequest.method = URLRequestMethod.POST;
imageFile.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
imageFile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
variables.ID = (RandomNumbers);
urlRequest.data = variables;
imageFile.upload(urlRequest);
}
// File Upload Progress Function.
function onUploadProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
UploadprogressBar.setProgress(percentLoaded, 100);
trace("loaded: "+percentLoaded+"%");
upload_status_txt.text='upload in progress...';
}
// Upload File Completed Function.
function onUploadComplete(event:Event):void {
upload_status_txt.text='upload complete';
imageFile.removeEventListener(ProgressEvent.PROGRESS, onProgress);
imageFile.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
UploadprogressBar.visible=false;
}
php Server Code:
<?php
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$fileID = $_POST['ID'];
$filename = basename( $_FILES['Filedata']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
$uploadID = $fileID.'.'.$extension;
if (move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$uploadID))
{
echo "OK";
}
else
{
echo "ERROR";
}
?>
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html
"When you call the FileReferenceList.browse() method, which creates an array of FileReference objects."
You need to use FileReferenceList instead of FileReference.