after loading a flash movie the form stops working - actionscript-3

I'm a total begginer in flash and action script, I do have some knowledge in other programming languages.
By trying to help a friend I added a contact form in his company website, that was made by someone else.
his site is full flash with a index frame that loads several pages.
After I built the new contact.swf and tested locally and also hosted by accessing it straight from the browser and embeded I tried to replace it with the old contact.swf that had no contact form.
During the tests, everything was fine but when included in the site the form is not functioning.
The form has a few rules that prevent empty fields with a green text warning. when I access it embeded separatelly in a web page it works, when I access it loaded by index.swf it does nothinh even if I press the submit button.
So I assume the problem is in the index.swf action script.
the button that loads the contact page has this code:
on (rollOver) {
gotoAndPlay(2);
}
on (rollOut) {
gotoAndPlay(11);
}
on (release) {
_root.main.secondary.loadMovie("contact.swf");
_root.main.logo.gotoAndPlay("s1");
}
I assume that is either the way the movie is loaded either there is something like a mask in the index movie that blocks the contact form.
If required I can submit also the fla of the index or the contact.
thanks
LE:
I tried to add to my contact.swf _lotroot=true; and i get compilation erros.
the action script for contact.swf is this:
stop();
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
// ----------------------------------------------------------------
var variables:URLVariables = new URLVariables();
// Be sure to change this URL to the PHP parse file on your site server
var varSend:URLRequest = new URLRequest("http://www.mydomain.tld/form.php");
var varLoader:URLLoader = new URLLoader;
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
status_txt.text = "";
submit_btn.addEventListener(MouseEvent.CLICK, ValidateAndSend);
function ValidateAndSend(event:MouseEvent):void{
//validate form fields
if(!name_txt.length) {
status_txt.text = "Introduceti numele dvs.";
} else if(!companie_txt.length) {
status_txt.text = "Introduceti numele companiei.";
} else if(!telefon_txt.length) {
status_txt.text = "Introduceti numarul de telefon.";
} else if(!email_txt.length) {
status_txt.text = "Introduceti o adresa de email.";
} else if(!validateEmail(email_txt.text)) {
status_txt.text = "Introduceti o adresa de email corecta.";
} else if(!message_txt.length) {
status_txt.text = "Introduceti mesajul.";
} else {
status_txt.text = "Multumim " + name_txt.text + ", mesajul a fost trimis!";
variables.userName = name_txt.text;
variables.userEmail = email_txt.text;
variables.userMsg = message_txt.text;
variables.companie = companie_txt.text;
variables.telefon = telefon_txt.text;
varLoader.load(varSend);
}
}
function validateEmail(str:String):Boolean {
var pattern:RegExp = /(\w|[_.\-])+#((\w|-)+\.)+\w{2,4}+/;
var result:Object = pattern.exec(str);
if(result == null) {
return false;
}
return true;
}

The code you have provided for the original site is in AS2, but the code from your contact SWF is in AS3. You cannot load AS3 into AS2, so you will have to rewrite your contact form using AS2.

Related

Actionscript: SWF file does not record data to txt, while the compiler does

I am trying to create a psychological experiment in ActionScript. The performance of the participants is to be stored in a separate .csv file. I have written this code (apparently, instead of "This is the text to write" there is going to be a data array, but the problem appears with this code on equal parts)
import flash.net.FileReference;
import flash.events.Event;
var hint:String = "Please give a name for the file";
var labelling:String;
experiment_label.addEventListener(KeyboardEvent.KEY_UP, enter_pressed)
function enter_pressed (event:KeyboardEvent):void{
if (event.keyCode == 13) {
labelling = experiment_label.text;
addEventListener(Event.ENTER_FRAME,saveFile);
var ss:String = "this is text to write";
var fileRef:FileReference;
fileRef = new FileReference();
function saveFile(event:Event):void
{
fileRef.save(ss, labelling+".csv");
removeEventListener(Event.ENTER_FRAME, saveFile);
}
}
}
The problem I am facing is as follows: when I run it in from under Flash, it operates perfectly, and the save window does pop up. However, if I run an .swf file separately, it just ignores saving command.
Could you kindly suggest, what can I do? Or maybe I should use a different approach to saving altogether?
You current code will give you an Error #2176 (use a try ... catch to catch it) because FileReference.save()
is not called in response to a user action, such as a mouse event or keypress event.
To avoid that, you have to remove the Event.ENTER_FRAME event listener, even you don't need it :
function on_KeyUp(event:KeyboardEvent):void
{
if (event.keyCode == 13)
{
save_File();
}
}
function save_File():void
{
try
{
var fileRef:FileReference = new FileReference();
fileRef.save('some text here', 'my_file.csv');
}
catch (e:Error)
{
trace(e.toString());
}
}
Hope that can help.

Run an AS3 function only once

I'm making an AIR app and I would like to know if it's possible to run a function only once ?
If the user reboot his device and launch the app, the function won't be trigger again.
Thank you for your answers,
Since you're using adobe AIR, you have a few options.
Write a file that contains your save data.
Using AIR you can write a data file to the user's machine. This is more permanent than SharedObject's, which for various reasons can not work or persist long term.
You can actually serialize entire classes for easy retrieval of complex objects.
Here is an example of a class that managers a save file (for preferences or a game save etc):
package
{
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.utils.ByteArray;
import flash.utils.Dictionary;
import flash.net.registerClassAlias;
public class SaveData
{
//a list of any vars you want to save.
public var firstRun:Boolean = true;
public var someVar:int = 1;
public var anotherVar:Dictionary;
//the file for the user save data
public static function getFile():File {
return File.applicationStorageDirectory.resolvePath("saveFile.data");
}
public static function loadFile():SaveData {
registerClassAlias("flash.utils.Dictionary", Dictionary); //you need to register any non-native classes you want to save
registerClassAlias("saveData", SaveData);
var data:SaveData
//check for a default save file in the application directory (optional, but sometimes it's nice to bundle a default with your application before the user saves their own data
var f:File = new File("app:/saveFile.data");
if(f.exists){
data = loadFromFile(f);
}else {
trace("Default File Doesn't Exist");
}
//check the user storage directory for a save file, and see if it's newer than the one in the application directory
var userFile:File = getFile();
if (userFile.exists) {
if (f.exists && f.modificationDate.time < userFile.modificationDate.time) {
trace("Local File is newer, load it");
data = loadFromFile(userFile);
}else {
trace("Default File is newer");
}
}else {
trace("Save File DOESN'T EXIST YET");
}
if (!data) {
//data didn't load, let's create a new save file
data = new SaveData();
saveToFile(data);
}
saveData = data; //assing to main static accessible var
return data;
}
private static function loadFromFile(f:File):SaveData {
var d:SaveData;
try{
var stream:FileStream = new FileStream();
stream.open(f, FileMode.READ);
d = stream.readObject() as SaveData;
stream.close();
trace("DATA LOADED: " + f.nativePath);
return d;
}catch (err:Error) {
trace("Error Loading Save Data: " + f.nativePath);
}
return null;
}
public static function saveToFile(saveData:SaveData):void {
var fileStream:FileStream = new FileStream();
fileStream.open(getFile(), FileMode.WRITE);
fileStream.writeObject(saveData);
fileStream.close();
}
}
}
Then to use it:
var saveData:SaveData = SaveData.loadFile();
if(saveData.firstRun){
//do you first run code
//now set first run to false
saveData.firstRun = false;
SaveData.saveFile(saveData);
}
use a SharedObject. There is another answer covering this, but since no example was given I'll give one:
var mySO:SharedObject = SharedObject.getLocal("yourAppNameHere");
if(mySO.data.hasHadFirstRun){
//the cookie exists
}else{
//the cookie hasn't indicated there has been a first run yet, so do whatever you need to do
//some code
mySO.data.hasHadFirstRun = true; //now that you've done whatever on the first run, let's save the cookie
mySO.data.flush(); //save (though it will save automatically I believe when you close the application
}
use SQLite.
AIR can create and read SQLite databases, if your application has a need to store other data that would make sense to live in a table structure, it might make sense for you to add on a table for storing user preferences and things like a first run flag - if you wouldn't use the database for anything else though, it would definitely be over-kill.
Find out more about SQLite in AIR here:
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118666ade46-7d49.html

How do I use this URLRequest script?

I started learning ActionScript 3 a week ago and have stumbled across a huge learning curve. I found this script on the internet:
var _loader:URLLoader;
var _request:URLRequest;
function loadData():void {
_loader = new URLLoader();
_request = new URLRequest("http://www.travoid.com/game/Purchase.php?gid=1");
_request.method = URLRequestMethod.POST;
_loader.addEventListener(Event.COMPLETE, onLoadData);
_loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFailedToLoad);
_loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFailedToLoad);
_loader.load(_request);
}
function onLoadData(e:Event):void {
trace("onLoadData",e.target.data);
}
function onDataFailedToLoad(e:IOErrorEvent):void {
trace("onDataFailedToLoad:",e.text);
}
This all seems to work and is generating no errors or output, however my issue comes about when I use this next part of code (which I made)
function vpBuy(e:MouseEvent):void{
loadData();
if (e.target.data == "false") {
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
I get this error:
ReferenceError: Error #1069: Property data not found on
flash.display.SimpleButton and there is no default value. at
travoid_fla::MainTimeline/vpBuy() onLoadData
The part that is probably throwing this is:
if (e.target.data == "false") {
I was hoping e.target.data was what stored the value on the web page (which displays as false) but apparently not. With the code I found on the internet, what stores the information on the web page?
Thanks,
Ethan Webster.
The URLLoader load method is asynchronous, you have to wait the server response before triyng to get the result.
The functions onLoadData and onDataFailedToLoad are there to do that. When the response is well received the function onLoadData is called and you can get the data in e.target.data or _loader.data
The error in your function vpBuy is you try to access the data property on the object that triggered the MouseEvent (maybe a Button) and that object don't have such variable.
Try the following:
/** button clicked load the datas from the server **/
function vpBuy(e:MouseEvent):void
{
// load the datas from the server
loadData();
}
/** the datas are well loaded i can access them **/
function onLoadData(e:Event):void
{
trace("onLoadData",e.target.data);
if( e.target.data == "false" )
{
inf_a.visible = true;
inf_b.visible = true;
inf_c.visible = true;
inf_d.visible = true;
btn_ok.visible = true;
}
}
Hope this could help you :)

AS3: Print Job without Print Dialog Box/Print Setup Dialog

Is there a way to print a job without the print dialog box.It will look for the default printer then it will also print all pages.After I click the print button, inside of the movieclip will be print all. Then how can I do it? I don't need AS3 swf not Air if possible..
Here's the code I'm using..
print_btn.addEventListener(MouseEvent.CLICK,printContent);
function printContent(evt:MouseEvent) {
var printJob:PrintJob = new PrintJob();
if (printJob.start()) {
if (content_mc.width>printJob.pageWidth) {
content_mc.width=printJob.pageWidth;
content_mc.scaleY=content_mc.scaleX;
}
printJob.addPage(content_mc);
printJob.send();
}
}
NOTE:
I already used the start2() but this code is for AIR.
It appears there's an answer out there just did some googling:
http://forums.adobe.com/thread/854070
var pj:PrintJob = new PrintJob();
var started:Boolean = pj.start2(null, false);
if(started)
{
pj.addPage(this);
pj.send();
};
pj = null;

Flash hyperlink - changing var name in url

I have trouble with hyperlink in flash on site. As site is in CMS, with different
stages of approval I do not know exact url, so
function piClick(e:MouseEvent):void
{
var url:String = "/cms__Main?name=Target_name";
var request:URLRequest = new URLRequest(url);
try {
navigateToURL(request, '_self');
} catch (e:Error) {
trace("Error occurred!");
}
}
does not work, as cms_Main changes according to stage of site. What I probably need to do is:
grab url (or part after last "/" if that is even possible)
change "name" variable inside String
f.e
https://domain_name/.../status?name=Name_I_need_to_swap&sname=Country_name&..
stage.loaderInfo.url will get you the address of the swf itself so it won't get you the full address, the query or the hash tag. You can, however, grab all this information using Javascript.
import flash.external.ExternalInterface;
function getFullAddress():String {
if (ExternalInterface.available) {
var href:String = ExternalInterface.call('function(){return window.location.href}');
if (href) return href.toString();
}
throw new Error('Make sure JS is enabled.');
}
var fullAddr:String = getFullAddress();
EDIT: Here's how you can get and modify all that you're asking.
import flash.external.ExternalInterface;
import flash.net.URLVariables;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.navigateToURL;
function getFullAddress():String {
if (ExternalInterface.available) {
var href:String = ExternalInterface.call('function(){return window.location.href}');
if (href) return href.toString();
}
throw new Error('Make sure JS is enabled.');
}
function getQuery(fullAddr:String):URLVariables {
if (!fullAddr.match(/\?[^#]/)) return new URLVariables();
var q:String = fullAddr.replace(/^.*?\?([^#]+)(#.*)?$/,'$1');
return new URLVariables(q);
}
function getAddress(fullAddr:String):String {
return fullAddr.replace(/\?.*/,'').replace(/#.*/,'');
}
function getRequest(url:String,query:URLVariables=null,method:String='GET'):URLRequest {
method = URLRequestMethod[method] || 'GET';
var req:URLRequest = new URLRequest(url);
req.method = method;
if (method == 'GET' && query != null) {
req.url += '?' + query.toString().replace(/%5f/gi,'_').replace(/%2d/gi,'-');
// this is because dash and underscore chars are also
// encoded by URLVariables. we want to omit this.
return req;
}
req.data = query;
return req;
}
var fullAddr:String = getFullAddress();
var addr:String = getAddress(fullAddr);
var query:URLVariables = getQuery(fullAddr);
query.name = 'Name_I_need_to_swap';
query.sname = 'Country_name';
// add as many variable-value pairs as you like
// and don't worry, they will be automatically
// encoded by the function called below
var req:URLRequest = getRequest(addr,query);
//navigateToURL(req);
// check the console to see the changed address
ExternalInterface.call('console.log',req);
you can get URL with this.loaderInfo.url