简体   繁体   中英

how can i determine if i click the right movieclip/button? in as3

i am making a game wherein a pattern will appear and the user must click among the items what the pattern has shown example pattern: eraser, pencil, stapler, clip.

so if the user clicked pencil first instead of eraser the game will reset.

also how can i shuffle the items, is it possible just by the codes? i made (5 pencils, 3 erasers, 2 staplers, 6 clips, 3 ballpen, 5 glue, and 4 liquid eraser), or do i need to make every frame and manually shuffle them?

I made the program with the pattern: clip, eraser, glue, clip what i done is give the pattern a value of 12 and then the value of items:pencil = 1, eraser = 2, clip = 3, glue = 4, stapler = 5, liquid eraser = 6, ballpen = 7,

the problem is anything i click as long as it create a value of 12 the user wins, that is why i want to make a detection if the item that is clicked is correct.

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);
    }
 }

the code is long and unorganized so i really don't want to post it :(

The problem with your code is that you add a MouseEvent.CLICK listeners to each object on the stage, and in each object's listener function you add 1 to the variable gattotal . Then if gattotal equals total you go to a different frame. You have to come up with a way to differentiate between the different objects your clicking. For instance you have pencil , glue , clips , ... So add booleans in your code so that when you choose which object you want to be clicked, say pencil , you can make sure it's a pencil being click. Because in your code if you click anything it raises gattotal . Why not choose which object you want to be clicked via an int . Then when you go to add listeners to each object just encompass that process into if statements so that you only add listeners to the object you want clicked.

ie.)

// 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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM