Actionscript 3 as3中的游戏颜色示例

Actionscript 3 as3中的游戏颜色示例,actionscript-3,Actionscript 3,确保在文本动态和其他颜色上添加了名称 这是一个关于颜色的游戏,其中颜色的名称将出现在顶部,你必须尝试猜测哪种颜色你将有3条生命 stop(); //var and text information texto.text = "Vermelho"; var vidas = 3; vida.text = "Vidas: " + vidas; //buttons function to be pressed vermelho.addEventListener(Mouse

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

这是一个关于颜色的游戏,其中颜色的名称将出现在顶部,你必须尝试猜测哪种颜色你将有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);
}
}
类似这样的情况(但完全未经测试:)

//所有颜色的显示顺序
变量颜色:数组=[“Vermelho”、“Azul”、“Verde”、“Amarelo”]//在此处添加所有其他颜色
//为每个按钮指定一个与“颜色”数组中的按钮完全相同的名称-单击后,您将读取该名称,并查看其是否与当前颜色匹配
//我相信有一种方法可以在flashide和objectproperties中设置名称,但我已经不记得了
vermelho.name=“vermelho”;
vermelho.addEventListener(MouseEvent.CLICK,onClick);
vermelho.name=“Azul”;
Azul.addEventListener(MouseEvent.CLICK,onClick);
Verde.name=“Verde”;
Verde.addEventListener(MouseEvent.CLICK,onClick);
//数组中的当前颜色索引(第一个颜色“Vermelho”位于位置0,我们将从-1开始,因此下一个颜色将为0)
var currentIndex:int=-1;
//开始游戏,这将显示第一种颜色
showNextColor();
函数onClick(事件:MouseEvent)
{
//检查按钮名称是否与当前颜色匹配
if(event.target.name==colors[currentIndex])
{
跟踪(“Correto”);
event.target.visible=false;
showNextColor();
}
其他的
{
vidaa();
vida.text=“Vidas:+Vidas;
痕迹(“不直接”);
}
}
//从数组中获取当前颜色并显示它
私有函数showNextColor()
{
//检查数组中是否还有要显示的颜色
如果(当前索引
如果您有一些想法,以及如何压缩代码,那么问题是什么?我想看看是否可以得到更多的压缩程序,您可以将其放入一个函数中,但有一个问题,请正确缩进您的代码。
// 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!";
    }
}