all groups > flash actionscript > august 2006 >
You're in the

flash actionscript

group:

prototype function problem


prototype function problem sbryner
8/24/2006 8:35:22 PM
flash actionscript:
Help, I'm stuck. I've got a prototype that works. I can pull out one of the new
array entries from it.

I want to be able to pull out all the array elements and place their
properites (if more than one element is in the array) and put their respective
pictures and names in another location.

the link below may help you understand what I'm trying to accomplish.

http://skysflashtest.50webs.com

please help, I've looked all over and found nothing that is working for me.

myArray = [{myname:"bob", town:"London", id:1, pic:"1.gif"},
{myname:"hans", town:"Munich", id:2, pic:"2.gif"},
{myname:"manuel", town:"Barcelona", id:3, pic:"3.gif"},
{myname:"bob", town:"Newark", id:4, pic:""}];

Array.prototype.id = function(prop, val) {
var res:Array = new Array();
var i = this.length;
while (i--) {
if (this[i][prop] == val) {
res.push(this[i]);
trace(myArray.myname);
for (var entry in res) {
for (var prop in res[entry]) {
theID = res[entry].id;
theName = res[entry].myname;
theTown = res[entry].town;
thePic = res[entry].pic;
anotherTown = res[entry[i]].town; // would be the second bob in the
Array's town.
anotherPic = res[entry].pic; // would be the second bob in the Array's
picture.

goodsearch();
}
}
} else {
trace("I'm in else");
}
}
return (res);
};

var myArray = myArray.id();


getInfo_mc.onRelease = function() {
var getConvertText = it_txt.text;
var convertedToLowerCase = getConvertText.toLowerCase();
var getName = convertedToLowerCase;
myArray.id("myname", "bob");
};
goodsearch = function () {
//trace("this is a good search");
dtName_txt.text = theName;
//trace(theName);
dtTown_txt.text = theTown;
dtAddress_txt.text = theID;
imageLoader.contentPath = thePic;
dtAnother_txt.text = "Another "+theName+" lives in ";
dtSecName_txt.text = anotherTown;
imageLoader2.contentPath = anotherPic;

};
Re: prototype function problem blemmo
8/24/2006 11:47:05 PM
You're calling the goodsearch function inside the id function, which does the
actual search, that's the wrong way around. The id function is supposed to
search the array and then return the found items, so you can just use the
result of this inside the goodsearch function. Try the attached code. The
search function might also get the parameters (key and value) and just pass it
along to the id function inside.
To display the results, you can fill in the text in the main area with a fixed
index, and add the other results in a loop through the result array, or also
with a defined index. Depends on what results shall be displayed.




myArray = [{myname:"bob", town:"London", id:1, pic:"1.gif"},
{myname:"hans", town:"Munich", id:2, pic:"2.gif"},
{myname:"manuel", town:"Barcelona", id:3, pic:"3.gif"},
{myname:"bob", town:"Newark", id:4, pic:""}];

Array.prototype.id = function(prop, val) {
var res:Array = new Array();
var i = this.length;
while (i--) {
if (this[i][prop] == val) {
res.push(this[i]);
} else {
trace("I'm in else");
}
}
return (res);
};

getInfo_mc.onRelease = function() {
var getConvertText = it_txt.text;
var convertedToLowerCase = getConvertText.toLowerCase();
var getName = convertedToLowerCase;
goodsearch();
};

function goodsearch() {
//trace("this is a good search");
// call the search
var searchResult:Array = myArray.id("myname","bob");
// set the text 1 (static index)
dtName_txt.text = searchResult[0].myname;
dtTown_txt.text = searchResult[0].town;
dtAddress_txt.text = searchResult[0].id;
imageLoader.contentPath = searchResult[0].pic;
// text 2, static index or loop here
dtAnother_txt.text = "Another "+searchResult[0].myname+" lives in ";
dtSecName_txt.text = searchResult[1].town;
imageLoader2.contentPath = searchResult[1].pic;
}
Re: prototype function problem sbryner
8/25/2006 9:02:24 PM
Thanks, Blemmo!

Works Awesome! Now for the Next Step

Re: prototype function problem blemmo
8/25/2006 9:26:48 PM
you're welcome.

cheers,
AddThis Social Bookmark Button