PDA

View Full Version : simplify repeating script


wfz
11-18-2008, 04:01 AM
I'm trying to use loop to simplify the button script below:

topic0.onRollOver = function() {
tHint.text = TopicHintList[0];
tHint._x = topic0._x;
tHint._y = 15;
topic0.gotoAndStop(2);
};
topic0.onRollOut = function() {
tHint._x = -100;
tHint._y = -1000;
topic0.gotoAndStop(1);
};
topic0.onPress = function() {
topic0.gotoAndStop(3);
};
topic0.onRelease = function() {
gotoAndPlay(TopicBeginFrameList[0]);
};

topic1.onRollOver = function() {
tHint.text = TopicHintList[1];
tHint._x = topic1._x;
tHint._y = 15;
topic1.gotoAndStop(2);
};
topic1.onRollOut = function() {
tHint._x = -100;
tHint._y = -1000;
topic1.gotoAndStop(1);
};
topic1.onPress = function() {
topic1.gotoAndStop(3);
};
topic1.onRelease = function() {
gotoAndPlay(TopicBeginFrameList[1]);
};
...


The main changes are the topic0, etc change from 0 to 1 to 2, etc.

I created one based on what I read here but it didn't work. Can you tell me what's wrong with the script below?

for (var i = 0; i<TopicBeginFrameList.length; i++) {
this["topic"+i].onRollOver = function() {
tHint.text = TopicHintList[this(i)];
tHint._x = this["topic"+i]._x;
tHint._y = 15;
this["topic"+i].gotoAndStop(2);
};
this["topic"+i].onRollOut = function() {
tHint._x = -100;
tHint._y = -1000;
this["topic"+i].gotoAndStop(1);
};
this["topic"+i].onPress = function() {
this["topic"+i].gotoAndStop(3);
};
this["topic"+i].onPress = function() {
this["topic"+i].gotoAndStop(3);
};
this["topic"+i].onRelease = function() {
gotoAndPlay(TopicBeginFrameList[this(i)]);
};
}

I really don't know how to describe (TopicBeginFrameList[0]) with loop. something like this "TopicHintList[this(i)];" ???


Thanks for the help.

Yui
11-18-2008, 07:58 AM
you have to use event listeners for those events.... although i can't help you much in AS2 =/

DJRoberts
11-18-2008, 08:27 AM
Have you tried using an "onEnterFrame" loop, then deleting the onEnterFrame property when you want the loop to stop?

wfz
11-18-2008, 03:22 PM
you have to use event listeners for those events.... although i can't help you much in AS2 =/

What would it be like if written in AS3? Do you have a link? Maybe that's the reason to learn AS3.
Thanks!

wfz
11-18-2008, 03:23 PM
Have you tried using an "onEnterFrame" loop, then deleting the onEnterFrame property when you want the loop to stop?
I'm not familiar with using onEnterFrame loop. Can you elaborate? THANKS!

DJRoberts
11-19-2008, 09:08 AM
From what I understand you want a script that dynamically creates buttons, which carryout almost the same tasks when pressed.

So I compiled this as an example. Hope it helps:

wfz
11-19-2008, 05:40 PM
Thanks for the code. This is great and dynamic generated button is what I need too. Currently it does the same thing for all buttons, how do I get it to do different things for each button? Maybe get the value of i and use if statements in buttonpress function?

Thanks,

DJRoberts
11-20-2008, 08:55 AM
I attached some code which should do what you are after. . .