这里,主要为游戏定义几个函数,它们共同实现了方块游戏的基本功能。其中,函数hitothers用于判断下落的方块是否与已经落下的方块接触,如果接触,就需要停止下落。函数attachsquares用在当方块停止下落时将库中的“square”元件附加到适当的位置。函数judgescore判断方块停止下落并附加小方块后是否得分,它是通过分析与方块对应位置的数组元素的值完成判断的。 (2)选中主时间轴的第2帧,在其“动作”面板中添加如下代码:
|
stop();
_root.restart.removeMovieClip(); _root.currentshape = 0; _root.iii = 1; _root.fall = true; _root.wait = 0; _root.v = 6-Number(_root.top.v); _root.hit = false; _root.top.duplicateMovieClip("top", 99999); _root.top.score = 0; _root.ran1 = random(7)+1; _root.box = new Array(); for (var i = 0; i<=29; i++) { _root.box[i] = new Array(); for (var j = 0; j<=15; j++) { _root.box[i][j] = 0; } } Key.addListener(_root); _root.onKeyDown = function() { if (Key.getCode() == Key.LEFT) { _root.currentmove._x -= 10; hitothers(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); if (_root.currentmove.hitTest(_root.left) || _root.hit == true) { _root.currentmove._x += 10; } } else if (Key.getCode() == Key.RIGHT) { _root.currentmove._x += 10; hitothers(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); if (_root.currentmove.hitTest(_root.right) || _root.hit == true) { _root.currentmove._x -= 10; } } else if (Key.getCode() == Key.DOWN) { _root.fall = false; _root.currentmove._y += 10; hitothers(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); if (_root.currentmove.hitTest(_root.base) || _root.hit == true) { _root.currentmove._y -= 10; attachsquares(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); removeMovieClip(_root.currentmove); _root.currentshape = 0; } _root.fall = true; } else if (Key.getCode() == Key.UP) { _root.currentmove._rotation += 90; hitothers(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); if (_root.currentmove.hitTest(_root.right) || _root.currentmove.hitTest(_root.left) || _root.hit == true) { _root.currentmove._rotation -= 90; } } }; _root.onEnterFrame = function() { if (_root.currentshape == 0) { _root.ran = _root.ran1; _root.attachMovie("shape"+_root.ran, "currentmove", 100); _root.currentmove._x = 120; _root.currentmove._y = 40; _root.currentshape = _root.ran; _root.currentmove.onEnterFrame = function() { if (_root.fall == true) { if (_root.wait == _root.v) { this._y += 10; _root.wait = 0; } else { _root.wait++; } } }; _root.ran1 = random(7)+1; _root.attachMovie("shape"+_root.ran1, "nextmove", 100001); _root.nextmove._x = 200; _root.nextmove._y = 20; } hitothers(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); if ((_root.hit == true || _root.currentmove.hitTest(_root.base)) && (_root.fall == true)) { _root.currentmove._y -= 10; attachsquares(_root.currentmove._x, _root.currentmove._y, _root.ran, _root.currentmove._rotation); removeMovieClip(_root.currentmove); _root.currentshape = 0; } };
|
这一帧实现游戏的运行,通过定义事件处理函数分析键盘动作,以实现用方向键对方块进行控制。在方块下落过程,适时地调用第1帧中定义的函数,保证了游戏的严密性。
(3)双击第2帧中的“界面”元件,进入其元件层级,选中“加速”按钮,在其“动作”面板中添加如下代码:
|
on (release) {
if (_root.top.v<6) { _root.top.v = Number(_root.top.v)+1; } _root.wait = 0; _root.v = 6-Number(_root.top.v); }
|
|