A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to add keyboard interaction?

  1. #1
    Junior Member
    Join Date
    Nov 2014
    Posts
    2

    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;
                    }
                }
            }
        }

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    If there are no errors, what response are you getting? Did you trace down to the specific function to see if it is moving through each line as expected?
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Posts
    1,818
    It's difficult to narrow down what exactly the problem is without better context. Posting the file if you can would help with figuring out the problem. Also, I feel like you might be complicating things, not that I'd blame you with this kind of thing. If I could get a look at everything I might be able to help you with improving the code. For example, you have some messy redundancies in there:
    Code:
    if (event.keyCode == 81) {
    	if (bool) {
    		stopTrack(Sprite(track1));
    		delete _playingTracks[track1];
    		bool = false;
    	} else {
    		startTrack(Sprite(track1));
    		_playingTracks[track1];
    		bool = true;
    	}
    }
    .

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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center