|
-
How to add keyboard interaction?
I got 16 different buttons in this application, each button playing different kind of sound. Currently i only able to use mouse click to start playing sound, and click again to stop, but now i wish to add shortcut key to access those track buttons, like pressing "q" for track 1, "w" for track 2. Pressing "q" for the first time will start the sound and animation, press again will stop the sound and animation. Currently I've implemented this code, there are no errors but it won't work somehow, what is the problem?
Code:
private var _ambientTracks:Array = [ambient1, ambient2, ambient3, ambient4];
private var _effectTracks:Array = [effect1, effect2, effect3, effect4];
private var _melodyTracks:Array = [melody1, melody2, melody3, melody4];
private var _beatTracks:Array = [beat1, beat2, beat3, beat4];
//handle click on track invoke start / stop functions
private function onTrackClicked(event:MouseEvent):void {
var track:Sprite = event.currentTarget as Sprite;
var trackName:String = track.name;
if (trackName in _playingTracks) {
stopTrack(track);
delete _playingTracks[trackName];
} else {
startTrack(track);
_playingTracks[trackName] = trackName;
}
}
//starts track animation and dispatch event for TrackMixer
private function startTrack(track:Sprite):void {
Actuate.tween(track, 0.6, {alpha: 0.3}).reflect().repeat();
dispatchEvent(new ObjectEvent(START_TRACK, track.name, true));
}
//stop track animation and dispatch event for TrackMixer
private function stopTrack(track:Sprite):void {
Actuate.stop(track, "alpha");
track.alpha = 1;
dispatchEvent(new ObjectEvent(STOP_TRACK, track.name, true));
}
//trigger keyboard shortcut
public function setup(stage:Stage):void {
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
}
private var bool:Boolean = false;
private function keyDown(event:KeyboardEvent):void {
var track1:Sprite = Sprite(String(ambient1));
if (event.keyCode == 81) {
if (bool == false) {
startTrack(Sprite(track1));
_playingTracks[track1];
bool = true;
} else {
if (bool == true) {
stopTrack(Sprite(track1));
delete _playingTracks[track1];
bool = false;
}
}
}
}
Tags for this Thread
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
|