..:: MX Studio Fóruns ::..: KEY_DOWN em AS 3.0 - ..:: MX Studio Fóruns ::..

Jump to content

Publicidade




Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

KEY_DOWN em AS 3.0 Movimento de barco e tiro ao mesmo tempo

#1 User is offline   gamesx 

  • Group: Membros
  • Posts: 5
  • Joined: 06-July 06

Posted 23 July 2006 - 12:08 PM

Estou tentando aprender a trabalhar com Action Script 3.0 acontece que algumas coisas não funcionam como esperado, ex.:

Quando quero mover o barco e atirar ao mesmo tempo, ou você move o barco ou atira, não é possível fazer os dois ao mesmo tempo. Em AS 2.0 funciona corretamente.

Em AS 2.0 seria:
barco.onEnterFrame = function () {
if(Key.isDown(Key.UP)) {
this._y-=velocidade;
}
if(Key.isDown(Key.DOWN)) {
this._y+=velocidade;
}
if(Key.isDown(Key.LEFT)) {
this._x-=velocidade;
}
if(Key.isDown(Key.RIGHT)) {
this._x+=velocidade;
}
if(Key.isDown(Key.SPACE)) {
this.atirar();
}
}


Em AS 3.0
public function Barco() {
stage.addEventListener(KeyboardEvent.KEY_DOWN, tecla);
}


private function tecla(event:KeyboardEvent){
if (event.keyCode == 37 && fase.colidido == false){ //LEFT
this.x -= velocidade;
}
if (event.keyCode == 39 && fase.colidido == false){ //RIGHT
this.x += velocidade;
}
if (event.keyCode == 32 && fase.colidido == false){ // SPACE
fase.atirou = true;
}
}

Se alguém estiver trabalhando com AS 3.0 e souber como ajudar, eu agradeço! thumbsup.gif
0

#2 User is offline   gamesx 

  • Group: Membros
  • Posts: 5
  • Joined: 06-July 06

Posted 23 July 2006 - 01:02 PM

Acabei descobrindo a resposta antes da ajuda do Forum smartass.gif
Ainda sim vou postar a reposta, se alguém souber uma maneira melhor posta aí!!!

Vou postar a classe inteira, mas é só comparar com o errado que vocês entenderão.

/* --------------------------
Inicio Código
-------------------------- */

package {

import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.util.trace;
import flash.events.*;

public class Barco extends MovieClip {

private var velocidade:uint = 5;
private var esq:Boolean = false;
private var dir:Boolean = false;
private var tiro:Boolean = false;


public function Barco() {
// Controla as teclas pressionadas
stage.addEventListener(KeyboardEvent.KEY_DOWN, tcEntra);
// Controla as teclas liberadas
stage.addEventListener(KeyboardEvent.KEY_UP, tcSai);
// Controla os movimentos e o tiro
addEventListener(Event.ENTER_FRAME, tcVerif);
}


public static function colisao(){
fase.colidido = true;
}

private function tcEntra(event:KeyboardEvent){
// Ativa Esquerda
if (event.keyCode == 37 && fase.colidido == false){
esq = true;
}
// Ativa Direita
if (event.keyCode == 39 && fase.colidido == false){
dir = true;
}
// Ativa Tiro
if (event.keyCode == 32 && fase.colidido == false){
tiro = true;
}
}

private function tcSai(event:KeyboardEvent){
// Desativa Esquerda
if (event.keyCode == 37 && fase.colidido == false){
esq = false;
}
// Desativa Direita
if (event.keyCode == 39 && fase.colidido == false){
dir = false;
}
// Desativa Tiro
if (event.keyCode == 32 && fase.colidido == false){
tiro = false;
}
}

private function tcVerif(event:Event){
// Mover para a Esquerda
if(esq == true){
this.x -= velocidade;
}
// Mover para a Direita
if(dir == true){
this.x += velocidade;
}
// Atirar
if(tiro == true){
fase.atirou = true;
}
}

}

}

/* --------------------------
Fim Código
-------------------------- */
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic



Publicidade




1 User(s) are reading this topic
0 membro(s), 1 visitante(s) e 0 membros anônimo(s)