Comunidad de diseño web y desarrollo en internet online

Cambiar las acciones de onRelease por acciones de teclado

Citar            
MensajeEscrito el 22 Ago 2008 07:51 pm
jugando a jugar decidi crear un jeugo educativo para niños de edad preescolar. El asunto es que consegui un tuto muy bueno donde explica como hacer un SIMON en flash. Muy bien explicado pude implementar el juego solo que para mayor facilidad de mis pequeños usuarios deseo implementarlo con teclas, en vez de onRelease dese que responda a eventos del teclado. Alguien podria decirme como puedo crear lo que quier? de antemano muchas gracias

Código :

fscommand("fullscreen",true);
var dim = 100;  //Alpha of pieces when clicked
aPieces = new Array(); //Array of pieces
aMoveList = new Array(); //Array of moves made
var ID;
var count = 0; //Number of moves made
var first;
var soSound0 = new Sound(this); //Creates a new Sound
soSound0.attachSound("simonSound1.mp3"); //Attaches simonSound1.mp3 to that sound
var soSound1 = new Sound(this);
soSound1.attachSound("simonSound2.mp3");
var soSound2 = new Sound(this);
soSound2.attachSound("simonSound3.mp3");
var soSound3 = new Sound(this);
soSound3.attachSound("simonSound4.mp3");
var step = 0;
muestra.stop();
for (var i = 1; i < 5; i++) //Adds the four movie clip quadrants to aPieces
{
        aPieces.push(this["m" + i]);
        this["m" + i].value = i - 1;
}
function buildPieces()
{
        for (var i = 0; i < aPieces.length; i++)
        {
                aPieces[i].onPress = function() //When the piece is pressed it dims
                {
                        this._alpha = dim;
                };
                aPieces[i].onRollOut = function() //When you roll your mouse off of the piece it undims
                {
                        this._alpha = 20;
                };
                aPieces[i].onRelease = function()  //When you release your mouse
                {
                        
                  this._alpha = 100;  //Set transparency to 100%
                        _root["soSound" + this.value].start(0); //Play the piece sound
                        if (aMoveList[step] != this.value) //If the piece you pressed does not equal the piece you were supposed to press
                        {
                                failure(); //failure method
                                return;
                        }
                        step++; //Add one to the amount of steps to do
                        if (step == aMoveList.length)
                        {
                                nextLevel();  //Go to the next level
                        }
                };
        }
}
function failure()
{
        tMainText.text = "Game Over.";
        killPieces();
      muestra.stop();
        mStart._alpha = 100;  //Set start button transparency back to 100%
      aMoveList = new Array();  //Delete array of moves
        mStart.onRelease = function()  //Recreate the start button
        {
                startGame();
        }
}
function killPieces()
{
        for (var i = 0; i < aPieces.length; i++) //Deletes all the functions of the pieces so you cant interact with them
        {
                delete aPieces[i].onPress;
                delete aPieces[i].onRollOut;
                delete aPieces[i].onRelease;
        }
}
function lightUp(num)
{
        clearInterval(ID);
        count--;
        if (count == 0)  //If no more pieces are left to light up, wait for user to input sequence
        {
                userTurn();
                return;
        }
        if (count == 1) //If the last piece in the list has just been lit up, pick a random light to light up
        {
                num = Math.floor(Math.random() * 4);
        }
        else //Set num to the next light to light up
        {
                num = aMoveList[aMoveList.length + 1 - count];
        }
        light(num); //Light up the light
}
function userTurn()
{
        step = 0; //Sets the step to 0
        buildPieces();  //Lets user interact with pieces
}
function light(num)
{
        dimIt(num); //dims the num piece
        ID = setInterval(pause, 1000, num); //Light up the piece for half of a second
}
function pause(num)
{
        clearInterval(ID);
        unDim(num);
        ID = setInterval(lightUp, 100, num); //Creates a 50ms pause between lighting up pieces
}
function dimIt(num)
{
        if (count == 1)
        {
                aMoveList.push(num);
        }
        _root["soSound" + num].start(0); //Plays the sound when lighting a piece up
        aPieces[num]._alpha = 100;
}
function unDim(num)
{
        aPieces[num]._alpha = 20;
}
function startGame()
{
        first = Math.floor(Math.random() * 4) //Picks first piece
        tMainText.text = ""; //Sets the text field to display nothing
        for (var i = 0; i < aPieces.length; i++)
        {
                aPieces[i]._alpha = 20; //Sets all the pieces to 100% transparency
        }
        mStart._alpha = 0; //Sets the start button to invisible
        delete mStart.onRelease; //Deletes the start button onRelease function
        muestra.play();
      buildPieces();
        count = 1;
        light(first); //Lights first piece
}
function nextLevel()
{
        killPieces(); //Disables interacting with pieces
        for (var i = 0; i < aPieces.length; i++)
        {
                aPieces[i]._alpha = 20; //Sets all pieces to 100% transparency
        }
        count = aMoveList.length + 1;
        var timer = getTimer();
        this.onEnterFrame = function()
        {
                if (timer < getTimer() - 1500) //Wait 1 second before starting sequence display after input
                {
                        delete this.onEnterFrame;
                        light(first);
                }
        };
}
mStart.onRelease = function()
{
        startGame();
};

Por darkesolutions

1 de clabLevel



 

firefox
Citar            
MensajeEscrito el 23 Ago 2008 05:45 pm
La verdad...
No leí el codigo...
Pero si deseas que registre un boton, seria mas o menos así:

Código :

rojo= function () {
if (Key.isDown(Key.UP) && !pressUp) {
doRojo=true;
}
pressUp=Key.isDown(Key.UP);
}


Esto registra la tecla, solo cuando se presiona... No cada frame...
¿porque?... porque el codigo funciona así...

1) si "Up" esta presionado y pressUp es false, se ejecuta el codigo... (supongamos que si se presiono)
2) pressUp lee si "Up" esta presionado, y devuelve un booleano... (resulta true)
3) En un nuevo frame se vuelve a correr el codigo, pero esta vez, pressUp es verdadero, por lo que no se ejecutara el codigo, hasta que se libere la tecla "Up"... (Liberamos Up)
4) pressUp se vuelve false (por liberar Up), por lo que en el proximo frame, si se presiona Up,se ejecutara nuevamente el codigo...

despues, con la variable booleana pressRojo, activarias las acciones del onRelease...

tendrias las funciones (por ejemplo)....

rojo ();
verde ();
azul ();
amarillo ();
creaRojo ();
creaVerde ();
creaAzul ();
creaAmarillo ();

colocadas en un onEnterFrame...

la funciones crea rojo, se ejecuta si doRojo es true:

Código :

creaRojo = function () {
//sentencias
doRojo=false// <----- Este es el truco
}


así, creaRojo se ejecuta una sola vez...

Se hace lo mismo con las otras funciones, y ya esta...

Por riajiru

38 de clabLevel



Genero:Masculino  

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.