-
function help - PLEASE
Hi,
I'm not that great at using functions yet, so I'm hoping someone can stear in the right direction...
Should I make the code below into a function to clean it up? If so how would I do that? Right now it is attached to a button. Thanks for your suggestions in advance.
Code:
on (release) {
_root.selected = nr;
with (this.content) {
if (_root.selected == 1) {
gotoAndPlay("one");
}
if (_root.selected == 2) {
gotoAndPlay("two");
}
if (_root.selected == 3) {
gotoAndPlay("three");
}
if (_root.selected == 4) {
gotoAndPlay("four");
}
if (_root.selected == 5) {
gotoAndPlay("ive");
}
if (_root.selected == 6) {
gotoAndPlay("six");
}
_root.click();
}
// set destinations
if (nr == 1) {
_root.x_values[0] = 0;
_root.x_values[1] = _root.x_values[0] + _root.content_width + _root.increment;
_root.x_values[2] = _root.x_values[1] + _root.increment;
_root.x_values[3] = _root.x_values[2] + _root.increment;
_root.x_values[4] = _root.x_values[3] + _root.increment;
_root.x_values[5] = _root.x_values[4] + _root.increment;
}
if (nr == 2) {
_root.x_values[0] = 0;
_root.x_values[1] = _root.x_values[0] + _root.increment;
_root.x_values[2] = _root.x_values[1] + _root.content_width + _root.increment;
_root.x_values[3] = _root.x_values[2] + _root.increment;
_root.x_values[4] = _root.x_values[3] + _root.increment;
_root.x_values[5] = _root.x_values[4] + _root.increment;
}
if (nr == 3) {
_root.x_values[0] = 0;
_root.x_values[1] = _root.x_values[0] + _root.increment;
_root.x_values[2] = _root.x_values[1] + _root.increment;
_root.x_values[3] = _root.x_values[2] + _root.content_width + _root.increment;
_root.x_values[4] = _root.x_values[3] + _root.increment;
_root.x_values[5] = _root.x_values[4] + _root.increment;
}
if (nr == 4) {
_root.x_values[0] = 0;
_root.x_values[1] = _root.x_values[0] + _root.increment;
_root.x_values[2] = _root.x_values[1] + _root.increment;
_root.x_values[3] = _root.x_values[2] + _root.increment;
_root.x_values[4] = _root.x_values[3] + _root.content_width + _root.increment;
_root.x_values[5] = _root.x_values[4] + _root.increment;
}
if (nr == 5) {
_root.x_values[0] = 0;
_root.x_values[1] = _root.x_values[0] + _root.increment;
_root.x_values[2] = _root.x_values[1] + _root.increment;
_root.x_values[3] = _root.x_values[2] + _root.increment;
_root.x_values[4] = _root.x_values[3] + _root.increment;
_root.x_values[5] = _root.x_values[4] + _root.content_width + _root.increment;
}
if (nr == 6) {
_root.x_values[0] = 0;
_root.x_values[1] = _root.x_values[0] + _root.increment;
_root.x_values[2] = _root.x_values[1] + _root.increment;
_root.x_values[3] = _root.x_values[2] + _root.increment;
_root.x_values[4] = _root.x_values[3] + _root.increment;
_root.x_values[5] = _root.x_values[4] + _root.increment;
}
}
-
Senior Member
Your code will also benefit from using arrays and loops, as well as functions.
Here's an implementation which uses an array for the frame labels, and a loop for setting up the desintation values in a more systematic way.
As you can see, by using arrays and loops, we can significantly shorten code by taking advantage of repetitive patterns.
code:
onClipEvent(load)
{
destLabels = ["zero","one","two","three","four","five","six"];
}
on (release)
{
_root.selected = nr;
this.content.gotoAndPlay(destLabels[nr]);
_root.click();
// set destinations
_root.x_values[0] = 0;
for (var i = 1; i <= 5; ++i) {
if (nr == i) {
_root.x_values[i] = _root.content_width + _root.x_values[i-1] + _root.increment;
}
else {
_root.x_values[i] = _root.x_values[i-1] + _root.increment;
}
}
}
Now, I'll take this rewritten code and turn it into a function, intended to be attached to the _root timeline. We can remove a lot of the '_root' references, because they will be assumed.
code:
destLabels = ["zero","one","two","three","four","five","six"];
makeSelection = function(nr)
{
selected = nr;
this.content.gotoAndPlay(destLabels[nr]);
click();
// set destinations
x_values[0] = 0;
for (var i = 1; i <= 5; ++i) {
if (nr == i) {
x_values[i] = content_width + x_values[i-1] + increment;
}
else {
x_values[i] = x_values[i-1] + increment;
}
}
}
Then your button code shortens to this:
code:
on (release)
{
_root.makeSelection(nr);
}
- Jim
-
Thanks for the code I think I'm starting to get it. I'm haveing one issue though. my "content" mc is burried a couple of levels down. I want to target it like the code below but its not working. Am I calling the mc correctly. I think I may have the "["square_"+nr]" part wrong...
Code:
_root.movie["square_"+nr].gotoAndPlay(destLabels[nr]);
Thanks again
Erik
-
Hi, this should work.
code:
_root.movie["square_"+(nr)].gotoAndPlay(destLabels[nr]);
Nick.
-
You again?!?!?!
nickh12 i don't think it matters if it has brackets or not....
it's the same thing....
and i might be wrong 'cause i didn't read the whole code....
BUT in the array u wrote zero, one etc etc. The gotoAndPlay() method requires numbers such as 1, 2 ,3 etc etc.... the second thing is that it's a string what u have inside the array and u need numbers....
so maybe change this:
code:
destLabels = ["zero","one","two","three","four","five","six"];
to this:
code:
destLabels = ["0","1","2","3","4","5","6"];
and write:
code:
_root.movie["square_"+nr].gotoAndPlay(Number(destLabels[nr]));
or u can create two arrays:
code:
destLabels = ["zero","one","two","three","four","five","six"];
destFrameNumbers = ["0","1","2","3","4","5","6"];
_root.movie["square_"+nr].gotoAndPlay(Number(destFrameNumbers[nr]));
but this might effect the rest of the code.... or not....
hope i helped....
Last edited by gunko2; 10-25-2004 at 12:59 PM.
It ain't broke, it just lacks duct tape.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|