Checking for a pressed button in FlashMX 2004

  • kirk.netiq
  • Born
  • Born
  • kirk.netiq
  • Posts: 4

Post 3+ Months Ago

Can anyone show me how to setup and ActionScript to check for a pressed button. If the button is pressed, the alpha of the button is set to 50% and the sound is stopped.

Thanks

Kirk
  • mahjik
  • Novice
  • Novice
  • mahjik
  • Posts: 33
  • Loc: Las Cruces, NM USA

Post 3+ Months Ago

here is the code to attach to the timeline the button is on. note, make sure you have given your button an instance name. you do this by selecting it, and then in the property inspector on the left hand side is a text box where you can put this name. it is good practice to end button names with _btn. i.e. myButton_btn. this enables code hinting when typing in your actionscript code.

Code: [ Select ]
myButton_btn.onRelease = function(){
  this._alpha = 50;
  delete this.soundClip_mc;
}
  1. myButton_btn.onRelease = function(){
  2.   this._alpha = 50;
  3.   delete this.soundClip_mc;
  4. }

note that if you have other buttons, you can use this on them too, but there is no code here to return the alpha to 100 when you click on a different button.

the delete command is if the sound is in its own movie clip. if it is an atached sound, replace it with

Code: [ Select ]
mySoundObject.stop();


if it is attached directly to the button up or over frame. put it in the down state and set it to stop, but as soon as you move the mouse again it will probably restart. if this is how you set it up, maybe someone with more experrience with sound can help you out. I am kind of curious myself how you would do that.

hope that helps
  • kirk.netiq
  • Born
  • Born
  • kirk.netiq
  • Posts: 4

Post 3+ Months Ago

Thank you. I am linking to sounds in a library..so I am not sure how to use the delete this.soundClip_mc; part of your code.
  • kirk.netiq
  • Born
  • Born
  • kirk.netiq
  • Posts: 4

Post 3+ Months Ago

To clarify....I have six buttons....when a user clicks on any button it sends them to another frame.....I need the code to say if this buttons is clicked, set the alpha to 50% so each time they click on a button and come back to the start frame that button is now 50%(eventually all six buttosn will be dimmed)....and to stop the sound that was played in the start frame so it will not start playing again...my sounds are linked from my library.
  • mahjik
  • Novice
  • Novice
  • mahjik
  • Posts: 33
  • Loc: Las Cruces, NM USA

Post 3+ Months Ago

i would use the sounds as follows.

lets say your sounds are in your library. right click and then click linkage, export for actionscript, and give them a linkage identifier like "sound1", or whatever

then in your timeline attach the sound to a sound object as follows:

Code: [ Select ]
var mySound = new Sound();
mySound.attachSound ("sound1");
mySound.start();
mySound.stop();
  1. var mySound = new Sound();
  2. mySound.attachSound ("sound1");
  3. mySound.start();
  4. mySound.stop();


you can have, i believe, up to 8 sound objects playing a sound at once. each object only plays the sound attached to it most recently.

use the start and stop as above to play it or stop it.

then the code for each button would be something like

Code: [ Select ]
thisButtonName.onRelease = function(){
  this._alpha = 50;
  this.enabled = false;
  mySound.stop();
  _root.gotoAndPlay (whateverFrameYouWant);
}
  1. thisButtonName.onRelease = function(){
  2.   this._alpha = 50;
  3.   this.enabled = false;
  4.   mySound.stop();
  5.   _root.gotoAndPlay (whateverFrameYouWant);
  6. }


the enabled = false makes it so the button cant be clicked on any more
change the last two lines to stop whichever sound you want stopped, and to goto whatever frame you want to

hope that helps
  • contstrictor
  • Novice
  • Novice
  • contstrictor
  • Posts: 26

Post 3+ Months Ago

instead of having to go back to a start frame how about simply adding about five more lines to the function controlling the button behaviour to enable all others and disable the current?
for example:
Code: [ Select ]
thisButtonName.onRelease = function(){
  this._alpha = 50;
  this.enabled = false;
  thatOtherButton.enabled = true;
  theThirdButton.enabled = true;
  //and do so for the other buttons...
  mySound.stop();
  //_root.gotoAndPlay (whateverFrameYouWant);
}
  1. thisButtonName.onRelease = function(){
  2.   this._alpha = 50;
  3.   this.enabled = false;
  4.   thatOtherButton.enabled = true;
  5.   theThirdButton.enabled = true;
  6.   //and do so for the other buttons...
  7.   mySound.stop();
  8.   //_root.gotoAndPlay (whateverFrameYouWant);
  9. }

that way each button enables the others and disables itself.
what do you think :D
  • kirk.netiq
  • Born
  • Born
  • kirk.netiq
  • Posts: 4

Post 3+ Months Ago

Thanks for the help. I could have set this up so it was all on one frame with all the various information (as MC's) set alpha to 0. But the issue was it would be too busy.

but since I choose to do it the way I did, I still have the problem that when the user clicks the close window and returnd to the start frame - they hear the audio again. Shouldn't there be some code in the start frame that says, if your back here a second time, do not play the audio again....
  • contstrictor
  • Novice
  • Novice
  • contstrictor
  • Posts: 26

Post 3+ Months Ago

you could add this line to the button script:
Code: [ Select ]
thisButtonName.onRelease = function(){
  this._alpha = 50;
  this.enabled = false;
  thatOtherButton.enabled = true;
  theThirdButton.enabled = true;
  //and do so for the other buttons...
  mySound.stop();
  //_root.gotoAndPlay (whateverFrameYouWant);
if(!alreadyPlayed){
// already played will be a variable you right to flash's memory as soon as
//the sound object is initialized so that the nextime you need to call it you
//just check to see if it's been played already if it has you can remove it if
//you want.
  mySound.start();
  alreadyPlayed = true;
}else{
  //remove sound script comes here
}
  1. thisButtonName.onRelease = function(){
  2.   this._alpha = 50;
  3.   this.enabled = false;
  4.   thatOtherButton.enabled = true;
  5.   theThirdButton.enabled = true;
  6.   //and do so for the other buttons...
  7.   mySound.stop();
  8.   //_root.gotoAndPlay (whateverFrameYouWant);
  9. if(!alreadyPlayed){
  10. // already played will be a variable you right to flash's memory as soon as
  11. //the sound object is initialized so that the nextime you need to call it you
  12. //just check to see if it's been played already if it has you can remove it if
  13. //you want.
  14.   mySound.start();
  15.   alreadyPlayed = true;
  16. }else{
  17.   //remove sound script comes here
  18. }

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 3 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.