FRIHOST Ľ FORUMS Ľ SEARCH Ľ FAQ Ľ TOS Ľ BLOGS Ľ COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Actionscript Problem: array failure?





Fire Boar
This is probably something really trivial, but I do have a huge problem with this script at the moment.

Code:
// Can't have it going on and on whilst choosing armour...
stop();
// Load up some variables from the file
var allvars = new LoadVars();
allvars.load("getflashvars.php?uid=37");
// Turn them into separate global vars for easy access
_global.armour = allvars.armour;
_global.helmet = allvars.helmet;
_global.shield = allvars.shield;
_global.pluses = allvars.pluses;
_global.plhp = allvars.plhp;
_global.plhpmax = allvars.plhpmax;
_global.plmp = allvars.plmp;
_global.plmpmax = allvars.plmpmax;
_global.plstatus = allvars.plstatus;
_global.platt = allvars.platt;
_global.pldef = allvars.pldef;
_global.plevel = allvars.plevel;
_global.plclass = allvars.plclass;
_global.plpic = allvars.plpic;

// Ok, now we've globalised it all, let's load up the armour and stuff for this frame
// Grab the armour data into an array
var armour_array:Array = _global.armour.split("▀");
var helmet_array:Array = _global.helmet.split("▀");
var shield_array:Array = _global.shield.split("▀");

// Set the current top item to be the first (0)
var currArm:Number = 0;
var currHel:Number = 0;
var currShi:Number = 0;

// Get the total number of each armour type
var totalArm:Number = armour_array.length;
var totalHel:Number = helmet_array.length;
var totalShi:Number = shield_array.length;

// Bung the text in the right places
// Armour
armtext1.textholder.text = armour_array[0];
armtext2.textholder.text = armour_array[1];
armtext3.textholder.text = armour_array[2];
armtext4.textholder.text = armour_array[3];
armtext5.textholder.text = armour_array[4];
armtext6.textholder.text = armour_array[5];
armtext7.textholder.text = armour_array[6];

// Helmets
heltext1.textholder.text = helmet_array[0];
heltext2.textholder.text = helmet_array[1];
heltext3.textholder.text = helmet_array[2];
heltext4.textholder.text = helmet_array[3];
heltext5.textholder.text = helmet_array[4];
heltext6.textholder.text = helmet_array[5];
heltext7.textholder.text = helmet_array[6];

// Shields
shitext1.textholder.text = shield_array[0];
shitext2.textholder.text = shield_array[1];
shitext3.textholder.text = shield_array[2];
shitext4.textholder.text = shield_array[3];
shitext5.textholder.text = shield_array[4];
shitext6.textholder.text = shield_array[5];
shitext7.textholder.text = shield_array[6];

// functions for the Next buttons
function armnextitem(eventObj:Object) {
if ((currArm+6)<totalArm) {
    currArm++;
armtext1.textholder.text = armour_array[currArm];
armtext2.textholder.text = armour_array[(currArm+1)];
armtext3.textholder.text = armour_array[(currArm+2)];
armtext4.textholder.text = armour_array[(currArm+3)];
armtext5.textholder.text = armour_array[(currArm+4)];
armtext6.textholder.text = armour_array[(currArm+5)];
armtext7.textholder.text = armour_array[(currArm+6)];
   }
}
function helnextitem(eventObj:Object) {
if ((currHel+6)<totalHel) {
    currHel++;
heltext1.textholder.text = helmet_array[currHel];
heltext2.textholder.text = helmet_array[(currHel+1)];
heltext3.textholder.text = helmet_array[(currHel+2)];
heltext4.textholder.text = helmet_array[(currHel+3)];
heltext5.textholder.text = helmet_array[(currHel+4)];
heltext6.textholder.text = helmet_array[(currHel+5)];
heltext7.textholder.text = helmet_array[(currHel+6)];
   }
}
function shinextitem(eventObj:Object) {
if ((currShi+6)<totalShi) {
    currShi++;
shitext1.textholder.text = shield_array[currShi];
shitext2.textholder.text = shield_array[(currShi+1)];
shitext3.textholder.text = shield_array[(currShi+2)];
shitext4.textholder.text = shield_array[(currShi+3)];
shitext5.textholder.text = shield_array[(currShi+4)];
shitext6.textholder.text = shield_array[(currShi+5)];
shitext7.textholder.text = shield_array[(currShi+6)];
   }
}
// functions for the Previous buttons
function armprevitem(eventObj:Object) {
if ((currArm)>0) {
    currArm = currArm-1;
armtext1.textholder.text = armour_array[currArm];
armtext2.textholder.text = armour_array[(currArm+1)];
armtext3.textholder.text = armour_array[(currArm+2)];
armtext4.textholder.text = armour_array[(currArm+3)];
armtext5.textholder.text = armour_array[(currArm+4)];
armtext6.textholder.text = armour_array[(currArm+5)];
armtext7.textholder.text = armour_array[(currArm+6)];
   }
}
function helprevitem(eventObj:Object) {
if ((currHel)>0) {
    currHel = currHel-1;
heltext1.textholder.text = helmet_array[currHel];
heltext2.textholder.text = helmet_array[(currHel+1)];
heltext3.textholder.text = helmet_array[(currHel+2)];
heltext4.textholder.text = helmet_array[(currHel+3)];
heltext5.textholder.text = helmet_array[(currHel+4)];
heltext6.textholder.text = helmet_array[(currHel+5)];
heltext7.textholder.text = helmet_array[(currHel+6)];
   }
}
function shiprevitem(eventObj:Object) {
if ((currShi)>0) {
    currShi = currShi-1;
shitext1.textholder.text = shield_array[currShi];
shitext2.textholder.text = shield_array[(currShi+1)];
shitext3.textholder.text = shield_array[(currShi+2)];
shitext4.textholder.text = shield_array[(currShi+3)];
shitext5.textholder.text = shield_array[(currShi+4)];
shitext6.textholder.text = shield_array[(currShi+5)];
shitext7.textholder.text = shield_array[(currShi+6)];
   }
}

// add the event listener for the buttons
armprev.addEventListener("click", armprevitem);
armnext.addEventListener("click", armnextitem);
helprev.addEventListener("click", helprevitem);
helnext.addEventListener("click", helnextitem);
shiprev.addEventListener("click", shiprevitem);
shinext.addEventListener("click", shinextitem);

It's for an RPG modification to my website, and the flash file is a battle system I'm making for it (the current one is really untidy and keeps having to refresh the page every time someone does something).

Now, when the frame loads up, all the *text*.textholder.text fields are set to "undefined". Meaning that there's probably something wrong with the array. So this part would seem to be the culprit:
Code:
// Load up some variables from the file
var allvars = new LoadVars();
allvars.load("getflashvars.php?uid=37");
// Turn them into separate global vars for easy access
_global.armour = allvars.armour;
_global.helmet = allvars.helmet;
_global.shield = allvars.shield;
_global.pluses = allvars.pluses;
_global.plhp = allvars.plhp;
_global.plhpmax = allvars.plhpmax;
_global.plmp = allvars.plmp;
_global.plmpmax = allvars.plmpmax;
_global.plstatus = allvars.plstatus;
_global.platt = allvars.platt;
_global.pldef = allvars.pldef;
_global.plevel = allvars.plevel;
_global.plclass = allvars.plclass;
_global.plpic = allvars.plpic;

// Ok, now we've globalised it all, let's load up the armour and stuff for this frame
// Grab the armour data into an array
var armour_array:Array = _global.armour.split("▀");
var helmet_array:Array = _global.helmet.split("▀");
var shield_array:Array = _global.shield.split("▀");


Am I setting it up right? Could someone please give me some advice on this.

Oh, and the file to load the variables from, "getflashvars.php" is working fine and returns the following:
Code:
armor=Studded Leather Armour▀&helmet=Metal Cap▀&shield=Buckler▀&pluses=&plhp=13&plhpmax=14&plmp=8&plmpmax=8&plstatus=0&platt=14&pldef=11&plevel=3&plclass=Journeyman&plpic=../../shop/uses/arena/arena_player_Journeyman


(there are no newlines in there, it just looks that way because the line is too long to fit)

Any ideas would be great. Thanks.
game_dev
Hi,

I think you haven't looked to carefully into how to use the LoadVars class?

The LoadVars class is assyncronous ( ie, data is not available to be processed right away ) So you have to use a callback function to act on data only when the data is made available.


Code:
var lv = new LoadVars();
lv.load( "myurl.php" );
lv.onLoad = function(){
    // data is available when this function runs so do stuff in here
}



Also, you should not be coding Flash "blindly", ie, simply running the code and waiting it to work or not, as opposed using the trace statement and the debugger tool in Flash. These are two powerful tools that any serious coder uses to find out when the code is really working or not.

For instance:


Code:
function changeColor( colorName ){

   trace( "The function was called");
   trace( "The received value was: "+ colorName );

   myAvatar.skins.gotoAndStop( colorName );
}

// call the function with data that you got somewhere else
_root.changeColor( myLoadedValue )




Now in the case of the above code, if the avatar doesn't change colors, the trace statements imediately tell you if the problem is that the function was not called or if it was called but the passed value was undefined.

Believe me, doing it this way will save you a LOT of headache in the future.
It's more than worth the extra effort. Wink

Regards,

gd
Fire Boar
Thanks a lot. I'm really new at Actionscripting, though I have been doing other languages. It never occured to me to wait for the PHP file to run before going ahead and setting the variables. I'll change the script now.

Thanks also for the advice on debugging, but I have a question: how do you activate the debug mode? Trace doesn't appear on screen as far as I'm aware. Do I have to use the CTRL+Enter test feature to see trace outputs? If so, that would mean I would need to install Apache or something similar on my computer, wouldn't it?

Edit: Okay, I found there were two problems. First, you were correct, the data hadn't loaded yet. But also, it seems that Flash didn't recognise the symbol ▀, probably because it's not on the standard ASCII table, only on the extended one. I changed it for a pair of square brackets and it works now. All I have to do now is get rid of the "undefined" entries, but that should be easy.
Related topics
A PHP problem !
HTML + PHP problem
PHP Problem...
Problem Help out
Problem with sending emails (sender address verification)
Need Expert help in php-mysql
Problem with usb mice (mouse-es).
Programming in C problem
Strange flash intro problem
php array() problem
Php while array problem
HP psc 750 - Scanner Failure problem
Snowboard Bindings
Girl of four died in sat-nav error crash
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.