PDA

View Full Version : [FMX] Call back a function



dboers
March 31st, 2004, 01:09 PM
Hi,

I have a made function to control the alpha of a few squares. The function looks like this:

function fadeSq (clip, fade, speed){
clip.onEnterframe= function(){
var fadeOut = fade - this._alpha;
this._alpha += fadeOut /speed;
}
}

I know that I can call that function for one mc with:

fadeSq (square1, 0, 4);

But is it also posibe to call the function for let's say seven mc's at once. I mean without writing seven lines of scrip?

Thanks in advance ;)

kode
March 31st, 2004, 01:18 PM
If those movie clips are named "square1", "square2", "square3", etc. you can use a for statement to define the onEnterFrame event handler for all of the squares:

function fadeSq(total, fade, speed) {
for (var i = 1; i<=total; i++) {
this["square"+i].onEnterFrame = function() {
var fadeOut = fade-this._alpha;
this._alpha += fadeOut/speed;
};
}
}
fadeSq(7, 0, 4);
If the instance names of the movie clips are different, I'd use something along these lines:

function fadeSq(clips, fade, speed) {
while (clips.length) {
clips[0].onEnterFrame= function() {
var fadeOut = fade-this._alpha;
this._alpha += fadeOut/speed;
};
clips.splice(0, 1);
}
}
fadeSq([square1, square2, other_mc, etc], 0, 4);

dboers
March 31st, 2004, 01:40 PM
Hi kode,

I used this one:
function fadeSq(clips, fade, speed) {
while (clips.length) {
clips[0].onEnterFrame= function() {
var fadeOut = fade-this._alpha;
this._alpha += fadeOut/speed;
};
clips.splice(0, 1);
}
}
fadeSq([square1, square2, other_mc, etc], 0, 4);


and it works perfect :)

But can you, if you have some time please explain this code , I don'd see an array created, but as far as I see it there is an array in the call function. And what is the clips.splice(0, 1); doing. I thank you very much for what you accomplished for me but I also try to understand it :huh:

kode
March 31st, 2004, 02:02 PM
Yeah, the array is passed to the function through the parameter "clips", this is an array containing a reference to the movie clips you want to fade. The function then runs a loop (by using a while statement) to execute the code until there are no more elements in the array, defining the onEnterFrame handler for all the movie clips in the array. :)

dboers
April 1st, 2004, 12:25 AM
I would like to thank you again! For me it is important to understand the things I'm doing. It's easy to just cut and paste the codes provided, but that's not what I want. So thanks again :)

kode
April 1st, 2004, 02:59 AM
You're welcome, dboers. Glad I could help! :P

dboers
April 1st, 2004, 04:30 AM
I have one last thing I don't know how to solve :puzzled:

I have the following code to rescale one thumb and to fade out the other thumbs:

function scaleThumb (clip, wijd, hoog, speed){
clip.onEnterFrame = function(){
var eindW = wijd - this._width;
this._width += eindW / speed;
var eindH = hoog - this._height;
this._height += eindH / speed;
}
}
function fadeThumb (clips, alpha, speed){
while (clips.length) {
clips[0].onEnterFrame = function(){
var eindA = alpha - this._alpha;
this._alpha += eindA / speed;
}
clips.splice(0, 1);
}
}
but1.onRelease = function(){
scaleThumb (this, 380, 271, 4);
fadeThumb ([but2, but3, but4, but5, but6], 0, 4);
}

and that is working fine. :thumb:

But I have close button with the purpose to reverse the above code. I tried the following:

butClose.onRelease = function(){
scaleThumb (but1, 120, 90, 4);
fadeThumb ([but2, but3, but4, but5, but6], 100, 4);
}

But that isn't working :( At least, only one of the two is working, depending of witch one is on top. What am I doing wrong :h:

kode
April 1st, 2004, 10:39 AM
I really don't see anything wrong in the code. Could you attach your FLA? :-/

dboers
April 1st, 2004, 11:02 AM
I just uploaded the files here is the link:



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

kode
April 1st, 2004, 11:05 AM
Ok, I'm downloading the file now... I'll check it out in a few minutes. ;)

kode
April 1st, 2004, 12:07 PM
I'm taking a slightly different approach, I think this is what you wanted to achieve...

function scaleAndFadeThumbs(width, height, alpha, speed) {
//loop through the items in the array "thumbs"
for (var prop in thumbs) {
if (thumbs[prop] != selected) {
//if the current item is not equal to the selected thumbnail, fade it
thumbs[prop].onEnterFrame = function() {
var adiff = alpha-this._alpha;
this._alpha += adiff/speed;
if (Math.abs(adiff)<1) {
this._alpha = alpha;
delete this.onEnterFrame;
}
};
} else {
//the current item is equal to the selected thumbnail, resize it
thumbs[prop].onEnterFrame = function() {
var wdiff = width-this._width;
this._width += wdiff/speed;
var hdiff = height-this._height;
this._height += hdiff/speed;
if (Math.abs(wdiff)<1 && Math.abs(hdiff)<1) {
this._width = width;
this._height = height;
delete this.onEnterFrame;
}
};
}
}
}
//define array containing the thumbnails
var thumbs = [but1, but2, but3, but4, but5, but6];
//variable to store the selected thumbnail
var selected = null;
//assign the onRelease handler to the thumbnails
for (var prop in thumbs) {
thumbs[prop].onRelease = function() {
//set the selected variable
selected = this;
scaleAndFadeThumbs(380, 271, 0, 4);
};
}
btClose.onRelease = function() {
//reset thumbs
scaleAndFadeThumbs(120, 90, 100, 4);
};
...Any questions? :P

dboers
April 1st, 2004, 12:22 PM
That's quit a story ;) And it is working :) Right now I (shame me) just copy and paste it, but i'm going to have a look at the code tommorow morning .

How can I thank you :h: If you're ever in Cyprus, please let me know and I will invite you for an ouzo :beer: or whatever you like

kode
April 1st, 2004, 12:31 PM
You're welcome, dboers. Don't hesitate to post your questions if you don't understand some part of the code. ;)

And thanks for the offer. I don't know what the hell an ouzo is, but as long as it contains alcohol... I'm up for it. :P

dboers
April 1st, 2004, 12:34 PM
Thank you for the offer about the code :) Ouzo is a Greek/Cypriote drink. A little bit like French pernod, and after a few classes you start to speek and sing greek ! I think ;) And yes as you understant it contains alcohol

kode
April 1st, 2004, 12:39 PM
So... what are we waiting for?! Let's go get drunk!! :P

dboers
April 1st, 2004, 12:41 PM
Good idea :beer: I hear from you :) Cheers

dboers
April 2nd, 2004, 12:57 AM
Hi Kode,

I had a look at the code and everything is bright and clear. The only two problems that are left are the close button witch I want to fade in and out so inserted the following function:

function fadeBut (clip, alpha, speed){
clip, onEnterFrame = function(){
var afade = alpha - this._alpha;
this._alpha += afade /speed;
}
}

I called: fadeBut(btClose, 100, 10); in the thumbs[prop].onRelease

and (this, 0, 10); in the onRelease from the button itself.

But that isn't working at all. First of all the button didn't appear after I had set it's property to alpha 0. To test if it was working the other way arround, I reversed the alpha settings back to 100. But clicking on btClose gave as result that not only btClose, but also the thumbs disappeared. So the new made fuction is in conflict with the function you've made and I don't know another way to integrate a function

The second problem encounterd is that with every thumb comes an external txt file. How do I integrate the loadVars in the thumbs[prop].onRelease.
When it turns out that the function are in conflicy with eachother, can you please try to explain to me how, in the future I can work arround such a problem ;) variable name of the textfield is txt_site

kode
April 2nd, 2004, 03:07 AM
First off, you must turn the symbol "btClose" from a button into a movie clip, so that you can use the onEnterFrame event handler.

And there's a little typo in this line:
clip, onEnterFrame = function() {
There should be a period instead of a comma, and no space:
clip.onEnterFrame = function() {

Other than that, I think it should work fine.


As for the text file thing, you could assign the URL to the text file in a variable, and then call the LoadVars.load() method within the onRelease event handler:

//define the loadvars object to handle the text files
var my_lv = new LoadVars();
var thumbs = [but1, but2, but3, but4, but5, but6];
but1.fileurl = "somefile.txt";
but2.fileurl = "anotherfile.txt";
//and so on...
var selected = null;
for (var prop in thumbs) {
thumbs[prop].onRelease = function() {
selected = this;
scaleAndFadeThumbs(380, 271, 0, 4);
//load text file assigned to the thumbnail
my_lv.load(this.fileurl);
};
}
btClose.onRelease = function() {
scaleAndFadeThumbs(120, 90, 100, 4);
};

dboers
April 2nd, 2004, 04:21 AM
Hi Kode,

First of all thanks for you quik reply :)
Stupid, Stupid, stupid about that button. I think I started to early this morning. So that is solved. The explanation about the txt is also very clear I'm going to work it out and will sent you the link when everything is ready. Can I sent it to an email address or you prefer to receive it here ?

kode
April 2nd, 2004, 04:25 AM
No problem. ;)

...And why don't you post the link in the Showcase (http://www.kirupaforum.com/forums/forumdisplay.php?f=49) section, so we all can see it? :)

dboers
April 2nd, 2004, 04:32 AM
That's also an idea ;) By theway are you working in Multimedia ? And so yes what are you doing in it? :sketchy:

kode
April 2nd, 2004, 04:38 AM
Nah! My job has nothing to do with this, this is just a hobby... :P
I used to have lots of free time, you know, so I needed something to spend my time with. ;)

dboers
April 2nd, 2004, 04:42 AM
And what is your job then? If I may be so free :nerd:

kode
April 2nd, 2004, 04:47 AM
I take care of my father's accounting and administration (he owns restaurants), that kind of boring stuff. :)

dboers
April 2nd, 2004, 04:51 AM
So you've made a site for that restaurants? And if yes what is the address. One more (or actualy two) questions. Where you stay in Mexico and how is tourism over there ?

dboers
April 2nd, 2004, 05:01 AM
One last question about the txt files:

I just made a txt file named teacher.txt
I changed the code but1.fileurl = "somefile.txt"; to but1.fileurl = "teacher.txt";

But nothing happens. Does that have to do with the variable name? The text file looks like:

txt_site=blablabla etc ;)

kode
April 2nd, 2004, 05:18 AM
So you've made a site for that restaurants? And if yes what is the address. One more (or actualy two) questions. Where you stay in Mexico and how is tourism over there ?
No, not really. I've been thinking about it, though, probably some time soon. :)

I live in Irapuato, Guanajuato. It's a nice little town, but there's not much tourism around here. :-/

One last question about the txt files:

I just made a txt file named teacher.txt
I changed the code but1.fileurl = "somefile.txt"; to but1.fileurl = "teacher.txt";

But nothing happens. Does that have to do with the variable name? The text file looks like:

txt_site=blablabla etc ;)
Well, yeah... nothing was supposed to happen with that code. It was just an example.

You have to define the onLoad event handler of the loadvars object, so you can use the variable contained in the text file once that the file has been loaded into Flash.

For example, let's say that you want to display the content of the text file in a text field named "my_txt":

//define the loadvars object to handle the text files
var my_lv = new LoadVars();
my_lv.onLoad = function(success) {
if (success) {
my_txt.text = this.txt_site;
}
};
var thumbs = [but1, but2, but3, but4, but5, but6];
but1.fileurl = "teacher.txt";
var selected = null;
for (var prop in thumbs) {
thumbs[prop].onRelease = function() {
selected = this;
scaleAndFadeThumbs(380, 271, 0, 4);
//load text file assigned to the thumbnail
my_lv.load(this.fileurl);
};
}
btClose.onRelease = function() {
scaleAndFadeThumbs(120, 90, 100, 4);
};

dboers
April 2nd, 2004, 05:42 AM
yes it's working :) Great

About the questions I asked you before !! My girfried and I having websites that are in to tourism. A few years ago we claimed the name www.worldholidayguide.com (http://www.worldholidayguide.com/) with the idea to set up a site providing tourism information. Since then we claimed about 14 more names:
thailandholidayguide.com, cyprusholidayguide.com, hellasholidayguide.com, italyholidayguide.com, maltaholidayguide.com, portugalholidayguide.com, indiaholidayguide.com, vietnamholidayguide.com, englandholidayguide.com, usholidayguide.com, and even yesterday mexicoholidayguide.com and hollandholidayguide.com

We had and still have the idea to set up a chain of websites providing tourism info about a particular country and then trying to frenchise them. We already had succes with www.thailandholidayguide.com (http://www.thailandholidayguide.com/) and www.cyprusholidayguide.com (http://www.cyprusholidayguide.com/) and now we're having talks about portugal and malta. So that's why I asked about the tourim in Mexico because as I told you I also claimed that name yesterday ;)

dboers
April 2nd, 2004, 07:11 AM
the html codes are not activated, while that opion is enabled in Flash ?

kode
April 2nd, 2004, 01:32 PM
You must assign the HTML formatted text to the text field thorugh the TextField.htmlText property:

my_txt.htmlText = this.txt_site;

dboers
April 2nd, 2004, 10:18 PM
I learned a lot today :) It's working perfect. Many thanks :)

kode
April 2nd, 2004, 10:20 PM
You're welcome. And good luck with your project! ;)

dboers
April 3rd, 2004, 05:00 AM
As said everything is working perfect. I only have a intro.txt what I want to appear as soon as the movie portfolio opens. I already integrated it on the close button with


my_lv.load(this.fileurl);
and that is working

So if anybody press the close button the text from one of the site will be replaced with intro.txt