Re: [Flashcoders] calling an "event" function with a parameter that is a number
Agreed! 2009/5/24 Steven Sacks > The latter is cleaner and better, IMO. > > Write a function that your event handler calls. Then, you can call it from > elsewhere and from your button click handler. > > function onThumbClick(event:MouseEvent):void > { >loadImage(event.target.name); > } > function loadImage(num:int):void > { > // loadImage(num); > > } > > Juan Pablo Califano wrote: > >> You have a couple of options: >> >> You could add an optional parameter to the function that handles the >> click, >> like this: >> >> function clickThumb(e:MouseEvent,index:int = 0):void{ >> >> } >> >> Setting a default value for the index parameter allows the function to be >> called without passing it. That's what will happen when a thumb is >> clicked. >> >> When you want to call the function directly, you'd do: >> >> clickThumb(null,image_num + 1); >> >> In clickThumb, check whether the event object is null or not. If it's not, >> do what you're currently doing. If it's null, use the index parameter as >> the >> index into my_images. >> >> >> Another thing you could do is a bit of refactoring. >> >> Have the click handler only resolve the index and calling another function >> to actually load the image. >> >> function clickThumb(e:MouseEvent):void { >> loadImage(e.target.name); >> } >> >> function loadImage(index:int):void { >>if (my_full_images[index] == undefined){ >> var full_url = my_images[inde...@full; >> ... etc ... >> } >> >> Then, from your other button's handler, call loadImage(image_num + 1); >> >> >> Cheers >> Juan Pablo Califano >> >> 2009/5/23 Isaac Alves >> >> Hello! >>> In my code there's a function that shows an image on the screen (or load >>> the >>> image and show a progress bar) that is called by clicking on a thumbnail, >>> and uses a parameter [e.target.name] refering to the property "name" of >>> the >>> thumbnail clicked, which is an integer. The thumbnails are placed inside >>> the >>> cointaner_mc MovieClip. >>> >>> container_mc.addEventListener(MouseEvent.CLICK, clickThumb); >>> >>> function clickThumb(e:MouseEvent):void{ >>> if (my_full_images[e.target.name] == undefined){ >>> var full_url = my_images[e.target.nam...@full; >>> >>> etc... >>> >>> but I want to call this function by clicking on a button, which would >>> need >>> to pass another type of parameter, in this case, a variable which refers >>> to >>> the number of the image displayed on the screen plus one. >>> >>> I tried this: >>> >>> clickThumb(image_num + 1) >>> >>> But it doesn't work, I get the following error: >>> >>> 1067: Implicit coercion of a value of type Number to an unrelated type >>> flash.events:MouseEvent. >>> >>> How could I call the function clickThumb and make it use "image_num + 1" >>> instead of "e.target.name" ? >>> >>> Could I dispatch the event (MouseEvent.CLICK, clickThumb), passing this >>> value "image_num + 1" ? >>> >>> Thanks in advance !! >>> Isaac >>> ___ >>> Flashcoders mailing list >>> Flashcoders@chattyfig.figleaf.com >>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >>> >>> ___ >> Flashcoders mailing list >> Flashcoders@chattyfig.figleaf.com >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >> >> ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] calling an "event" function with a parameter that is a number
The latter is cleaner and better, IMO. Write a function that your event handler calls. Then, you can call it from elsewhere and from your button click handler. function onThumbClick(event:MouseEvent):void { loadImage(event.target.name); } function loadImage(num:int):void { // loadImage(num); } Juan Pablo Califano wrote: You have a couple of options: You could add an optional parameter to the function that handles the click, like this: function clickThumb(e:MouseEvent,index:int = 0):void{ } Setting a default value for the index parameter allows the function to be called without passing it. That's what will happen when a thumb is clicked. When you want to call the function directly, you'd do: clickThumb(null,image_num + 1); In clickThumb, check whether the event object is null or not. If it's not, do what you're currently doing. If it's null, use the index parameter as the index into my_images. Another thing you could do is a bit of refactoring. Have the click handler only resolve the index and calling another function to actually load the image. function clickThumb(e:MouseEvent):void { loadImage(e.target.name); } function loadImage(index:int):void { if (my_full_images[index] == undefined){ var full_url = my_images[inde...@full; ... etc ... } Then, from your other button's handler, call loadImage(image_num + 1); Cheers Juan Pablo Califano 2009/5/23 Isaac Alves Hello! In my code there's a function that shows an image on the screen (or load the image and show a progress bar) that is called by clicking on a thumbnail, and uses a parameter [e.target.name] refering to the property "name" of the thumbnail clicked, which is an integer. The thumbnails are placed inside the cointaner_mc MovieClip. container_mc.addEventListener(MouseEvent.CLICK, clickThumb); function clickThumb(e:MouseEvent):void{ if (my_full_images[e.target.name] == undefined){ var full_url = my_images[e.target.nam...@full; etc... but I want to call this function by clicking on a button, which would need to pass another type of parameter, in this case, a variable which refers to the number of the image displayed on the screen plus one. I tried this: clickThumb(image_num + 1) But it doesn't work, I get the following error: 1067: Implicit coercion of a value of type Number to an unrelated type flash.events:MouseEvent. How could I call the function clickThumb and make it use "image_num + 1" instead of "e.target.name" ? Could I dispatch the event (MouseEvent.CLICK, clickThumb), passing this value "image_num + 1" ? Thanks in advance !! Isaac ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] calling an "event" function with a parameter that is a number
You have a couple of options: You could add an optional parameter to the function that handles the click, like this: function clickThumb(e:MouseEvent,index:int = 0):void{ } Setting a default value for the index parameter allows the function to be called without passing it. That's what will happen when a thumb is clicked. When you want to call the function directly, you'd do: clickThumb(null,image_num + 1); In clickThumb, check whether the event object is null or not. If it's not, do what you're currently doing. If it's null, use the index parameter as the index into my_images. Another thing you could do is a bit of refactoring. Have the click handler only resolve the index and calling another function to actually load the image. function clickThumb(e:MouseEvent):void { loadImage(e.target.name); } function loadImage(index:int):void { if (my_full_images[index] == undefined){ var full_url = my_images[inde...@full; ... etc ... } Then, from your other button's handler, call loadImage(image_num + 1); Cheers Juan Pablo Califano 2009/5/23 Isaac Alves > Hello! > In my code there's a function that shows an image on the screen (or load > the > image and show a progress bar) that is called by clicking on a thumbnail, > and uses a parameter [e.target.name] refering to the property "name" of > the > thumbnail clicked, which is an integer. The thumbnails are placed inside > the > cointaner_mc MovieClip. > > container_mc.addEventListener(MouseEvent.CLICK, clickThumb); > > function clickThumb(e:MouseEvent):void{ > if (my_full_images[e.target.name] == undefined){ > var full_url = my_images[e.target.nam...@full; > > etc... > > but I want to call this function by clicking on a button, which would need > to pass another type of parameter, in this case, a variable which refers to > the number of the image displayed on the screen plus one. > > I tried this: > > clickThumb(image_num + 1) > > But it doesn't work, I get the following error: > > 1067: Implicit coercion of a value of type Number to an unrelated type > flash.events:MouseEvent. > > How could I call the function clickThumb and make it use "image_num + 1" > instead of "e.target.name" ? > > Could I dispatch the event (MouseEvent.CLICK, clickThumb), passing this > value "image_num + 1" ? > > Thanks in advance !! > Isaac > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] calling an "event" function with a parameter that is a number
On Sun, May 24, 2009 at 2:00 AM, Isaac Alves wrote: > 1067: Implicit coercion of a value of type Number to an unrelated type > flash.events:MouseEvent. > > How could I call the function clickThumb and make it use "image_num + 1" > instead of "e.target.name" ? Do you have the value of image_num already? Why not change the signature of the clickThumb function so it accepts the number instead of the event object? private function clickThumb(image_num:Number):void { ... } Manish ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] calling an "event" function with a parameter that is a number
Hello! In my code there's a function that shows an image on the screen (or load the image and show a progress bar) that is called by clicking on a thumbnail, and uses a parameter [e.target.name] refering to the property "name" of the thumbnail clicked, which is an integer. The thumbnails are placed inside the cointaner_mc MovieClip. container_mc.addEventListener(MouseEvent.CLICK, clickThumb); function clickThumb(e:MouseEvent):void{ if (my_full_images[e.target.name] == undefined){ var full_url = my_images[e.target.nam...@full; etc... but I want to call this function by clicking on a button, which would need to pass another type of parameter, in this case, a variable which refers to the number of the image displayed on the screen plus one. I tried this: clickThumb(image_num + 1) But it doesn't work, I get the following error: 1067: Implicit coercion of a value of type Number to an unrelated type flash.events:MouseEvent. How could I call the function clickThumb and make it use "image_num + 1" instead of "e.target.name" ? Could I dispatch the event (MouseEvent.CLICK, clickThumb), passing this value "image_num + 1" ? Thanks in advance !! Isaac ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders