Groups | Blog | Home
all groups > flash actionscript > august 2006 >

flash actionscript : Arrays Project


sbryner
8/16/2006 8:58:15 PM
Hello all

Please check out this html page to see if anybody can give me a simple way or
a little more help with arrays to complete this supposedly fun project that's
kicking
my shines.

thank you all who've tried to help my cram this info into my brain.

http://http://skysflashtest.50webs.com/index.html
blemmo
8/16/2006 9:43:24 PM
Ok, I don't know what you're trying to do here, but maybe it will help to get
you going:
First, arrays start at index 0, not 1. You can also use Array.push() to add an
item on the next free position, instead of doing it by Array[x] = yz;
Then it seems you want to use objects in your array. This can be done by using
the {} as an object constructor, like this:
var obj = {prop1:value1, prop2:value2}

So the array creation might be like this, with the object creation inside the
push() function:

var myArray:Array = new Array();
myArray.push( {price:100, picture:"1.jpg", description:"my stuff"} );
etc.

You can access the object members in a certain array index like that:
theprice = myArray[0].price;

hth,
blemmo
sbryner
8/16/2006 9:48:51 PM
can you use a variable for the index?
say there is an input text with a number "1" in the field


myNum = inputText.text;
theprice = myArray[myNum].price;
thanks,
blemmo
8/16/2006 9:59:25 PM
Yep, but inputText.text will be a string type. Convert it to a number with
parseInt(). If it's user input, you might want to check if it's valid; parseInt
returns NaN when the input is not a number, so you could check that before you
use it.

myNum = parseInt(inputText.text);
if ( isNaN(myNum) ){
// bad input
}
else {
theprice = myArray[myNum].price;
}


sbryner
8/16/2006 10:20:37 PM
Thanks Blemmo,
not sure if you could see the html. I tried clicking on the link and it didn't
work

try this.

so, see if this sounds right to you?

http://skysflashtest.50webs.com/index.html

myArray:Array = new Array ();
//creates an array

myArray.push( {price:100, picture:"1.jpg", description:"my stuff"} );
myArray.push( {price:150, picture:"2.jpg", description:"his stuff"} );
myArray.push( {price:100, picture:"3.jpg", description:"her stuff"} );
myArray.push( {price:180, picture:"4.jpg", description:"our stuff"} );

//populates myArray

var myNum:Number = input_txt.text

btn_mc.onRelease =function(){
myNum = parseInt(inputText.text);
if ( isNaN(myNum) ){
// bad Number
}
else {
for(i=0;i<myArray.length-1;i++;){
if (myArray[myNum].price==100){
theprice = myArray[myNum].price;
thepicture=myArray[myNum].price;
thedescription=myArray[myNum].description;
dynamicTextPrice_txt.text = theprice;
imageLoader.contentPath=thepicture;
dynamicTextDescription_txt.text= thedescription;
}
}
};
sbryner
8/16/2006 10:43:18 PM
somethings wrong with this code: syntax error.

especially:

for(i=0;i<myArray.length+1;i++;){
(myArray[myNum].price=100){




btn_mc.onRelease =function(){
myNum = parseInt(inputText.text);
if ( isNaN(myNum) ){
input_txt.text="";
}else{
for(i=0;i<myArray.length+1;i++;){
(myArray[myNum].price=100){

theprice = myArray[myNum].price;
thepicture=myArray[myNum].price;
thedescription=myArray[myNum].description;

dynamicTextPrice_txt.text =theprice;
imageLoader.contentPath=thepicture;
dynamicTextDescription_txt.text= thedescription;
}
}
}
Mr Helpy mcHelpson
8/16/2006 10:52:13 PM
w/o looking at it too deep, it seems you have an extra ; on the end of i++...

for(i=0;i<myArray.length+1;i++;)
should be

for(i=0;i<myArray.length+1;i++)

sbryner
8/17/2006 12:57:00 AM
I've figured out most of the code now but now I'm getting syntax error on my
"if... else.." statement.

it appears everything is ok so I don't get it. Only thing I can think of is
you can't have these three lines together.

} else {
for( ...){
if( ....)



here's the code:


btn_mc.onRelease = function ( ) {
myNum = parseInt(input_txt.text);

if ( isNaN(myNum) ) {
input_txt.text = "This is not a Number";
}

} else {
// here is where my syntax error is.

for (var i:Number=0;i<=myArray.length+1;i++){
myhomeNum=myArray[myNum].price;
if (myhomeNum = 100) {

theprice = myArray[myNum.price;
thepricture=myArray[myNum].picture;
thedescription=myArray[myNum].description;

dynamicTextPrice_txt.text = the price;
imageLoader.contentPath=thepicture;
dynamicTextDescription_txt.text = thedescription;
}
}
};
myIP
8/17/2006 1:10:43 AM
Tip: always kept your code organized.

btn_mc.onRelease = function ()
{
myNum = parseInt(input_txt.text);

if (isNaN(myNum))
{
input_txt.text = "This is not a Number";
}else{

for (var i:Number=0;i<=myArray.length+1;i++)
{
myhomeNum=myArray[myNum].price;

if (myhomeNum == 100)
{
theprice = myArray[myNum].price;
thepricture=myArray[myNum].picture;
thedescription=myArray[myNum].description;

dynamicTextPrice_txt.text = theprice;
imageLoader.contentPath = thepicture;
dynamicTextDescription_txt.text = thedescription;
}//ends if (myhomeNum == 100)
}//ends for (var i:Number=0;i<=myArray.length+1;i++)
}//ends else{
}//ends btn_mc.onRelease = function ()
AddThis Social Bookmark Button