|
Flash游戏制作基础:跟随鼠标的曲线(2) tail_nodes = 100; nodes = new Array(); _root.attachMovie("the_head", "the_head", 1, {_x:250, _y:200}); _root.createEmptyMovieClip("the_tail", 2); _root.attachMovie("wall", "wall", 3, {_x:250, _y:200}); for (x=1; x<tail_nodes; x++) { nodes[x] = {x:the_head._x, y:the_head._y}; } the_head.onEnterFrame = function() { this._x = _root._xmouse; this._y = _root._ymouse; the_tail.clear(); the_tail.lineStyle(2, 0x00ff00); the_tail.moveTo(the_head._x, the_head._y); nodes[0] = {x:the_head._x, y:the_head._y}; for (var x = 1; x<tail_nodes-1; ++x) { rotation = Math.atan2(nodes[x].y-nodes[x-1].y, nodes[x].x-nodes[x-1].x); pos_x = nodes[x-1].x+tail_len*Math.cos(rotation); pos_y = nodes[x-1].y+tail_len*Math.sin(rotation); nodes[x] = {x:pos_x, y:pos_y}; if (wall.hitTest(pos_x, pos_y, true)) { the_tail.lineStyle(2, 0xff0000); } the_tail.lineTo(pos_x, pos_y); } };
效果如下。
|