|
我们用Flash ActionScript代码制作一个模拟弹弓弓弦弹性形变的小游戏教程,我们一步一步由浅入深来详细说明制作步骤。
思路:准备三个小球元件,使其中一个小球可以拖动,在使它沿着一条线移动,用ActionScript代码计算移动的角度,最后实现弹弓弓弦弹性形变的一个互动游戏。
一、Flash制作小球随鼠标拖动
启动Flash,首先我们绘制两个元件,非常简单的。绘制一个小球然后转变为影片剪辑元件,同样方法再建议一个不同颜色的小球的影片剪辑。


然后在主场景中直接输入如下代码:
attachMovie(sling, sling_1, _root.getNextHighestDepth(), {_x:20, _y:200}); attachMovie(sling, sling_2, _root.getNextHighestDepth(), {_x:480, _y:200}); attachMovie(ball, ball, _root.getNextHighestDepth(), {_x:250, _y:100}); _root.createEmptyMovieClip(elastic, _root.getNextHighestDepth()); ball.onPress = function() { startDrag(this); }; ball.onRelease = function() { stopDrag(); };
二、Flash制作随小球移动的弦
然后我们通过修改上面的代码,在两个小球之间绘制一条线,可以随中间小球任意移动。
attachMovie(sling, sling_1, _root.getNextHighestDepth(), {_x:20, _y:200}); attachMovie(sling, sling_2, _root.getNextHighestDepth(), {_x:480, _y:200}); attachMovie(ball, ball, _root.getNextHighestDepth(), {_x:250, _y:100}); _root.createEmptyMovieClip(elastic, _root.getNextHighestDepth()); ball.onPress = function() { startDrag(this); }; ball.onRelease = function() { stopDrag(); }; elastic.onEnterFrame = function() { this.clear(); this.lineStyle(2, 0x009900); this.moveTo(sling_1._x, sling_1._y); if (ball._y>182) { dist_x = ball._x-sling_1._x; dist_y = ball._y-sling_1._y; distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y); elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18); angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling); this.lineTo(sling_1._x+elastic_length*Math.cos(angle), sling_1._y+elastic_length*Math.sin(angle)); } else { this.lineTo(sling_2._x, sling_2._y); } this.moveTo(sling_2._x, sling_2._y); if (ball._y>182) { dist_x = ball._x-sling_2._x; dist_y = ball._y-sling_2._y; distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y); elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18); angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling)*-1; this.lineTo(sling_2._x+elastic_length*Math.cos(angle), sling_2._y+elastic_length*Math.sin(angle)); } else { this.lineTo(sling_2._x, sling_2._y); } };
三、Flash计算弦的形变角度
在这里我们可以很轻松的计算出小球在线上的角度来。

稍微做一下改动。
attachMovie(sling, sling_1, _root.getNextHighestDepth(), {_x:20, _y:200}); attachMovie(sling, sling_2, _root.getNextHighestDepth(), {_x:480, _y:200}); attachMovie(ball, ball, _root.getNextHighestDepth(), {_x:250, _y:100}); _root.createEmptyMovieClip(elastic, _root.getNextHighestDepth()); xspeed = 0; yspeed = 0; fire = false; ball.onPress = function() { fire = false; startDrag(this); }; ball.onRelease = function() { stopDrag(); fire = true; }; ball.onEnterFrame = function() { if (fire) { this._x += (xspeed*0.001); this._y += (yspeed*0.001); } }; elastic.onEnterFrame = function() { this.clear(); this.lineStyle(2, 0x009900); this.moveTo(sling_1._x, sling_1._y); if (ball._y>182) { dist_x = ball._x-sling_1._x; dist_y = ball._y-sling_1._y; distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y); elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18); angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling); saved_angle = angle; this.lineTo(sling_1._x+elastic_length*Math.cos(angle), sling_1._y+elastic_length*Math.sin(angle)); this.lineTo(ball._x,ball._y); } else { this.lineTo(sling_2._x, sling_2._y); } this.moveTo(sling_2._x, sling_2._y); if (ball._y>182) { dist_x = ball._x-sling_2._x; dist_y = ball._y-sling_2._y; distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y); elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18); angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling)*-1; this.lineTo(sling_2._x+elastic_length*Math.cos(angle), sling_2._y+elastic_length*Math.sin(angle)); this.lineTo(ball._x,ball._y); } else { this.lineTo(sling_2._x, sling_2._y); } };
三、Flash计算弦的形变角度
在这里我们可以很轻松的计算出小球在线上的角度来。

稍微做一下改动。
attachMovie(sling, sling_1, _root.getNextHighestDepth(), {_x:20, _y:200}); attachMovie(sling, sling_2, _root.getNextHighestDepth(), {_x:480, _y:200}); attachMovie(ball, ball, _root.getNextHighestDepth(), {_x:250, _y:100}); _root.createEmptyMovieClip(elastic, _root.getNextHighestDepth()); xspeed = 0; yspeed = 0; fire = false; ball.onPress = function() { fire = false; startDrag(this); }; ball.onRelease = function() { stopDrag(); fire = true; }; ball.onEnterFrame = function() { if (fire) { this._x += (xspeed*0.001); this._y += (yspeed*0.001); } }; elastic.onEnterFrame = function() { this.clear(); this.lineStyle(2, 0x009900); this.moveTo(sling_1._x, sling_1._y); if (ball._y>182) { dist_x = ball._x-sling_1._x; dist_y = ball._y-sling_1._y; distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y); elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18); angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling); saved_angle = angle; this.lineTo(sling_1._x+elastic_length*Math.cos(angle), sling_1._y+elastic_length*Math.sin(angle)); this.lineTo(ball._x,ball._y); } else { this.lineTo(sling_2._x, sling_2._y); } this.moveTo(sling_2._x, sling_2._y); if (ball._y>182) { dist_x = ball._x-sling_2._x; dist_y = ball._y-sling_2._y; distance_from_sling = Math.sqrt(dist_x*dist_x+dist_y*dist_y); elastic_length = Math.sqrt(distance_from_sling*distance_from_sling-18*18); angle = Math.atan2(dist_y, dist_x)+Math.asin(18/distance_from_sling)*-1; this.lineTo(sling_2._x+elastic_length*Math.cos(angle), sling_2._y+elastic_length*Math.sin(angle)); this.lineTo(ball._x,ball._y); } else { this.lineTo(sling_2._x, sling_2._y); } };
详解Flash AS代码实现弹弓弓弦弹性形变 |