PDA

View Full Version : an array with nothing in it returns null or 0 or what????


ursulaminor
02-10-2004, 09:41 AM
Hi

I have this AS code

for(i=0;i<10;i++){
if ((dataStore[i]==0)&&(pos[i]<280)){
Q1count +=Number(((dataStore[i]==0)&&(pos[i]<280)));
possumAnswer_mc.possum_mc.theAnswer_txt.text = Q1count;
}
}
this.deconstruct();
}


The problem is, that sometimes there is nothing in the array (pos[i] array meeting the <280 requirement. Instead of it returning 0 or null...it returns absolutely nothing. How can I get it to say '0'. I feel like I should be able to write an if statement that says there's no entry found then theAnswer_txt.text = 0 but I don't know how to say in actionscript 'if there is nothng in the array'

Thanks to anyone who has an aswer to this.

tg
02-10-2004, 10:39 AM
dataStore[i]==undefined

pixelwit
02-10-2004, 10:40 AM
Test to see if the current index of your array is "undefined".var arry = [];
arry[0] = "str";
arry[2] = 1;
arry [4] = this.createEmptyMovieClip("Dot", 10);
for(var i=0; i<arry.length; i++){
if(arry[i]== undefined){
trace("arry["+i+"] == undefined");
}
}-PiXELWiT
http://www.pixelwit.com

tg
02-10-2004, 10:42 AM
your typing way too much pixelwit... but as usually, its the more robust answer.

pixelwit
02-10-2004, 10:48 AM
Yeah I know but I like posting code you can test with a quick copy and paste. Plus it assures that what I say works actually does work. You know, keeping myself honest. ;)

-PiXELWiT
http://www.pixelwit.com

tg
02-10-2004, 10:56 AM
[i] You know, keeping myself honest. ;)
-PiXELWiT
http://www.pixelwit.com [/B]

why in the world would anyone ever want to do that....

pixelwit
02-10-2004, 11:03 AM
Good point. Like my footer says, "truth is only relevant in times of reason" and I don't find today's times very reasonable. Perhaps I'll start posting crap code (well crappier than usual anyway) for the fun of it. ;)

-PiXELWiT
http://www.pixelwit.com

tg
02-10-2004, 11:32 AM
hehhehheh.

senocular
02-10-2004, 11:54 AM
overachiever :rolleyes:











:D

pixelwit
02-10-2004, 12:42 PM
"Overachiever"? Ha! That's a good one. I've never been called that before. ;)

Although I do have to admit sometimes I get caught up in answering questions. Take this thread (http://www.actionscript.org/forums/showthread.php3?s=&threadid=41410) for example. The five lines describing the snapIt function probably would have been enough to get the point across but all that other stuff was so much fun to make I just had to post it.

So I talk too much. Is that what you're saying? Well in that case I

-PiXELWiT
http://www.pixelwit.com

ursulaminor
02-10-2004, 01:05 PM
Hi

I don't know how to use what you have sent! I'm going to post more code from the function, because when I put pixel wit's code in it made things really whacky...sent back over 1800 undefineds!!!


question2 = function(){
pos = new Array();{
pos[0] = (hedgehog1_mc._x);
pos[1] = (hedgehog2_mc._x);
pos[2] = (hedgehog3_mc._x);
pos[3] = (hedgehog4_mc._x);
pos[4] = (hedgehog5_mc._x);
pos[5] = (hedgehog6_mc._x);
pos[6] = (hedgehog7_mc._x);
pos[7] = (hedgehog8_mc._x);
pos[8] = (hedgehog9_mc._x);
pos[9] = (hedgehog10_mc._x);
}

for(i=0;i<10;i++){
if ((dataStore[i]==0)&&(pos[i]>280)){
Q2count +=Number(((dataStore[i]==0)&&(pos[i]>280)));

possumAnswer_mc.possum_mc.theAnswer_txt.text = Q2count;
}
}
this.deconstruct();
}

The whole array is never empty, it's filled with the positions of mc's. After you drag a mc, the array is repopulated witht he new positions. In the case of this code I want to find out how many in that array have a position >280. I should try an if statement that says if all of them are less than 280 Q2count = 0 or is there a shorter way?
Thanks


& truth = madness

senocular
02-10-2004, 01:05 PM
theres nothing wrong with being descriptive ;) Ive had my fair share of long posts :D

ursulaminor
02-10-2004, 01:27 PM
Hi there

I'm not sure how to solve this. I tried making the info from the pos[] array a new variable and adding an if statement that if the array is undefined the text = 0, but it turns up 0 in every instance. Previously it counted correctly up until there were no mc<280. I think the problem is that the array is never undefined because there is always something in it. There is just not always something in it that fits the criteria I'm asking for. Is that null as oppossed to undefined?


//this array is outside of the function and doesn't change during the the session and movNum_ is always 1 or 0
dataStore = [movNum1,movNum2,movNum3,movNum4,movNum5,movNum6,mo vNum7,movNum8,movNum9,movNum10];

//this is the function with the dynamic array

question1 = function(){
pos = new Array();{
pos[0] = (hedgehog1_mc._x);
pos[1] = (hedgehog2_mc._x);
pos[2] = (hedgehog3_mc._x);
pos[3] = (hedgehog4_mc._x);
pos[4] = (hedgehog5_mc._x);
pos[5] = (hedgehog6_mc._x);
pos[6] = (hedgehog7_mc._x);
pos[7] = (hedgehog8_mc._x);
pos[8] = (hedgehog9_mc._x);
pos[9] = (hedgehog10_mc._x);
}
//locating the 0.swfs and combining with position
for(i=0;i<10;i++){
position =(pos[i]<280);
if ((dataStore[i]==0)&&position){
Q1count +=Number(((dataStore[i]==0)&&postion));
possumAnswer_mc.possum_mc.theAnswer_txt.text = Q1count;
} else if ((dataStore[i]==0)&&(position==undefined)){
possumAnswer_mc.possum_mc.theAnswer_txt.text = 0;
}
}
this.deconstruct();
}

pixelwit
02-10-2004, 04:42 PM
Is this what you're trying to do?dataStore = [0, 0, 0, 1, 1, 0, 1, 0, 1, 1];
pos = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500];
//
function someFunc(){
var Q1count = 0;
for (i=0; i<10; i++) {
Q1count +=Number((dataStore[i]==0)&&(pos[i]<280));
}
trace(Q1count);
}
//
someFunc();-PiXELWiT
http://www.pixelwit.com

ursulaminor
02-10-2004, 09:08 PM
For some reason I can't post a reply....so I'm testing with this Quick Reply Box...

ursulaminor
02-10-2004, 09:10 PM
Hi PixelWit
Thank you and Senocular for trying to help.:D I don't think my brain is suited to this. :confused:

Here is a link to the files Hedgehog Math (http://www.edu.uwo.ca/essofamilymath/hhogs.zip)

I think I'm asking the wrong question in the function..or I have to find a way to have the information be funnelled into 4 arrays right from the start. (the start of each question function) I'm going to attach the last working file so that you can see what it should look like. it works up until you have all of one outside the bed.

I've tried a dozen different ways of making if's and else if 's to get it right. I'm going to have a closer look at what you've posted and see if I can sort it out.

Thanks so so much!!

ursulaminor
02-10-2004, 09:18 PM
Hi again

Yes Pixel Wit...that is what I'm trying to do....but..what happens is that if all the '0'mc in dataStore = [0, 0, 0, 1, 1, 0, 1, 0, 1, 1]; have been dragged to the right of the stage, to have an x position greater that 280 than there is nothing in the combination of the 2 arrays that meets the criteria...( a 0.MC on the left of the stage) so it doesn't track as null or undefined or 0 or false or anything.

Stymied

pixelwit
02-10-2004, 09:33 PM
I don't know what you're trying to achieve or the structure of your file. It would be easier for me if we worked with the code I provided earlier.

Change the values in the array, tell me what happens and then tell me what should happen. Then we'll both be reading from the same page.

-PiXELWiT
http://www.pixelwit.com

ursulaminor
02-10-2004, 10:23 PM
Your code works when I put it in a file by itself. (which I should have tried right away) It traces what it's supposed to. It doesn't work when I integrate it into my file. There must be something else messing things up. I'm going to go over everything again. Thanks for your trouble. I'll let you know how it goes. :)

ursulaminor
02-11-2004, 09:31 AM
PixelWit

Thank you!!! That code is perfect! It is so much cleaner and (most importantly) it works perfectly. Thank You thank You thank You!:D

pixelwit
02-11-2004, 10:47 AM
You're welcome.

-PiXELWiT
http://www.pixelwit.com