万卷书   首页 > 电脑书籍 > Flash > 用flashMX制作迷宫游戏(2)
 
  到了这里,应该是差不多了,就差一个我们控制的方块怎么检测和迷宫的碰撞以及判断谁先到对方的起点。这个我们在maze这个mc中解决:

鼠标右击maze,选actions,我们会看到如下的actions:

onClipEvent (enterFrame) {//:响应mcclip的enterframe事件
if (_root.started && _root._currentframe == 1) {//:判断标志变量start=true及根目录为第一帧,则开始
with (_root.square) {//:对根目录下的square这个mc
//
// 键盘控制
if (Key.isDown(Key.DOWN)) {
_y += 1;
}
if (Key.isDown(Key.UP)) {
_y -= 1;
}
if (Key.isDown(Key.LEFT)) {
_x -= 1;
}
if (Key.isDown(Key.RIGHT)) {
_x += 1;
}
//
//检测玩家的方块是否和迷宫的墙发生碰撞
if (walls.hitTest(getBounds(_root).xMax, _y, true)) {
_x -= 1;
}
if (walls.hitTest(getBounds(_root).xMin, _y, true)) {
_x += 1;
}
if (walls.hitTest(_x, getBounds(_root).yMax, true)) {
_y -= 1;
}
if (walls.hitTest(_x, getBounds(_root).yMin, true)) {
_y += 1;
}
//
// 检测方块,要是方块和goal碰撞,就跳转到根目录下的第三帧。
if (_root.goal.hitTest(_x, getBounds(_root).yMax, true)) {
_root.gotoandstop(3);
}
}
}
}

  当然第三帧上,早就放好了我们想要的显示的内容,比如你蠃了等。

  至此,我们的迷宫游戏就全部完成了,相信你也学会了一些东西。

上一页