as3中的游戏颜色示例

时间:2017-04-12 09:16:47

标签: actionscript-3

How can you put names

确保您在文本动态和其他颜色上添加名称

这是一款关于颜色的游戏,其中颜色的名称将出现在顶部,你必须尝试猜测你将拥有3种生命的颜色

  stop();

 //var and text information
  texto.text = "Vermelho";
  var vidas  = 3;
  vida.text = "Vidas: " + vidas;

  //buttons function to be pressed
  vermelho.addEventListener(MouseEvent.CLICK, clicked);
  Azul.addEventListener(MouseEvent.CLICK, clicked1);
  Verde.addEventListener(MouseEvent.CLICK, clicked2);
  Amarelo.addEventListener(MouseEvent.CLICK, clicked3);
  Rosa.addEventListener(MouseEvent.CLICK, clicked4);
  Laranja.addEventListener(MouseEvent.CLICK, clicked5);

 //function to validate if the text is equal to the button
function clicked(clickInfo){

//If click on red go to blue
if(clickInfo.target == vermelho){
    trace("Correto");
    texto.text = "Azul";
    vermelho.visible = false;
}else{
    vidaa();
    vida.text = "Vidas: " + vidas;
    trace("Incorecto");
}
}
function clicked1(clickInfo){
//If click on blue go to green
if(clickInfo.target == Azul && texto.text == "Azul"){
    trace("Correto");
    texto.text = "Verde";
    Azul.visible = false;
}else{
    vidaa();
    vida.text = "Vidas: " + vidas;
    trace("Incorecto");
}
}
function clicked2(clickInfo){
//If click on green go to yellow
if(clickInfo.target == Verde && texto.text == "Verde"){
    trace("Correto");
    texto.text = "Amarelo";
    Verde.visible = false;
}else{
    vidaa();
    vida.text = "Vidas: " + vidas;
    trace("Incorecto");
}
}
function clicked3(clickInfo){
//If click on yellow go to pink
if(clickInfo.target == Amarelo && texto.text == "Amarelo"){
    trace("Correto");
    texto.text = "Rosa";
    Amarelo.visible = false;
}else{
    vidaa();
    vida.text = "Vidas: " + vidas;
    trace("Incorecto");
}
}
function clicked4(clickInfo){
//If click on pink go to orange
if(clickInfo.target == Rosa && texto.text == "Rosa"){
    trace("Correto");
    texto.text = "Laranja";
    Rosa.visible = false;
}else{
    vidaa();
    vida.text = "Vidas: " + vidas;
    trace("Incorecto");
}
}
function clicked5(clickInfo){
//If click on orange go to win the game
if(clickInfo.target == Laranja && texto.text == "Laranja"){
    trace("Correto");
    texto.text = "Parabéns";
    Laranja.visible = false;
}else{
    vidaa();
    vida.text = "Vidas: " + vidas;
    trace("Incorecto");
}

}


function vidaa(){
vidas -=1;
if (vidas == 0){
    gotoAndStop(2);
}
}

1 个答案:

答案 0 :(得分:1)

像这样的东西(完全没有经过测试:)

// all colors in the order they will appear
var colors:Array = ["Vermelho", "Azul", "Verde", "Amarelo"] // add all other colors here

// assign a name to each button that is exactly the same as in the colors array - on click you will read that name and see if it matching the current color
// I believe there is a way to set the name in the Flash IDE as well in the object properties, but I don't remember anymore
vermelho.name = "Vermelho";
vermelho.addEventListener(MouseEvent.CLICK, onClick);

vermelho.name = "Azul";
Azul.addEventListener(MouseEvent.CLICK, onClick);

Verde.name = "Verde";
Verde.addEventListener(MouseEvent.CLICK, onClick);

// current color index in the array (first color "Vermelho" is at position 0, we will start at -1 so the next color will be 0)
var currentIndex:int = -1;

// start the game, this will show the first color
showNextColor();

function onClick(event:MouseEvent)
{
    // check if the button name matches the current color
    if(event.target.name == colors[currentIndex])
    {
        trace("Correto");
        event.target.visible = false;
        showNextColor();
    }
    else
    {
        vidaa();
        vida.text = "Vidas: " + vidas;
        trace("Incorecto");
    }
}


// gets the current color from the array and displays it
private function showNextColor()
{
    // check if we have any colors left to show in the array
    if(currentIndex < colors.length - 1)
    {
        // if yes increase the index and show next color
        currentIndex++;
        texto.text = colors[currentIndex];
    }
    else
    {
        // otherwise the user have guessed all colors, do something here
        texto.text = "You are awesome!";
    }
}