0

我正在创建一个拖动益智游戏,我需要解决一个问题,确切地说是两个:

a) 检查dragArray变量中的所有对象是否与 matchArray 中的对象位于同一位置。b) 如果是,则显示一个按钮并播放声音文件。(按钮是*play_btn*,点击它会播放声音文件,但我也需要在解谜后播放声音。)

会添加一些视觉帮助,但论坛说我需要声誉。

期待一些帮助。游戏基于本教程

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

1 回答 1

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
}
于 2013-04-24T22:12:18.023 回答