javascript - Keydown / Keyup gets called 7 times -
i'm building angularjs website game. here want use keyboard resume/pause , control game. code have add eventlistener following:
$window.addeventlistener('keydown', function(e) { if ($scope.gamestate.playing) { (var control in controls) { if (controls.left.indexof(e.keycode) > -1) { gameengine.startleft(); } else if (controls.right.indexof(e.keycode) > -1) { gameengine.startright(); } else if (controls.powerjump.indexof(e.keycode) > -1) { gameengine.powerjump(); } else if (controls.pause.indexof(e.keycode) > -1) { $scope.pausegame(); } } } });
the content of function isn't important, problem gets called 7 times every time press key. same keyup
. fast or slow press it.
var gameapp = angular.module('gameapp', []); gameapp.controller('gamecontroller', function($scope, $timeout, $window)
the above code how create angular app , controller $window argument.
i couldn't find solution this. hope knows why that's happening.
wild guess: event handler registered 7 times?
to check like:
$window.addeventlistener('keydown', function(e) { if(e.iamnotalone) { throw new error('oh dear!'); } e.iamnotalone = true; /* control in control thingy goes here */ });
are setting in singleton part of service or in sort of controller (which recreated every time , explain behaviour) ?
Comments
Post a Comment