Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 2.0

Reply
 
Thread Tools Rate Thread Display Modes
Old 12-17-2004, 01:37 AM   #1
bittman
Registered User
 
Join Date: Dec 2004
Posts: 11
Default Array index variable not stored in dynamically created buttons --onRelease() problem?

Is there any way to pass a variable when calling the onRelease() function? In the example below, I am dynamically creating a series of rows/buttons and would like to dynamically load additional text from an array when clicking the row/button.

The problem is...the value for "i" is not being stored when the button behavior is defined. In the case below, "i" always has a value of "4" -- which traces undefined. I need "i" to be stored as "0", "1", "2", etc. when I define the button behavior.

Any help would be greatly appreciated...including a different way of doing it


Code:
//Loads easing
var easeType = mx.transitions.easing.Strong.easeOut;

//Expands view
contentHolder.showRows.onRelease = function(){
	myTween = new mx.transitions.Tween(contentHolder, "_X", easeType, -445, 35, 10);	
}

//Dummy data for testing
var myARR = new Array(
				{city:"New York", description:"MTA raises subway fares", date:"2005", contentT:"Blah, blah, blah"},
				{city:"Los Angeles", description:"Traffic continues to be a problem", date:"2004", contentT:"Blah, blah, blah"},
				{city:"London", description:"Will the rain ever stop?", date:"2005", contentT:"Blah, blah, blah"},
				{city:"Paris", description:"Top 10 things to do", date:"2003", contentT:"Blah, blah, blah"}
				);
//Vertical starting position for the first button
vertPosition=0;

//Dynamically create rows of buttons.
for (var i = 0; i < myARR.length; i++) {

	contentHolder.scrollWindow.spContentHolder.attachMovie("singleRow", "singleRow"+i+"_mc", contentHolder.scrollWindow.spContentHolder.getNextHighestDepth(), {_x:0, _y:vertPosition});
	contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].cityText = myARR[i].city;
	contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].descriptionText = myARR[i].description;
	contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].dateText = myARR[i].date;

	//Assign actions to each button
	contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].rowButton.onRelease = function(){
		//PROBLEM HERE -- "i" not being stored. The value of "i" gets looked up when calling the function.
		contentHolder.descriptionText2 = myARR[i].description;
		contentHolder.contentText = myARR[i].contentT;
		myTween = new mx.transitions.Tween(_root.contentHolder, "_X", easeType, 0, -445, 10);
	}
	vertPosition=vertPosition+24;	
}

stop();
bittman is offline   Reply With Quote
Old 12-17-2004, 02:03 AM   #2
uten
Registered User
 
Join Date: May 2004
Posts: 175
Default

dude,

because your event onRelease is triggered only after the for-loop, it always gets the current value of i... which after the loop is 4? or 3 since it stops less than your array length.

anyhow, define and store the i value before and outside of your onRelease event handler.

ActionScript Code:
contentHolder.scrollWindow.spContentHolder.attachMovie("singleRow", "singleRow"+i+"_mc", contentHolder.scrollWindow.spContentHolder.getNextHighestDepth(), {_x:0, _y:vertPosition});     contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].cityText = myARR[i].city;     contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].descriptionText = myARR[i].description;     contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].dateText = myARR[i].date;     [B]contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].iValue= i;[/B]     // then call in event handler contentHolder.scrollWindow.spContentHolder["singleRow"+i+"_mc"].rowButton.onRelease = function(){                 contentHolder.descriptionText2 = myARR[iValue].description;         contentHolder.contentText = myARR[iValue].contentT; }

hope i helped.
jon
uten is offline   Reply With Quote
Old 12-17-2004, 02:10 AM   #3
uten
Registered User
 
Join Date: May 2004
Posts: 175
Default

havent tested my solution.. i think you need to rewrite the code inside event handler as: myARR[this._parent.iValue].description;

hope it works. tell me what you think.

jon

Last edited by uten; 12-17-2004 at 02:12 AM.
uten is offline   Reply With Quote
Old 12-17-2004, 03:00 PM   #4
bittman
Registered User
 
Join Date: Dec 2004
Posts: 11
Default

Yep...your last addition did the trick. By calling "iValue" directly it was looking on the "_root" level. You have to use "myARR[this._parent.iValue].description;"

Thank you for the fix!!! It was driving me crazy
bittman is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:39 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.