PDA

View Full Version : Flash & PHP - looping issue


villek
03-11-2004, 05:50 AM
I tried searching, but couldn't find any answers, so...

I have multiple loadVars objects on my timeline, like this one:


dataset_a1 = new LoadVars();
dataset_a1.load("fetchstuff.php?number=a-1");
dataset_a1.onLoad = function (loaded_a1) {
if (loaded_a1) {
_root.status.text = "a1 ok, go ahead";
} else {
_root.status.text = "Whoops, something went wrong.";
}
}


The problem is that when I have like 10 of these loadVars objects coded, I just can't be sure whether they've actually loaded or not. I've tried a bunch of different methods and loops, but I just can't seem to get it right, so that when all of these loadVars objects have definately loaded, I could tell my movie to go on and


play();


I know that this is a simple question and propably solved simply with the correct kind of looping, but still. I can't get it done. What I'd like to know is, what would be a consistant way of doing this?

Oh, btw, the reason why I have multiple loadVars objects is that then I can use just one .php script and assign things to a number of movieclips which are not alike, not by far. This was the best way I could think of. Haven't been working on Flash for some time now... and it shows.

Suggestions?

snapple
03-11-2004, 06:45 AM
villek,

could you not just increment a variable, so you might have something like:


myVars = new LoadVars();
myVars.varUserName = userName.text;
myVars.varUserPass = userPass.text;
myVars.onLoad = function (checkPlease)
{
if(checkPlease == true)
{
loadCounter += 1
}
else
//error handling here
}
}


Then on a mc, have something along the lines of:


onClipEvent (enterFrame)
{
if (loadCounter == 10)
{
trace("all the loadVars worked fine");
}
}


I appreciate that puting it on an 'enterFrame' handler, is not exactly efficiant, so you might want it just to be called when your last loadVars is called - so it might be an idea to create a listener.

Regards, snapple

villek
03-11-2004, 07:11 AM
Thank you, Snapple!!!

So could it just go like this:



myVars = new LoadVars();
myVars.varUserName = userName.text;
myVars.varUserPass = userPass.text;
myVars.onLoad = function (checkPlease)
{
if(checkPlease == true)
{
loadCounter += 1
}
else
//error handling here
}
}

followed by the other loadVars (10 all together)... and then

if(loadCounter == 10) {
play();
} else {
stop();
}


In case of stop i.e the loadCounter beeing 9, how would I go about repeating the whole procudure with the 10*loadVars?

With a while loop?

I'm thinking like this:

I'll have 10 diffrent loadVars objects and when one is returned true, the loadCounter is increased by one. The whole thing would go into a while loop like this:



while(loadCounter<10){

everything here...

}


Can't think straight. Simple stuf becomes hard when you don't have time to sleep...

snapple
03-11-2004, 08:27 AM
Yes that would work - have you given it a go?

Regards, snapple :)

villek
03-11-2004, 08:43 AM
:)

Of course I gave it a go. And it crashed my machine. I think the while loop became infinate, because it couldn't get the loadCounter variable out of the functions. What to do?

loadCounter=0;

while(loadCounter<10) {

myVars_1 = new LoadVars();
myVars_1.varUserName = userName.text;
myVars_1.varUserPass = userPass.text;
myVars_1.onLoad = function (checkPlease)
{
if(checkPlease == true)
{
loadCounter += 1
}
else
//error handling here
}
}

myVars_2 = new LoadVars();
myVars_2.varUserName = userName.text;
myVars_2.varUserPass = userPass.text;
myVars_2.onLoad = function (checkPlease)
{
if(checkPlease == true)
{
loadCounter += 1
}
else
//error handling here
}
}

[...it goes on 'til nr. 10...]

} //end while

if(loadCounter == 10) {
play();
} else {
stop();
}


Dunno what to do, I also tried do while, for and what not, no dice. I just can't believe it's this hard to do such a simple thing.

Basicly I just want to get all my variables from php in one frame, for that I need to loop, since Flash can't get 10 or more variables in one frame, since they come too fast.

Frustrating.

Should the loadCounter variable be a _global since it's coming from functions inside the loop? Ho would I go about that?

I am sincerely SO grateful to you Snapple. If you don't have the time or energy to waste on this, please let me know and I'll go back to my previous solution today:

Use a for loop to loop twice through a bundle of ten loadVars objects at a time.

It actually worked, but it's SO lame and inconsistant and insecure that I have to find a beter solution.

snapple
03-11-2004, 09:04 AM
The structure of your while loop is incorrect.


var i = 10;

while (loadCounter < i)
{
trace("not all loadVars done yet");
loadCounter++;
}

if(loadCounter == 10)
{
trace("all loadVars have worked");
}


Regards, snapple :)

villek
03-11-2004, 09:25 AM
Tried that, no dice.

If this was a case of anything else, it would be easy, but it's a case where I need dozens of different rows, with one of four different values each. I need to make dozens of mc's act according to the value that their spesified. Everything else works like a jiffy.

It's only the checking of the getting of the variables that I'm stuck on.

How do people usually do this kind of stuff. I realised that even if one variable from the php script fails the whole idea crumbles, since with while there's no way of knowing which variable failed. I'm leaning towards my hack of a system from earlier today which isn'n even close to good, but it worked.

Grr. Stupid Brain do {thinking} while (problem!=solved)

Here's the code so far:



var i = 30;

// Item 1
while (counter < i) {
dataset_a1 = new LoadVars();
dataset_a1.load("fecthboy.php?item=a-1");
dataset_a1.onLoad = function (loaded_a1) {
if (loaded_a1) {

counter++;

} else {
_root.status.text = "Failed miserably.";
stop();
}
}

// Item 2
[...]

} //end while

if(counter >= 30) {
play();
} else {
stop();
_root.status.text = "Oh boy, no dice!.";
}

villek
03-11-2004, 03:50 PM
Booyah!

I got it done!

Finally, after messing with a bunch of crappy code, I decided to go with arrays.

After a little testing I went with gathering all the info from the db into an array in Flash and splitting that, getting necessary parts / line.

It worked like charm.

Thanks for all the assistance snapple, even if this time it didn't do the trick. It was still great and I'm very greatful to you for helping.

Ta taa!

snapple
03-11-2004, 05:15 PM
Great news. Glad to hear it worked out!