Sto sviluppando un piccolo gioco. Io uso il seguente codice per rilevare i tasti premuti da parte del giocatore:Quanti tasti premuti possono rilevare Flash? using as3
private function onKeyDown(event:KeyboardEvent):void {
//moviment keys
if (event.keyCode == 37 || event.keyCode == 65) {
this.leftKeyPressed = true;
}
if (event.keyCode == 39 || event.keyCode == 68) {
this.rightKeyPressed = true;
}
if (event.keyCode == 38 || event.keyCode == 87) {
this.upKeyPressed = true;
}
if (event.keyCode == 40 || event.keyCode == 83) {
this.downKeyPressed = true;
}
if (event.keyCode == this.shootKey) {
this.shootKeyPressed = true;
}
}
L'evento onKeyUp:
private function onKeyUp(event:KeyboardEvent):void {
if (event.keyCode == 37 || event.keyCode == 65) {
this.leftKeyPressed = false;
}
if (event.keyCode == 39 || event.keyCode == 68) {
this.rightKeyPressed = false;
}
if (event.keyCode == 38 || event.keyCode == 87) {
this.upKeyPressed = false;
}
if (event.keyCode == 40 || event.keyCode == 83) {
this.downKeyPressed = false;
}
if (event.keyCode == this.shootKey) {
this.shootKeyPressed = false;
}
if (event.keyCode == changeColorKey) {
trace('color key released');
trace(getTimer());
this.changeColorKeyPressed = true;
}
}
Fondamentalmente lo shootKey saranno tenere premuto dal giocatore quasi tutto il tempo. E il ChangeColorKey verrà premuto molto spesso ma non tenere premuto. Durante i test ho notato che tenendo premuto il tasto di selezione e la freccia rivolta verso il basso, gli eventi onKeyUp per ChangeColorKey non vengono attivati. Tenere premuto il tasto freccia su o giù anziché la freccia destra ha lo stesso effetto. Se tengo il tasto freccia sinistra, gli eventi vengono sparati. Perché si trova? C'è qualcosa che non va nel mio codice?
E un paio di articoli su questo problema (e anche su come hackerare una tastiera). http://www.extremetech.com/article2/0,2845,2301151,00.asp http://www.extremetech.com/article2/0,2845,1656177,00.asp Sembra solitamente usb le tastiere supportano fino a sei chiavi simultanee, quindi se hai una vecchia tastiera potrebbe essere un buon momento per aggiornarla. – LopSae