如何确定是否单击右侧的动画片段/按钮?在as3

时间:2013-11-20 14:43:22

标签: actionscript-3 flash-cs5

我正在制作一个游戏,其中会出现一个模式,用户必须在项目中点击模式显示的示例模式:橡皮擦,铅笔,订书机,剪辑。

因此,如果用户首先点击铅笔而不是橡皮擦,则游戏将重置。

我怎样才能将这些物品洗牌,是否只能通过代码?我做了(5支铅笔,3支橡皮擦,2个订书机,6个夹子,3个圆珠笔,5个胶水和4个液体橡皮擦),还是我需要制作每一帧并手动洗牌?

我使用模式制作程序:剪辑,橡皮擦,胶水,剪辑 我所做的是给模式一个12的值 然后是项目的值:铅笔= 1,橡皮擦= 2,夹子= 3,胶水= 4,订书机= 5,液体橡皮擦= 6,圆珠笔= 7,

问题是我点击的任何内容,只要它创建了一个用户获胜的值12,这就是为什么我想检测点击的项目是否正确。

var total = 0;
var gattotal = 0;
var rndNum = 0;
pattern.visible = false;

//for random number
function randomNumbers(min:Number,max:Number) {
var Results:Number=Math.floor(Math.random()*max)+min;
return Results;
}

function determineWin() {
if (rndNum == 1){
    total = 12;
}

if (rndNum == 2){
    total = 16;
}

if (rndNum == 3){
    total = 7;
}
if (rndNum == 4){
    total = 15;
}
}

pushme.addEventListener (MouseEvent.CLICK, begin);

function begin (event:MouseEvent): void {
rndNum = randomNumbers(1,4);
pattern.visible = true;
trace(rndNum);

if (rndNum== 1){
    pattern.gotoAndStop(1);
}

if (rndNum == 2){
    pattern.gotoAndStop(2);
}

if (rndNum == 3){
    pattern.gotoAndStop(3);
}

if (rndNum == 4){
    pattern.gotoAndStop(4);
}

}


//to know if the pencil is clicked
this.pencil1.addEventListener (MouseEvent.CLICK, pen);
this.pencil2.addEventListener (MouseEvent.CLICK, pen);
this.pencil3.addEventListener (MouseEvent.CLICK, pen);
this.pencil4.addEventListener (MouseEvent.CLICK, pen);
this.pencil5.addEventListener (MouseEvent.CLICK, pen);

function pen(event:MouseEvent):void
{
var button:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 1;
trace('total gathered value: ' + gattotal);
event.target.visible = false;


if (gattotal == total){
    gotoAndStop(2);
    }
 }

 this.eraser1.addEventListener (MouseEvent.CLICK, erase);
 this.eraser2.addEventListener (MouseEvent.CLICK, erase);
 this.eraser3.addEventListener (MouseEvent.CLICK, erase);


 function erase(event:MouseEvent):void
 {
 var buttons:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 2;
 trace('total gathered value: ' + gattotal);
event.target.visible = false;

if (gattotal == total){
    gotoAndStop(2);
    }
  }

 this.stapler1.addEventListener (MouseEvent.CLICK, staple);
 this.stapler2.addEventListener (MouseEvent.CLICK, staple);

 function staple(event:MouseEvent):void
 {
 var buttons1:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 5;
 trace('total gathered value: ' + gattotal);
event.target.visible = false;

if (gattotal == total){
    gotoAndStop(2);
    }
 }

 this.clips1.addEventListener (MouseEvent.CLICK, clip);
 this.clips2.addEventListener (MouseEvent.CLICK, clip);
 this.clips3.addEventListener (MouseEvent.CLICK, clip);
 this.clips4.addEventListener (MouseEvent.CLICK, clip);
 this.clips5.addEventListener (MouseEvent.CLICK, clip);
 this.clips6.addEventListener (MouseEvent.CLICK, clip);

 function clip(event:MouseEvent):void
 {
 var buttons2:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 3;
 trace('total gathered value: ' + gattotal);
event.target.visible = false;

if (gattotal == total){
    gotoAndStop(2);
    }
 }

this.glue1.addEventListener (MouseEvent.CLICK, glue);
this.glue2.addEventListener (MouseEvent.CLICK, glue);
this.glue3.addEventListener (MouseEvent.CLICK, glue);
this.glue4.addEventListener (MouseEvent.CLICK, glue);
this.glue5.addEventListener (MouseEvent.CLICK, glue);

function glue(event:MouseEvent):void
{
var buttons3:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 4;
trace('total gathered value: ' + gattotal);
event.target.visible = false;

if (gattotal == total){
    gotoAndStop(2);
    }
}

this.ballpen1.addEventListener (MouseEvent.CLICK, bpen);
this.ballpen2.addEventListener (MouseEvent.CLICK, bpen);
this.ballpen3.addEventListener (MouseEvent.CLICK, bpen);

function bpen(event:MouseEvent):void
{
var buttons4:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 7;
trace('total gathered value: ' + gattotal);
event.target.visible = false;

if (gattotal == total){
    gotoAndStop(2);
    }
 }

this.liquidErase1.addEventListener (MouseEvent.CLICK, leraser);
this.liquidErase2.addEventListener (MouseEvent.CLICK, leraser);
this.liquidErase3.addEventListener (MouseEvent.CLICK, leraser);
this.liquidErase4.addEventListener (MouseEvent.CLICK, leraser);

function leraser(event:MouseEvent):void
{
var buttons5:DisplayObject = DisplayObject(event.target);
gattotal = gattotal + 6;
trace('total gathered value: ' + gattotal);
event.target.visible = false;

if (gattotal == total){
    gotoAndStop(2);
    }
 }

代码很长且没有组织,所以我真的不想发布它:(

1 个答案:

答案 0 :(得分:0)

您的代码的问题在于您向舞台上的每个对象添加了MouseEvent.CLICK个侦听器,并且在每个对象的侦听器函数中,您将1添加到变量gattotal。然后,如果gattotal等于total,则转到不同的框架。您必须想出一种方法来区分您点击的不同对象。例如,您有pencilglueclips,...因此,请在代码中添加布尔值,以便在选择要单击的对象时,请说pencil ,你可以确保点击pencil。因为在您的代码中,如果您点击任何内容,它会引发gattotal。为什么不通过int选择要点击的对象。然后,当您向每个对象添加侦听器时,只需将该进程包含在if语句中,这样您只需将侦听器添加到要单击的对象中。

即)

// top of code
var objChosen:int = 1; // choose which object to be clicked

// [code]

// only add event listeners to the object you want clicked
if(objChosen == 1) {
    this.pencil1.addEventListener (MouseEvent.CLICK, pen);
    this.pencil2.addEventListener (MouseEvent.CLICK, pen);
    this.pencil3.addEventListener (MouseEvent.CLICK, pen);
    this.pencil4.addEventListener (MouseEvent.CLICK, pen);
    this.pencil5.addEventListener (MouseEvent.CLICK, pen);

    function pen(event:MouseEvent):void
    {
        var button:DisplayObject = DisplayObject(event.target);
        gattotal = gattotal + 1;
        trace('total gathered value: ' + gattotal);
        event.target.visible = false;


        if (gattotal == total){
            gotoAndStop(2);
        }
     }
} else if(objChosen == 2) {
    this.eraser1.addEventListener (MouseEvent.CLICK, erase);
    this.eraser2.addEventListener (MouseEvent.CLICK, erase);
    this.eraser3.addEventListener (MouseEvent.CLICK, erase);


    function erase(event:MouseEvent):void
    {
        var buttons:DisplayObject = DisplayObject(event.target);
        gattotal = gattotal + 2;
        trace('total gathered value: ' + gattotal);
        event.target.visible = false;

        if (gattotal == total){
            gotoAndStop(2);
        }
    }
}