2013-04-24 21 views
0

我创建一个拖动的益智游戏,我需要解决的问题,二是准确的:将游戏拼图定制调度事件

一)做一个检查,如果从dragArray所有对象变量与matchArray相同。 b)如果是,则显示一个按钮并播放声音文件。 (该按钮* play_btn *和点击时播放声音文件,但我还需要一次让人不解的是解决这么说要播放的声音。)

会增加一些视觉上的援助,但论坛说我需要声望。

期待一些帮助。 游戏基于this tutorial

var dragArray:Array = [p1, p2, p3, p4, p5, p6, p7, p8, p9]; 
var matchArray:Array = [p1_n, p2_n, p3_n, p4_n, p5_n, p6_n, p7_n, p8_n, p9_n]; 

var currentClip:MovieClip; 
var startX:Number; 
var startY:Number; 

for(var i:int = 0; i < dragArray.length; i++) { 
    dragArray[i].buttonMode = true; 
    dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
    matchArray[i].alpha = 0.2; 
} 

function item_onMouseDown(event:MouseEvent):void { 
    currentClip = MovieClip(event.currentTarget); 
    startX = currentClip.x; 
    startY = currentClip.y; 
    addChild(currentClip); //bring to the front 
    currentClip.startDrag(); 
    stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp); 
} 

function stage_onMouseUp(event:MouseEvent):void { 
    stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp); 
    currentClip.stopDrag(); 
    var index:int = dragArray.indexOf(currentClip); 
    var matchClip:MovieClip = MovieClip(matchArray[index]); 
    if(currentClip.hitTestObject(matchClip)) { 
     //a match was made! position the clip on the matching clip: 
     currentClip.x = matchClip.x; 
     currentClip.y = matchClip.y; 
     //make it not draggable anymore: 
     currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown); 
     currentClip.buttonMode = false; 
    } else { 
     //match was not made, so send the clip back where it started: 
     currentClip.x = startX; 
     currentClip.y = startY; 
    } 
} 

var my_sound:Sound = new Sound(); 
my_sound.load(new URLRequest("sounds/song.mp3")); 
var my_channel:SoundChannel = new SoundChannel(); 

play_btn.addEventListener(MouseEvent.CLICK, playSound); 

function playSound(event:MouseEvent):void{ 
my_channel = my_sound.play(); 
} 

回答

0

您可以使用像这样的验证功能。它将返回true,如果所有掉落的物品都是他们的目标,否则为false

function validate(drags:Array, drops:Array):Boolean { 
    var found:uint = 0  
    for (var i:uint = 0;i<drags.length;i++) { 
    var drag:MovieClip = MovieClip(drags[i]); 
    var drop:MovieClip = MovieClip(drops[i]); 
    found += (drag.hitTestObject(drop)) ? 1 : 0 
    } 

    return found == drop.length  
} 

然后你可以用它来检查的全球互动:

var result:Boolean = validate(dragArray,matchArray); 
if (result) { 
    // all ok 
    // play sound... 
} else { 
    // errors 
}