PDA

View Full Version : [FMX]Desperately seeking help! nested loop



dboers
April 27th, 2004, 02:26 AM
Hi all,

Thanks to scotty, :thumb: I have the following code to control the x position of one single movieclip (company_mc) with five button mc's (btn_corporate, btn_dimitriou, btn_cars, btn_cycles, btn_hertz):


function movoClip(x) {
company_mc.onEnterFrame = function() {
this._x += (x-this._x)/5;
};
}
var content = ["corporate", "dimitriou", "cars", "cycles", "hertz"];
for (i = 0; i < content.length; i++) {
this["btn_"+content[i]].i = i;
clip = this["btn_"+content[i]];
clip.onRollOver = function() {
new Color(this).setRGB(0x666666);
}
clip.onRollOut = function () {
if (!this.pushed){
new Color(this).setRGB(0xFFFFFF);
}else{
new Color(this).setRGB(0x666666);
}
}
clip.onRelease = function () {
_root.content_company.resetBut();
this.pushed = true;
new Color(this).setRGB(0xFFFFFF);
movoClip(-this.i*700);
}
}
function resetBut() {
for (var i in this) {
if (this[i]._name.substr(0, 4) == "btn_") {
var clip = eval(this[i]._name);
clip.pushed = false;
new Color(clip).setRGB(0xFFFFFF);
}
}
}

Now I also want to control tha alpha fade for 5 photo_mc's (photo_corporate, photo_dimitriou, etc etc) So if btn_corparate is pussed photo_corporate should go to alpha 100 and the others to alpha 0. I think it has something to do with a nested loop, but I'm not sure and also don't know how to implement such a nested loop insite the existing loop.

Is there somebody who can push me in the right direction :ne:

m_andrews808
April 27th, 2004, 04:42 AM
Hi there

okay I've looked over the code you posted briefly but I'd do it a little differently myself. First off create a custom method called easeClip:


Object.prototype.easeClip = function(Param, TargetValue, Rate, SnapAccuracy) {
this[Param] += (TargetValue - this[Param]) / Rate;
if(Math.abs(this[Param] - TargetValue) < SnapAccuracy) {
this[Param] = TargetValue;
return true;
}
return false;
}

this method will let you ease any property or value of a movie clip/btn/texfield etc. from its current value to a target value much like the code you've already posted. Here's how to use it (lets pretend your 5 buttons are called btn1 to btn5):



Object.prototype.easeClip = function(Param, TargetValue, Rate, SnapAccuracy) {
this[Param] += (TargetValue - this[Param]) / Rate;
if(Math.abs(this[Param] - TargetValue) < SnapAccuracy) {
this[Param] = TargetValue;
return true;
}
return false;
}

btn1.TargetAlpha = btn2.TargetAlpha = btn3.TargetAlpha = btn4.TargetAlpha = btn5.TargetAlpha = 50;

this.onEnterFrame = function() {
btn1.easeClip("_alpha", btn1.TargetAlpha, 5, 5);
btn2.easeClip("_alpha", btn2.TargetAlpha, 5, 5); btn3.easeClip("_alpha", btn2.TargetAlpha, 5, 5); btn4.easeClip("_alpha", btn2.TargetAlpha, 5, 5); btn5.easeClip("_alpha", btn2.TargetAlpha, 5, 5);
}

btn1.onRelease = btn2.onRelease = btn3.onRelease = btn4.onRelease = btn5.onRelease = function() {
btn1.TargetAlpha = btn2.TargetAlpha = btn3.TargetAlpha = btn4.TargetAlpha = btn5.TargetAlpha = -10;
this.TargetAlpha = 110;
}


thats just how I'd do it, tho there may be better ways. Let me know if you're still having problems

dboers
April 27th, 2004, 04:59 AM
Hi thank you for your responce :) This looks very difficult to me. I'm not that long in AS as jet. So there are some questions.


First of all where doe I tell that when a certain button is pressed that a certain photo needs to go to alpha 100, while the others have to go to alpha 0

and secondly where do i integrate the part I already have for the x position of company_mc

I know :ne: this are a lot of questins but, what you told me is completely new to me

m_andrews808
April 27th, 2004, 05:01 AM
ah sorry, I didn't read your post properly, I thought the buttons had to fade, not the photos, I've got to dash to a meeting now but I'll be back in a bit to answer your questions... ;)

dboers
April 27th, 2004, 05:07 AM
Thanks a lot ;)

dboers
April 28th, 2004, 01:04 AM
From Ilyas da Pom I have the following code to make the photo mc's fade in and out, depending on which button was pressed:


var currentPhoto;
var currentTitel;
for (var i in this) {
if (this[i]._name.substr(0, 4) == "btn_") {
setKnop(this[i]._name);
}
}
MovieClip.prototype.alpha = function(a) {
this.onEnterFrame = function() {
this._alpha += (a - this._alpha)/5;
if (this._alpha > a-1 && this._alpha < a+1) {
delete this.onEnterFrame;
}
};
};
function setKnop(clip) {
var clip = eval(clip);
clip.myTitel = _root["title_" + clip._name.substr (4, clip._name.length)];
clip.myPhoto = _root["photo_" + clip._name.substr (4, clip._name.length)];
clip.onRollOver = function() {
new Color(this).setRGB(0xA10000);
};

clip.onRollOut = function() {
if (!this.pushed) {
new Color(this).setRGB(0xFFFFFF);
} else {
new Color(this).setRGB(0xA10000);
}
};

clip.onRelease = function() {
_root.resetKnop();
this.pushed = true;
this.useHandCursor = false;
new Color(this).setRGB(0xA10000);
pagina(this._name.substr(4));

delete btn_company.onEnterFrame;
delete title_company.onEnterFrame;
_root.title_company.gotoAndStop(1);
_root.photo_company.alpha (0);

if (currentPhoto != this.myPhoto){
if (currentPhoto) currentPhoto.alpha (0);
this.myPhoto.alpha (99);
currentPhoto = this.myPhoto

if (currentTitel != this.myTitel) {
if (currentTitel) currentTitel.gotoAndStop (1);
this.myTitel.gotoAndStop (2) ;
currentTitel = this.myTitel ;
}
}
};
}
function resetKnop() {
for (var i in this) {
if (this[i]._name.substr(0, 4) == "btn_") {
var clip = eval(this[i]._name);
clip.pushed = false;
new Color(clip).setRGB(0xFFFFFF);
this[i].useHandCursor = true;
}
}
}

Is there please somebody who can tel me how to combine this with the code I get from scotty ?

scotty
April 28th, 2004, 09:38 AM
Can you post a fla/zip?

scotty(-:

dboers
April 28th, 2004, 09:53 AM
I'm working on in now:)

dboers
April 28th, 2004, 10:18 AM
Here is the link. I already included the alpha function. I like the photo's to fade related to on which button was pressed. I was thinking of using another Array, but didn't know how to integrate it in your function. And I prefer to continue with the function you gave me instead of doing it a complete other way again.

http://www.testfolder.com/company.htm

Thank you in advance

scotty
April 28th, 2004, 11:04 AM
Here you go, looks nice btw:)
Have a look in the fadeFoto function, I think it's clear else let me know.

scotty(-:

dboers
April 28th, 2004, 11:15 AM
It works :) Scotty thanks a lot. I was looking and looking, but couldn't find the right solution.


Again thanks a lot :)

scotty
April 28th, 2004, 11:21 AM
no problem dboers:thumb:

dboers
April 28th, 2004, 11:53 PM
Hi scotty,


I have on last (yes realy :ne: ) question. I just made 5 little pen animations (animatie_een, animatie_twee etc), which are related to the 5 buttons (the Fla I posted yesterday).

All animations(7 fames) have a stop in fames 1 and 7. When I roll over one of the buttons I want the related animation to start play at frame 2, on roll out the related animation should go back to frame 1 and on release it should go to frame 7. Releasing one of the other buttons should send the previous button also back from frame 7 to 1.

Is it very difficult to integrate this in the existing script :sigh:

scotty
April 29th, 2004, 03:11 AM
Hmmm, shouldn't be that hard:)
Can you upload the fla with the animations?
Makes it more easy to test it;)

scotty(-:

dboers
April 29th, 2004, 03:41 AM
Hi,



I just oploaded the Fla again, you can use the same link as before. It is about the little arrow animations left from the buttons.



Thanks in advance

scotty
April 29th, 2004, 04:39 AM
Hi dboers, here's your new fla. I made another array for the pijl-animations, so it's easy to get access to them.
Hope this is what you're after:)

scotty(-:

dboers
April 29th, 2004, 06:29 AM
Hi scotty,


Thanks a lot for all you've done. :thumb: When I see it it all looks so easy, but the problem is to combine the right things at the right moment and that is confusing me most of the time.

Everything is working great :) Thanks again

scotty
April 29th, 2004, 06:36 AM
welcome again=)