Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Associate input text with array

Status
Not open for further replies.

pmoorey

Technical User
Mar 19, 2007
144
GB
Hi

I was just wondering, I have a an array of pictures that I randomise. I also have an inout tect box. I display a picture from the array for example a picture of an apple and I want the user to type the correct answer into the text box. I am not sure how to check the answer is correct if I am using an array to hold the images

Thanks

Peter
CCNA, Cisco Qualified Specialist
 
This is what I have done so far would this be easy to adapt to use the text within the array too?

Code:
_global.picNumber = 0;
pictureArray = ["images1/badger", "images1/butter", "images1/computer"];
	var n:Number = pictureArray.length;
	while (n) {
		var k:Number = Math.floor(Math.random()*n);
		--n;
		var tmp:Object = pictureArray[n];
		pictureArray[n] = pictureArray[k];
		pictureArray[k] = tmp;
	}
	trace("pictureArray: "+pictureArray);
	var pictureCount:Number = 2;
	var m:Number = pictureCount;
	var pictureList:Array = new Array(m);
	while (m) {
		--m;
		pictureList[m] = pictureArray[m];
		trace("pictureList: "+pictureList);
		holder_mc.loadMovie(pictureArray[picNumber]+".gif");
		
	}
one_btn.onRelease = function () {
		picNumber++;
if(picNumber <= pictureList.length){
holder_mc.loadMovie(pictureArray[picNumber]+".gif");
}else{
picNumber = 0;
holder_mc.loadMovie(pictureArray[picNumber]);
next_btn._visible = false;
} 
		trace (+picNumber);

}

Peter
CCNA, Cisco Qualified Specialist
 
Your Array would be:
[tt]pictureArray = [{image:"images1/badger", text:"Badger"}, {image:"images1/butter", text:"Butter"}, {image:"images1/computer", text:"Computer"}];[/tt]

Then to retrieve the data just do:
[tt]pictureArray.image[/tt] // for "image"
[tt]pictureArray.text[/tt] // for "text"

Kenneth Kawamoto
 
When I tried to get the picture on the screen it is coming back as undefined, any idea what the problem would be?

Thanks again!

Peter
CCNA, Cisco Qualified Specialist
 
Well, it's hard to tell without seeing your code, but here's a working example:
Code:
var picNumber:Number = 0;

var pictureArray:Array = [{image:"images1/badger", text:"Badger"}, {image:"images1/butter", text:"Butter"}, {image:"images1/computer", text:"Computer"}];
var n:Number = pictureArray.length;
while (n) {
	var k:Number = Math.floor(Math.random()*n);
	--n;
	var tmp:Object = pictureArray[n];
	pictureArray[n] = pictureArray[k];
	pictureArray[k] = tmp;
}

var pictureCount:Number = 2;
var m:Number = pictureCount;
var pictureList:Array = new Array(m);
while (m) {
	--m;
	pictureList[m] = pictureArray[m];
}

one_btn.onRelease = function():Void  {
	var root:MovieClip = this._parent;
	var mc:MovieClip = root.createEmptyMovieClip("pic"+picNumber, root.getNextHighestDepth());
	with (mc) {
		_x = picNumber*200;
		_y = 100;
	}
	mc.loadMovie(pictureList[picNumber].image+".gif");

	if (++picNumber>=pictureCount) {
		delete this.onRelease;
	}
};

Kenneth Kawamoto
 
I managed to figure out why it was returning undefined. When the button is pressed to go to the next picture you have to press it twice before it goes to the next one in the array. It does this the way I have done it and I tried the way you stated above but both seem to do this?

Thanks a lot for the help :)

Peter
CCNA, Cisco Qualified Specialist
 
No it doesn't your right I must have done something wrong. Is it possible to delete the previous picture when you press the button to get the next picture?

Peter
CCNA, Cisco Qualified Specialist
 
[tt]removeMovieClip()[/tt] will remove a MovieClip (surprise!)

Also in AS1/2, I think if you create a new MovieClip in the depth occupied by some other object, that object will be deleted - IIRC.

Kenneth Kawamoto
 
Thanks, you were right about if deleting the previous object if it is replaced by another object on the same depth.

I am trying to get a button to check if the answer written in the text box is the same as the answer associated with the picture in the array

Code:
checkAnswer_btn.onRelease = function() {
		if (answer.text == pictureArray[m].text) {
			correct._visible = true;
		}
	};
I assume that I have the a problem with this part
pictureArray[m].text) {
I have tried a few differnt ways do you know what I am doing wrong?

Thanks so much for the help!

Peter
CCNA, Cisco Qualified Specialist
 
I tried it with picNumber but I still cant seem to get it to work

Peter
CCNA, Cisco Qualified Specialist
 
This is what I have done so far,

Code:
picNumber = 0;
//trace(picNumber);
correct._visible = false;
pictureArray = [{image:"images1/badger", text:"badger"}, {image:"images1/butter", text:"butter"}, {image:"images1/computer", text:"computer"}, {image:"images1/cooker", text:"cooker"}];

while (n) {
	var k:Number = Math.floor(Math.random()*n);
	--n;
	var tmp:Object = pictureArray[n];
	pictureArray[n] = pictureArray[k];
	pictureArray[k] = tmp;
}
trace("pictureArray: "+pictureArray);
var pictureCount:Number = 4;
var m:Number = pictureCount;
var pictureList:Array = new Array(m);
while (m) {
	--m;
	pictureList[m] = pictureArray[m];
	trace("pictureList: "+pictureList);
	trace(pictureArray[m].text);
	//holder_mc.loadMovie(pictureArray[m].image+".gif");
}
next_btn.onRelease = function() {
	correct._visible = false;
	var root:MovieClip = this._parent;
	var mc:MovieClip = root.createEmptyMovieClip("pic"+picNumber);
	holder_mc.loadMovie(pictureList[picNumber].image+".gif");
	
	if (++picNumber>=pictureCount) {
		delete this.onRelease;
	}
	
}
	
	checkAnswer_btn.onRelease = function() {
		if (answer.text == pictureArray[picNumber].text) {
			correct._visible = true;
		}
	}
thanks for your help

Peter
CCNA, Cisco Qualified Specialist
 
Code:
var picNumber:Number = 0;

correct._visible = false;

var pictureArray:Array = [{image:"images1/badger", text:"badger"}, {image:"images1/butter", text:"butter"}, {image:"images1/computer", text:"computer"}, {image:"images1/cooker", text:"cooker"}];

var n:Number = pictureArray.length;
while (n) {
	var k:Number = Math.floor(Math.random()*n);
	--n;
	var tmp:Object = pictureArray[n];
	pictureArray[n] = pictureArray[k];
	pictureArray[k] = tmp;
}

var pictureCount:Number = 4;
var m:Number = pictureCount;
var pictureList:Array = new Array(m);
while (m) {
	--m;
	pictureList[m] = pictureArray[m];
}

next_btn.onRelease = function() {
	correct._visible = false;
	holder_mc.loadMovie(pictureList[picNumber].image+".gif");

	if (++picNumber>=pictureCount) {
		delete this.onRelease;
	}
};

checkAnswer_btn.onRelease = function() {
	trace("Input: "+answer.text);
	trace("Answer: "+pictureArray[picNumber-1].text);
	if (answer.text == pictureArray[picNumber-1].text) {
		correct._visible = true;
	}
};
Make sure your input field is set to "single line". Also note that "cooker" and "cooker " aren't the same.

Kenneth Kawamoto
 
Can I use this to associate a sound clip to the picture and text also?

Thanks

Peter
CCNA, Cisco Qualified Specialist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top