hello there ! I need some help , because i’m trying to do a simple game in which you have to move a frog in X axis to catch the falling flies. sorry if I mistake some words cuz I talk in spanish
It’s already done, but it has 2 bugs and I dunno what the reasons are
The 1st bug,
Each time the game is over, and you click in "PLAY AGAIN ",
I could notice that the frog movement (which you can move by pressing the keys) is increased a little;
for example if you played 6 times the 7th you can reach the opposite side by pressing the corresponding key 2 times, despite the fact that in the code i did write only +15px for the right when you press right arrow and -15px for the left when you press the left one (on X axis) for each time the key is pressed
And the 2nd,
After clicking the button which leads you to start playing,
the keyboard functions to move the character in X axis don’t work until you click again wherever in the stage (it remains also when playing again after lose)
this is the code
Code:
//"AGARRADOR" DESIGNS THE "HERO"
var agarrador:Agarrador;
var crearIDenemigo:uint;
var velocidadJuego:uint;
var atrapadasText:TextField;
var erradasText:TextField;
var score:uint=0;
function iniciarJuego():void{
agarrador=new Agarrador();
agarrador.x=400;
agarrador.y=500;
addChild(agarrador);
velocidadJuego=500;
crearIDenemigo=setInterval(crearID,velocidadJuego);
Mouse.hide();
erradasText=new TextField();
erradasText.x=50;
erradasText.y=50;
addChild(erradasText);
atrapadasText=new TextField();
atrapadasText.x=250;
atrapadasText.y=50;
addChild(atrapadasText);
erradasText.text=atrapadasText.text=“0”;
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, presionoTecla);
stage.addEventListener(KeyboardEvent.KEY_UP, sueltoTecla);
stage.addEventListener(Event.ENTER_FRAME , mover);
var izquierda:Boolean;
var derecha:Boolean;
function mover(e:Event):void {
if (derecha) {
agarrador.x+=15;
}
if (izquierda) {
agarrador.x-=15;
}
//FOR NOT GOING BEYOND THE STAGE
if(agarrador.x > stage.stageWidth - agarrador.width / 2){
agarrador.x-=15
}
if(agarrador.x < agarrador.width / 2){
agarrador.x+=15
}
//ANOTHER WAY,THANKS STOWNS /*
/*var agarradorRightSide = agarrador.x + agarrador.width;
if (agarradorRightSide >= 800) {
agarrador.x = 759 - agarrador.width;
}
else if (agarrador.x <= 0) {
agarrador.x = 1;
} */
}
function sueltoTecla(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.LEFT :
izquierda=false;
break;
case Keyboard.RIGHT :
derecha=false;
break;
default :
break;
}
}
function presionoTecla(event:KeyboardEvent):void {
switch (event.keyCode) {
case Keyboard.LEFT :
izquierda=true;
break;
case Keyboard.RIGHT :
derecha=true;
break;
default :
break;
}
}
//
}
function crearID():void{
var enemigo:Cayendo=new Cayendo()
//QUE ESTE EN Y FUERA DE LA PANTALLA Y ALEATORIAMENTE EN X
enemigo.y=-50;
enemigo.x=Math.random()*stage.stageWidth;
enemigo.addEventListener(Event.ENTER_FRAME,dropEnemy);
addChild(enemigo);
}
function dropEnemy(e:Event):void{
var mc:Cayendo=Cayendo(e.target);
mc.y+=10
if(mc.hitTestObject(agarrador)){
//FUNCION
atrapadas(mc);
}else if(mc.y>stage.stageHeight){
//FUNCION
erradas(mc);
}
}
function atrapadas(mc:Cayendo):void{
mc.removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
atrapadasText.text=String(Number(atrapadasText.text)+1);
}
function erradas(mc:Cayendo):void{
mc.removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChild(mc);
erradasText.text=String(Number(erradasText.text)+1);
//PARA EL GAME OVER
if(erradasText.text==“5”){
gameOver();
}
}
function gameOver():void{
score=Number(atrapadasText.text);
removeChild(agarrador);
clearInterval(crearIDenemigo);
removeChild(atrapadasText);
removeChild(erradasText);
//removimos todos los child pero POR AHI SOBRABA ALGUNO
while(numChildren>0){
getChildAt(0).removeEventListener(Event.ENTER_FRAME,dropEnemy);
removeChildAt(0);
}
gotoAndStop(“game over”);
Mouse.show();
}
iniciarJuego();
these are also the codes from the previous and next frame with the instruction to start the game and with game over
Code:
stop();
start_mc.but_txt.text=“Comenzar”;
start_mc.buttonMode=true;
start_mc.mouseChildren=false;
function comenzarjuego (e:MouseEvent):void{
gotoAndStop("juego");
}
start_mc.addEventListener(MouseEvent.CLICK,comenzarjuego);
Code:
start_mc.but_txt.text=“Jugar de nuevo”;
//PARA QUE LE BOTON ESTE HABILITADO
start_mc.buttonMode=true;
//ASI PODEMOS TOCAR SIN QUE INFLUYA LO DE ADENTRO DEL BOTON
start_mc.mouseChildren=false;
start_mc.addEventListener(MouseEvent.CLICK,comenzarjuego);
score_txt.text=“GAME OVER
Has comido “+score+” moscas!.”
/*function comenzarjuego (e:MouseEvent):void{
gotoAndStop("juego");
}
Can you see some mistake?
Thank you very much