execute function every x seconds in flash as3? - php

I'm using the following code to check for updates in mysql database using flash as3 (actionscript 3.0).
The code will check the database using PHP on my server and will display the details in Flash application.
The issue that I have is that I am trying to run this code every 8 seconds but when I run the code and I trace(urlRequest); I get 100's of traces in less than 20 seconds!
I thought giving a setInterval(checkDataBase, 8000); will sort this issue out and will run the code every 8 seconds but I have no luck with this!
Could someone please advise on this issue?
This is my code:
addEventListener(Event.ENTER_FRAME,checkForNewOrder);
function checkForNewOrder (e:Event):void
{
checkDataBase();
}
/*
function we use to send the form
*/
function checkDataBase ():void
{
/*
we use the URLVariables class to store our php variables
*/
var phpVars:URLVariables = new URLVariables();
phpVars.result_textL = result_textL.text;
/*
we use the URLRequest method to get the address of our php file and attach the php vars.
*/
var urlRequest:URLRequest = new URLRequest("http://mywebsite.com/checkOrder.php");
trace(urlRequest);
/*
the POST method is used here so we can use php's $_POST function in order to recieve our php variables.
*/
urlRequest.method = URLRequestMethod.POST;
/*
this attaches our php variables to the url request
*/
urlRequest.data = phpVars;
/*
we use the URLLoader class to send the request URLVariables to the php file
*/
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
/*
runs the function once the php file has spoken to flash
*/
urlLoader.addEventListener(Event.COMPLETE, showResult);
/*
we send the request to the php file
*/
urlLoader.load(urlRequest);
}
setInterval(checkDataBase, 8000);
/*
function to show result
*/
function showResult (e:Event):void
{
orderDetails.text = "" + e.target.data.result_message;
if(orderDetails.text != "")
{
rejectBtn.y = 380.20;
}
}

addEventListener(Event.ENTER_FRAME,checkForNewOrder);
This causes your function to be called with the framerate you set up, so mabye 30 times per second.
setIntervall will add additional calls to the function. It does not override the enter frame.
Get rid of the enter frame register and you should be good to go.

Related

ActionScript error when attempting call method within PHP file

I have been following a flash tutorial online and I have created a simple flash interface. I am trying to retrieve data from my SQL database via a PHP file and display. I get the following error when I compile:
Error #1009: Cannot access a property or method of a null object reference
var variables1:URLVariables = new URLVariables();
var varSend1:URLRequest = new URLRequest("databaseCall.php");
varSend1.method = URLRequestMethod.POST;
varSend1.data = variables1;
var varLoader1:URLLoader=new URLLoader();
varLoader1.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader1.addEventListener(Event.COMPLETE,completeHandler1);
variables1.comType = "requestEntries";
varLoader1.load(varSend1);
function completeHandler1(event:Event):void{
if(event.target.data.returnBody ==""){
gbOutput_txt.text = "No data coming through";
} else{
gbOutput_txt.condenseWhite = true;
gbOutput_txt.htmlText = "" +event.target.data.returnBody;
}
}
My code exactly matches the code that is used within the tutorial. I have modified the php file to simply return "" so the issue almost definitely lies within the action script...I think :S The compiler falls over when he completeHandler1 function is called. What do you think could be causing this?
Thanks in advance.
You need to declare the completeHandler1 function before you attempt to use it anywhere else.
Here is your code modified to describe what I am talking about.
var variables1:URLVariables = new URLVariables();
var varSend1:URLRequest = new URLRequest("databaseCall.php");
varSend1.method = URLRequestMethod.POST;
varSend1.data = variables1;
function completeHandler1(event:Event):void{
if(event.target.data.returnBody ==""){
gbOutput_txt.text = "No data coming through";
} else{
gbOutput_txt.condenseWhite = true;
gbOutput_txt.htmlText = "" +event.target.data.returnBody;
}
}
var varLoader1:URLLoader=new URLLoader();
varLoader1.dataFormat = URLLoaderDataFormat.VARIABLES;
varLoader1.addEventListener(Event.COMPLETE,completeHandler1);
variables1.comType = "requestEntries";
varLoader1.load(varSend1);
Give this a try and let me know if it works.

UrlRequest not working inside a SimpleButton

I have a UrlRequest in my Main Class, that does a request to a server in cakePHP, and its working fine, but when I do the exact same request in a click of a button its not working.
Can someone help me?
This is my code to do the request:
var token:String = LoaderInfo(root.loaderInfo).parameters.requestToken;
var myData:URLRequest = new URLRequest("users/personalInfo/"+token);
myData.method = URLRequestMethod.GET;
loader = new URLLoader(myData);
loader.addEventListener(Event.COMPLETE, onLoaded);
loader.dataFormat=URLLoaderDataFormat.TEXT;
loader.load(myData);
function onLoaded(e:Event):void {
MovieClip(this.root).gotoAndStop("main");
}
Click Handler
public function SaveProfileButton() {
addEventListener(MouseEvent.CLICK, mouseClick);
}
private function mouseClick(e:MouseEvent):void{
MovieClip(this.root).output.text = "loading...";
send();
}
send() is the code to do the request.
Thanks
I think you may need a cross domain policy xml on your server where the php is. 99% sure. http://www.adobe.com/devnet/articles/crossdomain_policy_file_spec.html

qt progress bar is not working properly

I have one problem..
I am making post request to php script. And I am getting back result as an xml. How can I make my qprogressbar working.
I have tried this:
v
oid MainWindow::init()
{
url = "http://127.0.0.1:8888/direkt_php_qt.php";
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(updateDataTransferProgress(qint64,qint64)));
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
}
void MainWindow::updateDataTransferProgress(qint64 bytesReceived, qint64 bytesTotal)
{
ui->progressBar->setMaximum(bytesTotal + (bytesTotal * 0.25));
ui->progressBar->setValue(bytesReceived);
ui->progressBar->show();
}
But it's not working. I am getting error:
Object::connect: No such signal QNetworkAccessManager::downloadProgress(qint64,qint64)
How can I make this work with manager variable or something like that.
EDIT 2:
This is for example function that is calling init()
void MainWindow::Citanje_korisnika()
{
init();
QUrl params;
params.addQueryItem("action","Citanje_korisnika");
QByteArray data;
data.append(params.toString());
data.remove(0,1);
QNetworkRequest request;
request.setUrl(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant("application/x-www-form-urlencoded"));
reply = manager->post(request, data);
}
I tried your code but I always get unexpectedly out from program. Can you change me my function from which I am calling post method so it can work with init()
donwloadProgress is a signal of QNetworkReply.
Try something like this:
url = "http://127.0.0.1:8888/direkt_php_qt.php";
manager = new QNetworkAccessManager(this);
QNetworkReply* reply = manager->get(QNetworkRequest(url));
connect(reply, SIGNAL(downloadProgress(qint64, qint64)),this, SLOT(updateDataTransferProgress(qint64,qint64)));

How to pass value from flash to php

Hello i am new to the flash . Just i want to how to pass values from Flash variable to php
i am using this code
var myVars:LoadVars = new LoadVars();
myVars.playerName = "Some Body";
myVars.playerTime = Timer;
myVars.send("index.html", "_parent", "POST");`
it show error - >
Warning: 1060: Migration issue: The method LoadVars is no longer supported. For more information, see the URLVariables class, the URLRequest.urlVariables and URLRequest.postData properties, and the URLLoader.dataFormat property..
please guide me how do i resolve this error . i am using CS4 for flash and AS3.0
LoadVars() is AS2 only. You need to use URLLoader. Try this class:
package
{
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.Event;
/**
* #author Marty Wallace
* #version 1.00
*/
public class PHPData extends Object
{
/**
* Sends data to a PHP script
* #param script A URL to the PHP script
*/
public function send(script:String, vars:URLVariables):void
{
var req:URLRequest = new URLRequest(script);
req.data = vars;
req.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.load(req);
// listeners
loader.addEventListener(Event.COMPLETE, _complete);
}
/**
* Called when a response has been received from a PHP script
* #param e Event.COMPLETE
*/
private function _complete(e:Event):void
{
var vars:URLVariables = new URLVariables(e.target.data);
var i:String;
for(i in vars)
{
trace(i + ": " + vars[i]);
}
e.target.removeEventListener(Event.COMPLETE, _complete);
}
}
}
Then you can do what you're doing like so:
var php:PHPData = new PHPData();
var vars:URLVariables = new URLVariables();
vars.playerName = "Some Body";
vars.playerTime = Timer;
php.send("index.php", vars);
Another thing I noticed was you're using send to send data to a .html document rather than a .php document..
A small tutorial on creating this class to use (based on comments):
Click File -> New -> ActionScript file.
Paste the above package (first chunk of code) into the new file.
Save the file in the same directory as your .fla file.
Paste my second snippet of code into the timeline of your .fla file.
All should work from here.
Here is a .zip containing an example you can use.
http://junk.projectavian.com?f=phpdata.zip

AS2: load class variables with sendandload

I'm using Actionscript 2.0 in combination with PHP, now I can make a call to my PHP file and receive data but apparently I have to use that data immediately, I cannot use it to fill my class variables.
This is what I want :
class user {
var lastname:String;
function user(in_ID:Number){
var ontvang:LoadVars = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
ontvang.onLoad = function(success:Boolean) {
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
};
}
}
I've found out that this is a big issue in AS2, I found this post to work around it if you're loading XML data but I can't seem to get it to work with LoadVars :
http://www.actionscript.org/forums/showthread.php3?t=144046
Any help would be appreciated ..
When your onLoad handler is called, it is being called as if it were a member function of the LoadVars instance, and not your user instance.
There are several ways around this, one is to use Delegate.create() to create a function which will work as intended, for example:
import mx.utils.Delegate;
class user {
var lastname:String;
var ontvang:LoadVars;
function user(in_ID:Number){
ontvang = new LoadVars();
var zend:LoadVars = new LoadVars();
zend.ID = in_ID;
ontvang.onLoad = Delegate.create(this, onLoad);
zend.sendAndLoad("http://localhost/Services/getUser.php", ontvang, "POST");
};
}
function onLoad(success:Boolean) : Void
{
if (success) {
lastname = ontvang.lastname;
} else {
lastname = 'error';
}
}
}
Don't forget that the load is asynchronous - when you create one of your user objects, the member variables won't be immediately available. What you may need to do is let your user object be capable of signaling its readiness much like LoadVars does, (e.g. with a callback function provided by the caller) so that your app is driven by by these asynchronous events.

Categories