Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 05-05-2012, 11:19 PM   #1
patawic
Registered User
 
Join Date: May 2010
Posts: 36
Default 61 dynamic button onclicks

Its probably an easy solution. But i have 61 buttons being generated for a level selection screen for a game i am making. and i need an onclick function for each of the buttons.

I would much prefer it if i didn't have to write a separate function for each of the 61 buttons and would prefer to do it in a loop or something. but every time i place it in a loop, i just get undefined outputs.


My current loop is this
Code:
			
for (var i = 1; i < onscreen.length; i++)
{
	trace(levels[onscreen[i]]);
	levels[onscreen[i]].onRelease = function():Void 
	{
		trace(i);
		loadTrack(onscreen[i]);
	};
}
The onscreen array contains names of every one of the buttons. and has a length of 62 (number 1 is the main template screen, not a button hence why the loop starts at 1, not 0). But for some reason, every button i click on
returns a value of 62.

Any help would be greatly appreciated.
patawic is offline   Reply With Quote
Old 05-06-2012, 05:38 AM   #2
patawic
Registered User
 
Join Date: May 2010
Posts: 36
Default

in basic terms. Im trying to rewrite this, to be in a neat function.
Code:
			//TUTORIAL
				levels["tut1"].onRelease = function() { loadTrack("tut1"); }
				levels["tut2"].onRelease = function() { loadTrack("tut2"); }
				levels["tut3"].onRelease = function() { loadTrack("tut3"); }
				levels["tut4"].onRelease = function() { loadTrack("tut4"); }
				levels["tut5"].onRelease = function() { loadTrack("tut5"); }
			//NOVICE
				levels["nov1"].onRelease = function() { loadTrack("nov1"); }
				levels["nov2"].onRelease = function() { loadTrack("nov2"); }
				levels["nov3"].onRelease = function() { loadTrack("nov3"); }
				levels["nov4"].onRelease = function() { loadTrack("nov4"); }
				levels["nov5"].onRelease = function() { loadTrack("nov5"); }
				levels["nov6"].onRelease = function() { loadTrack("nov6"); }
				levels["nov7"].onRelease = function() { loadTrack("nov7"); }
				levels["nov8"].onRelease = function() { loadTrack("nov8"); }
				levels["nov9"].onRelease = function() { loadTrack("nov9"); }
				levels["nov10"].onRelease = function() { loadTrack("nov10"); }	
				levels["nov11"].onRelease = function() { loadTrack("nov11"); }
				levels["nov12"].onRelease = function() { loadTrack("nov12"); }
				levels["nov13"].onRelease = function() { loadTrack("nov13"); }
				levels["nov14"].onRelease = function() { loadTrack("nov14"); }
				levels["nov15"].onRelease = function() { loadTrack("nov15"); }	
				levels["nov16"].onRelease = function() { loadTrack("nov16"); }
				levels["nov17"].onRelease = function() { loadTrack("nov17"); }
				levels["nov18"].onRelease = function() { loadTrack("nov18"); }
				levels["nov19"].onRelease = function() { loadTrack("nov19"); }
				levels["nov20"].onRelease = function() { loadTrack("nov20"); }	
				
			//INTERMEDIATE
				levels["int1"].onRelease = function() { loadTrack("int1"); }
				levels["int2"].onRelease = function() { loadTrack("int2"); }
				levels["int3"].onRelease = function() { loadTrack("int3"); }
				levels["int4"].onRelease = function() { loadTrack("int4"); }
				levels["int5"].onRelease = function() { loadTrack("int5"); }
				levels["int6"].onRelease = function() { loadTrack("int6"); }
				levels["int7"].onRelease = function() { loadTrack("int7"); }
				levels["int8"].onRelease = function() { loadTrack("int8"); }
				levels["int9"].onRelease = function() { loadTrack("int9"); }
				levels["int10"].onRelease = function() { loadTrack("int10"); }	
				levels["int11"].onRelease = function() { loadTrack("int11"); }
				levels["int12"].onRelease = function() { loadTrack("int12"); }
				levels["int13"].onRelease = function() { loadTrack("int13"); }
				levels["int14"].onRelease = function() { loadTrack("int14"); }
				levels["int15"].onRelease = function() { loadTrack("int15"); }	
				levels["int16"].onRelease = function() { loadTrack("int16"); }
				levels["int17"].onRelease = function() { loadTrack("int17"); }
				levels["int18"].onRelease = function() { loadTrack("int18"); }
				levels["int19"].onRelease = function() { loadTrack("int19"); }
				levels["int20"].onRelease = function() { loadTrack("nov20"); }	
			//EXPERT
				levels["exp1"].onRelease = function() { loadTrack("exp1"); }
				levels["exp2"].onRelease = function() { loadTrack("exp2"); }
				levels["exp3"].onRelease = function() { loadTrack("exp3"); }
				levels["exp4"].onRelease = function() { loadTrack("exp4"); }
				levels["exp5"].onRelease = function() { loadTrack("exp5"); }
				levels["exp6"].onRelease = function() { loadTrack("exp6"); }
				levels["exp7"].onRelease = function() { loadTrack("exp7"); }
				levels["exp8"].onRelease = function() { loadTrack("exp8"); }
				levels["exp9"].onRelease = function() { loadTrack("exp9"); }
				levels["exp10"].onRelease = function() { loadTrack("exp10"); }	
			//CHALLANGE
				levels["cha1"].onRelease = function() { loadTrack("cha1"); }
				levels["cha2"].onRelease = function() { loadTrack("cha2"); }
				levels["cha3"].onRelease = function() { loadTrack("cha3"); }
				levels["cha4"].onRelease = function() { loadTrack("cha4"); }
				levels["cha5"].onRelease = function() { loadTrack("cha5"); }			
			//MASTER
				levels["mas1"].onRelease = function() { loadTrack("mas1"); }
patawic is offline   Reply With Quote
Old 05-06-2012, 05:57 AM   #3
tacos
Grrrrrrrrreat!
 
tacos's Avatar
 
Join Date: Apr 2007
Location: Montréal, Québec
Posts: 5,473
Default

Senocular describes here what's happening and the solution to it...

http://www.senocular.com/flash/tutor...#loopfunctions
__________________
WHO I WAS...
BANDS OF THE 80's
tacos is offline   Reply With Quote
Old 05-06-2012, 06:02 AM   #4
patawic
Registered User
 
Join Date: May 2010
Posts: 36
Default

Quote:
Originally Posted by tacos View Post
Senocular describes here what's happening and the solution to it...
thankyou so much!
patawic is offline   Reply With Quote
Old 05-06-2012, 03:17 PM   #5
tacos
Grrrrrrrrreat!
 
tacos's Avatar
 
Join Date: Apr 2007
Location: Montréal, Québec
Posts: 5,473
Default

Grrrrrrrreat!
__________________
WHO I WAS...
BANDS OF THE 80's
tacos 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 10:19 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.