用Flash制作一個(gè)類似彈弓的小游戲教程_Flash教程
推薦:Flash動(dòng)畫(huà)技巧:動(dòng)畫(huà)中人物奔跑動(dòng)作的規(guī)律人的奔跑動(dòng)作的基本規(guī)律:人在奔跑中的基本規(guī)律是:身體重心前傾,手臂成屈曲狀,兩手自然握拳,雙腳的跨步動(dòng)作幅度較大,頭的高低變化也比走路動(dòng)作大。在處理
利用Flash制作一個(gè)類似于彈弓的小游戲的教程,主要由Actionscript實(shí)現(xiàn),代碼大家由淺入深來(lái)理解。
思路是預(yù)備三個(gè)小球元件,然后使其中一個(gè)可以拖動(dòng),使之沿著一條線移動(dòng),計(jì)算一下移動(dòng)的角度,最后實(shí)現(xiàn)類似彈弓的一個(gè)小游戲。友情提示,本文中的演示動(dòng)畫(huà)在文章最后提供Fla源文件。
啟動(dòng)Flash首先我們繪制兩個(gè)元件,非常簡(jiǎn)單的。繪制一個(gè)小球然后轉(zhuǎn)變?yōu)橛捌糨嬙�,同樣方法再建議一個(gè)不同顏色的小球的影片剪輯。


然后在主場(chǎng)景中直接輸入如下代碼:
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();
};
測(cè)試效果如下:
然后我們通過(guò)修改上面的代碼,在兩個(gè)小球之間繪制一條線,可以隨中間小球任意移動(dòng)。
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);
}
};
測(cè)試效果。
在這里我們可以很輕松的計(jì)算出小球在線上的角度來(lái)。

稍微做一下改動(dòng)。
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);
}
};
測(cè)試效果如下。
最后我們讓小球自動(dòng)運(yùn)動(dòng)或者你可以用鼠標(biāo)拖動(dòng)小球。
var ball:MovieClip = _root.attachMovie("ball", "ball", 2, {_x:250, _y:100});
var elastic:MovieClip = _root.createEmptyMovieClip("elastic", 1);
var point1:MovieClip = _root.attachMovie("sling", "point1", 3, {_x:50, _y:200});
var point2:MovieClip = _root.attachMovie("sling", "point2", 4, {_x:450, _y:200});
var gravity = 0.1;
var angle1:Number = 0;
var angle2:Number = 0;
var radius:Number = 13.0;
// Or whatever half the width of your ball is.
var elasticCoefficient:Number = 0.001;
// This number will affect the stretchiness of the sling. The greater the number
// the tighter the elastic will be.
var released:Boolean = true;
var forced:Boolean = false;
var acc:Object = {x:0, y:0};
var vel:Object = {x:0, y:0};
_root.onMouseDown = function() {
ball._x = _root._xmouse;
ball._y = _root._ymouse;
ball.startDrag();
_root.released = false;
vel.x = 0;
vel.y = 0;
};
_root.onMouseUp = function() {
ball.stopDrag();
_root.released = true;
};
ball.onPress = function() {
ball.startDrag();
_root.released = false;
vel.x = 0;
vel.y = 0;
};
ball.onRelease = function() {
ball.stopDrag();
_root.released = true;
};
_root.onEnterFrame = function() {
elastic.clear();
elastic.lineStyle(3, 0xFF0000);
if (released) {
ball._x = vel.x;
ball._y = vel.y;
}
if (ball._y>=187) {
/* This area differs from the code in the tutorial.
The reason for that is I didn't read the code, I just looked at the examples.
I try to gain experience with problem-solving by doing things as close to by myself as possible. */
forced = true;
var x1:Number = ball._x-point1._x;
var y1:Number = ball._y-point1._y;
var x2:Number = point2._x-ball._x;
var y2:Number = point2._y-ball._y;
var distance1:Number = Math.sqrt(x1*x1 y1*y1);
var distance2:Number = Math.sqrt(x2*x2 y2*y2);
_root.angle1 = Math.atan2(y1, x1);
_root.angle2 = Math.atan2(y2, x2);
var xOffset:Number = Math.cos(angle1 Math.PI/2)*radius;
var yOffset:Number = Math.sin(angle1 Math.PI/2)*radius;
var xOffset2:Number = Math.cos(angle2 Math.PI/2)*radius;
var yOffset2:Number = Math.sin(angle2 Math.PI/2)*radius;
angle1 = Math.sin(radius/distance1);
angle2 = Math.sin(radius/distance2)*-1;
elastic.moveTo(point1._x, point1._y);
elastic.lineTo(ball._x xOffset, ball._y yOffset);
elastic.moveTo(point2._x, point2._y);
elastic.lineTo(ball._x xOffset2, ball._y yOffset2);
} else {
forced = false;
elastic.moveTo(point1._x, point1._y);
elastic.lineTo(point2._x, point2._y);
}
acc.x = 0;
acc.y = 0;
acc.y = gravity;
if (released && forced) {
/* This section applies the force of the sling to the ball in terms of acceleration based on the stretching of
the sling, in the direction of the displacement, scaled by a coefficient that also encapsulates the mass of
the ball. */
acc.x = distance1*Math.sin(angle2)*elasticCoefficient;
acc.y = -distance1*Math.cos(angle1)*elasticCoefficient;
acc.x = distance2*Math.sin(angle1)*elasticCoefficient;
acc.y = -distance2*Math.cos(angle2)*elasticCoefficient;
}
if (released) {
vel.x = acc.x;
vel.y = acc.y;
}
};
測(cè)試最終效果。
以上演示動(dòng)畫(huà)的所有Fla源文件提供下載:點(diǎn)擊這里下載源文件(解壓密碼:www.hl5o.cn)
分享:Flash教程:AS3.0 實(shí)現(xiàn)FLASH的“動(dòng)態(tài)鏈接庫(kù)”因?yàn)橐郧笆亲?net的開(kāi)發(fā)的,所以很習(xí)慣與DLL文件,覺(jué)得它異常方便,非凡開(kāi)發(fā)大型項(xiàng)目時(shí),分成不同的DLL文件進(jìn)行開(kāi)發(fā),不但節(jié)省了編譯的時(shí)間,也使得程序結(jié)構(gòu)更加完
- as中禁用ESC鍵
- AS3.0 圖片變黑白 圖片彩色變黑白代碼
- flash as3.0 跨域的解決辦法
- 模板無(wú)憂FLASH透明代碼
- Flash教你制作卡通MM眨眼睛動(dòng)畫(huà)
- Flash從零開(kāi)始學(xué)習(xí)創(chuàng)建單選按鈕
- Flash繪制小龍與花插畫(huà)場(chǎng)景
- Flash程序的測(cè)試方法
- Flash CS4文字顏色緩動(dòng)特效
- 網(wǎng)頁(yè)中演示類FLASH動(dòng)畫(huà)制作規(guī)范
- Flash CS3循環(huán)背景的運(yùn)用技巧
- Flash鼠繪技巧教你制作紅綠色的樹(shù)葉
Flash教程Rss訂閱網(wǎng)站制作教程搜索
Flash教程推薦
- Flash 8中文版動(dòng)畫(huà)制作入門
- Flash鼠繪技巧基礎(chǔ):繪制有趣臉形圖
- Flash實(shí)例剖析:碧海浪涌海鷗飛(1)
- 利用Flash動(dòng)畫(huà)讀取IE瀏覽器參數(shù)
- Flash制作四種3D隧道視覺(jué)動(dòng)畫(huà)效果
- 用Flash AS制作逼真的下雨動(dòng)畫(huà)效果
- Photoshop結(jié)合Flash制作瓢蟲(chóng)變色交互動(dòng)畫(huà)(1)
- 由淺入深學(xué)習(xí)Flash制作高射炮游戲
- 獲得動(dòng)畫(huà)在網(wǎng)絡(luò)上地址的小技巧
- Flash制作動(dòng)畫(huà)之掌握基本按鈕知識(shí)(1)
- 相關(guān)鏈接:
- 教程說(shuō)明:
Flash教程-用Flash制作一個(gè)類似彈弓的小游戲教程
。