-
How to I make it play a sound on button press?
Hi, basically I want it so that when I press the ctrl button it will play a short sound, how do I do this?
I have the sound file (laserSound)
(Basically in context I am trying to follow a tutorial to make a game and ctrl makes the laser gun fire, and I would like to attach a sound to this action)
-
Busy doing nothing
//function that checks for key presses
function myOnKeyDown() {
//if the pressed key is control
if (Key.isDown(Key.CONTROL)) {
//make a new sound object, attach the laser sound to it and play the sound
var my_sound:Sound = new Sound();
my_sound.attachSound("laserSound");
my_sound.start();
}
}
//listener object for checking key presses
var myListener:Object = new Object();
myListener.onKeyDown = myOnKeyDown;
Key.addListener(myListener);
hope this helps
--------------------------------------------------
‘There is no emoticon to express how I am feeling’ - Comic Book Guy
There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!
-
thanks buddy! I'll try that ASAP, really appreciate it
-
Hi, I have tried your method but unfortunately it does not seem to work for me, perhaps I am not fitting it into the code correctly? I was wondering if you could be so kind as to show me how it would fit into my existing code which I have posted below (this code goes within the space ship). At present the sound file is in my library and has the name as stated above!
onClipEvent(load){
moveSpeed=10;
moveSpeed2=20;
_root.laser._visible=false;
laserCounter=1;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.CONTROL)) {
laserCounter++;
_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
_root["laser"+laserCounter]._visible=true;
}
if (Key.isDown(Key.RIGHT)) {
this._x+=moveSpeed2;
} else if (Key.isDown(Key.LEFT)) {
this._x-=moveSpeed2;
}
if (Key.isDown(Key.DOWN)) {
this._y+=moveSpeed;
} else if (Key.isDown(Key.UP)) {
this._y-=moveSpeed;
}
}
-
Busy doing nothing
Could try:
onClipEvent(load){
moveSpeed=10;
moveSpeed2=20;
_root.laser._visible=false;
laserCounter=1;
//----------------<new code>--------------//
my_sound = new Sound();
my_sound.attachSound("laserSound");
//----------------<end : new code>--------------//
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.CONTROL)) {
laserCounter++;
_root.laser.duplicateMovieClip( "laser"+laserCounter, laserCounter );
_root["laser"+laserCounter]._visible=true;
//----------------<new code>--------------//
my_sound.start();
//----------------<end : new code>--------------//
}
--------------------------------------------------
‘There is no emoticon to express how I am feeling’ - Comic Book Guy
There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!
-
Busy doing nothing
bTW the sound in the library must have the name "laserSound" set as the identifier in the Linkage - with "export as Actionscript" selected!
--------------------------------------------------
‘There is no emoticon to express how I am feeling’ - Comic Book Guy
There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!
-
Originally Posted by Boris the Frog
bTW the sound in the library must have the name "laserSound" set as the identifier in the Linkage - with "export as Actionscript" selected!
works like a dream , I hadn't done this bit, and the extra code you posted was very helpful too, thankyou!!
-
Busy doing nothing
cool - my work here is done
--------------------------------------------------
‘There is no emoticon to express how I am feeling’ - Comic Book Guy
There's an effective, simple solution to carbon sequestration... it's called 'coal', so leave it alone!
There's an effective, simple solution to carbon capture....it's called 'trees', so plant some!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|