Wednesday, 23 June 2010

JavaScript CAN Call PHP Functions

So this is where I got to:

function gQ() {
var request = new XMLHttpRequest();
request.open("GET","getQus.php?val=What",false);
request.send();
val = request.responseText;
return val;
}

The thing is, this just gives me a query string, I can get that from PHP directly if I want one, and with less fuss.

Oh well... at least I know it can be done.

Can JavaScript Call PHP Functions? cont...

I just changed the js part to:

document.getElementsByTagName('head')[0].appendChild(script);
var vall = script.attributes.toString();
return vall;

and that got through to the Flash straightaway... what does it all mean?

Can Javascript call PHP functions?

I am beginning to wonder...

Here's the thing, I want to get some info from a MySQL database into Flash, and if I do it straight via PHP, I am going to get these really long query strings, that just might be a bit unreliable, for example if they have any ampersands in them where they shouldn't be.

But Javascript outputs arrays and objects, and these are so much tidier. If I can get the PHP to send the info to Flash as Javascript, that will be much cooler.

'include' won't work, because the output needs to depend on the parameters that Flash sends it, 'include' would simply output something as the html file is loading or whatever, and would then remain that way forever.

'Notion commotion' on Dev Shed had a solution that I can't quite get to do what I want, but here is where I am so far:

function gQ() {

var script = document.createElement('script');
script.src = 'getQus.php?val=What';
document.getElementsByTagName('head')[0].appendChild(script);
return vall;

}

and in the getQus.php file:

header("Cache-Control:no-cache");
header('Content-Type:text/javascript');
$val=$_GET['val'].' the...';
echo "vall = '$val';";


The Flash file makes an External Interface call (when a button is pressed) to the Javascript function, and puts the returned value into a text box.

What actually happens is that I have to press the button twice to get anything back.

Why, why?

Monday, 21 June 2010

Sorting by date

Hurrah!

My date sorting problem is finally solved. Simply format your date with the most important figures first, and with leading zeros. Then any ordinary sort (ie alphabetical) will return an array of these sorted by year, then month, etc.

eg. 4.15pm 23rd September 2009 => '2009-09-23-16-15'

PS. Actionscript 3 considers 09 to represent the month of October, because January is 0.

Friday, 11 June 2010

Oh dear, oh dear ... oh dear.

Stung by the cache of loaded swfs... again.

Basically, the animation was getting hung up because it couldn't find the variables is was looking for - because instead of loading the updated swf - it was loading some outdated version that didn't have everything in it.

The way to avoid this, for testing purposes, is to attach a querystring with a random value - so that each time the swf is loaded, it is a different call, then the browser will look for the most recent file.

Text won't load

I have decided to mention this before I figure out how to do it - thinking out loud, as it were, might help me to make sense of it.

So I am going to remove functionality step by step, until I track the source of the problem - I have an array that contains a list of all the text fields, and a pointer to what text it should be displaying, it works on the frames that aren't that complicated.

It also works on my test environment.

So first of all, I will switch off all the php/js type things in the swf, and see if it works then.

Thursday, 10 June 2010

Array.filter

Using a helpful page by 'devon o wolfgang', I am using this:

var arr:Array = ['','later','what','','ok'];
trace(arr);
arr = arr.filter(removeBlankElements);
function removeBlankElements(element:*, index:uint, arr:Array):Boolean {
return element != '';
}
trace(arr);

to get rid of blank string elements in my arrays.

Getting the String out of the Textfield

I had always thought, possibly due to the lack of autofill, that you couldn't do this:

trace(f.text.indexOf('h'));

but you can.

Oh well.

Friday, 4 June 2010

Inactivating buttons

This is just to remind myself, it is not the first time I have come across this issue, but I still managed to forget it anyway...

I have a movieclip with a button inside it. I attached an event listener to the movie clip. In order to deactivate it, I had to set mouseEnabled to false for both the button AND the movieclip.

Event Listeners

When adding eventListeners, it is normally necessary to write a specific function that will respond, because most of actionscript's pre-existing functions that you might think you could just use 'straight' as it were, get upset when they receive the event as a parameter.

e.g.

btn.addEventListener(MouseEvent.CLICK, stop);

stop() is not expecting any parameters, and doesn't like the event parameter it receives. I have found one exception to this rule, and the result is quite interesting.

btn.addEventListener(MouseEvent.CLICK, trace);

results in:

[MouseEvent type="click" bubbles=true cancelable=false eventPhase=3 localX=4.15 localY=-0.95 stageX=41.6 stageY=405.65 relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]

Interesting...I wonder what delta is, but can't be bothered to figure it out now.