2013-11-20 32 views
0

我正在做一个游戏,其中将出现一个模式,用户必须在项目中点击该模式显示的示例模式:橡皮擦,铅笔,订书机,剪辑。如何确定我是否点击正确的动画片段/按钮?在as3

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

也我该如何洗牌的项目,是否有可能只是由代码?我做了(5支铅笔,3把橡皮,2把订书机,6把夹子,3支圆珠笔,5支胶水和4支液体橡皮擦),还是我需要制作每一帧并手动洗牌?

我用图案制作程序:剪辑,橡皮擦,胶水,剪辑 我所做的是给模式赋值12 ,然后item的值为:pencil = 1,eraser = 2,clip = 3 ,胶水= 4,订书机= 5,液体橡皮擦= 6,ballpen = 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); 
    } 
} 

代码是漫长的,无组织的,所以我真的不想将它张贴:(

+0

你能编辑您的文章,并添加在你迄今为止编写的代码?它使得它更容易帮助你,你是否在时间轴上或类文件中编写代码? –

+0

我使用了时间线,我会发布代码 – BlueDragon

回答

0

与您的代码的问题是,你添加一个MouseEvent.CLICK听众在舞台上的每个对象,并在每个对象的侦听器函数将1加到变量gattotal中,然后如果gattotal等于total则转到另一个框架,您必须想出一种方法来区分不同的对象,例如您有pencil,glue,clips ,...因此,在你的代码中添加布尔值,以便当你选择你想要的对象时被点击,例如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); 
     } 
    } 
} 
+0

我看到谢谢你 – BlueDragon

相关问题