1 FL 教程Flash 設計物體彈跳游戲 周四 3月 03, 2011 7:13 pm
Admin
Admin
我也不知道該如何起這篇文章的名字,假如您覺得我的文章的名字起的不好,在轉載過程中你可以自己改個好的名字。別忘了告訴我下啊!希望本文對開發Flash游戲入門的人有用。
這個游戲就是,首先設置一個地圖,然后出來一個人(或別的東西都可以),從空中落下來,然后我們利用鍵盤的方向鍵可以控制左右移動,當想向上移動的時候,使用空格鍵可以跳高。簡單研究,希望大家喜歡。轉載必須注明出處和作者,假如加上本站連接,那就感謝你了。
開始給大家講解如何設計。(每一步都有演示動畫,在文章末尾還給出了所有演示動畫的源文件!)
建立Flash文檔,Ctrl J修改影片屬性。
然后建立一個影片剪輯元件,作為地圖來使用,名字為terrain,外形如下圖。
然后再建立兩個按鈕,一個是go,另一個是reset。最后建立一個char影片剪輯元件(看你的繪畫工夫了,建立什么都可以)。
然后分別把三類元件(terrain,char,2個按鈕)都放到場景中來。然后給char影片剪輯元件起實例名walker,地圖terrain的實例名為terrain。然后選擇第一幀,按F9打開動作面板,輸入代碼。
別忘了地圖terrain的實例名為terrain。同樣方法,不給截圖了。
第一幀加入下面代碼:
go=false;//主要是一個變量賦初始值
go按鈕:
on (release) {
go = true;
}//開始運動
reset按鈕:
on (release) {
go = false;
walker._x = 107;
walker._y = 49;
}//回到初始狀態
然后char影片剪輯的action如下:
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
}
onClipEvent (enterFrame) {
if (_root.go) {
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
_y = yspeed;
}
}
效果如下(只要點GO按鈕,物體就會掉下,點reset就會回到初始狀態):
大家通過看上面的代碼可以看到是利用速度總是增加的,所以物體接觸地面時顯得總是動。
改良上面代碼:
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
}
onClipEvent (enterFrame) {
if (_root.go) {
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hitTest(_x, _y _height/2 1, true)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
效果(通過yspeed = 0;使接觸到地面的時候速度變為0):
通過上面兩個步驟我們就設計好了這個游戲的基礎部分。下面我們開始設計游戲使用鍵盤來控制移動。
用鍵盤來左右方向鍵來控制物體左右移動。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
_x -= xspeed;
}
if (Key.isDown(Key.RIGHT)) {
_x = xspeed;
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hitTest(_x, _y _height/2 1, true)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
效果如下:
上面效果,大家通過用鍵盤方向鍵移動,假如物體到了地圖的邊緣,可以移動到外面去。下面我們繼續改良代碼,把物體的移動范圍做一個限制。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
if (!_root.terrain.hitTest(_x-_width/2, _y _height/4, true)) {
_x -= xspeed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.terrain.hitTest(_x _width/2, _y _height/4, true)) {
_x = xspeed;
}
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hitTest(_x, _y _height/2 1, true)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
上面代碼控制了左右兩邊的界限。效果如下(這時就不會移動到外面去了吧!):
下面我們開始改良代碼使該物體能被鍵盤的空格鍵控制跳動。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
if (!_root.terrain.hitTest(_x-_width/2, _y _height/4, true)) {
_x -= xspeed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.terrain.hitTest(_x _width/2, _y _height/4, true)) {
_x = xspeed;
}
}
if (Key.isDown(Key.SPACE)) {
yspeed = -5;
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if ((!_root.terrain.hitTest(_x, _y _height/2 1, true)) or (yspeed<0)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
效果如下(物體移動后按空格鍵試試!!):
大家通過測試上面影片,按空格鍵會不會跳呢??而且跳的還很高啊!!假如你總按空格鍵還可以跳出屏幕呢!哈哈!!!所以下面大家也該知道干什么了!!給跳的范圍做一個限制,你非常聰明的。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
jumping = 0;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
if (!_root.terrain.hitTest(_x-_width/2, _y _height/4, true)) {
_x -= xspeed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.terrain.hitTest(_x _width/2, _y _height/4, true)) {
_x = xspeed;
}
}
if ((Key.isDown(Key.SPACE)) and (!jumping)) {
yspeed = -5;
jumping = 1;
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
jumping = 0;
}
if ((!_root.terrain.hitTest(_x, _y _height/2 1, true)) or (yspeed<0)) {
_y = yspeed;
} else {
yspeed = 0;
jumping = 0;
}
}
}
最終演示效果:
最終一個小游戲就做好了!!不難吧!!希望大家支持我!支持本站。謝謝!差點忘了!!以上所有演示動畫的源文件提供給大家!!!
點擊這里下載源文件(還記得解壓密碼嗎??mid.lt263.com/mb)]
這個游戲就是,首先設置一個地圖,然后出來一個人(或別的東西都可以),從空中落下來,然后我們利用鍵盤的方向鍵可以控制左右移動,當想向上移動的時候,使用空格鍵可以跳高。簡單研究,希望大家喜歡。轉載必須注明出處和作者,假如加上本站連接,那就感謝你了。
開始給大家講解如何設計。(每一步都有演示動畫,在文章末尾還給出了所有演示動畫的源文件!)
建立Flash文檔,Ctrl J修改影片屬性。
然后建立一個影片剪輯元件,作為地圖來使用,名字為terrain,外形如下圖。
然后再建立兩個按鈕,一個是go,另一個是reset。最后建立一個char影片剪輯元件(看你的繪畫工夫了,建立什么都可以)。
然后分別把三類元件(terrain,char,2個按鈕)都放到場景中來。然后給char影片剪輯元件起實例名walker,地圖terrain的實例名為terrain。然后選擇第一幀,按F9打開動作面板,輸入代碼。
別忘了地圖terrain的實例名為terrain。同樣方法,不給截圖了。
第一幀加入下面代碼:
go=false;//主要是一個變量賦初始值
go按鈕:
on (release) {
go = true;
}//開始運動
reset按鈕:
on (release) {
go = false;
walker._x = 107;
walker._y = 49;
}//回到初始狀態
然后char影片剪輯的action如下:
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
}
onClipEvent (enterFrame) {
if (_root.go) {
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
_y = yspeed;
}
}
效果如下(只要點GO按鈕,物體就會掉下,點reset就會回到初始狀態):
大家通過看上面的代碼可以看到是利用速度總是增加的,所以物體接觸地面時顯得總是動。
改良上面代碼:
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
}
onClipEvent (enterFrame) {
if (_root.go) {
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hitTest(_x, _y _height/2 1, true)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
效果(通過yspeed = 0;使接觸到地面的時候速度變為0):
通過上面兩個步驟我們就設計好了這個游戲的基礎部分。下面我們開始設計游戲使用鍵盤來控制移動。
用鍵盤來左右方向鍵來控制物體左右移動。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
_x -= xspeed;
}
if (Key.isDown(Key.RIGHT)) {
_x = xspeed;
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hitTest(_x, _y _height/2 1, true)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
效果如下:
上面效果,大家通過用鍵盤方向鍵移動,假如物體到了地圖的邊緣,可以移動到外面去。下面我們繼續改良代碼,把物體的移動范圍做一個限制。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
if (!_root.terrain.hitTest(_x-_width/2, _y _height/4, true)) {
_x -= xspeed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.terrain.hitTest(_x _width/2, _y _height/4, true)) {
_x = xspeed;
}
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if (!_root.terrain.hitTest(_x, _y _height/2 1, true)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
上面代碼控制了左右兩邊的界限。效果如下(這時就不會移動到外面去了吧!):
下面我們開始改良代碼使該物體能被鍵盤的空格鍵控制跳動。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
if (!_root.terrain.hitTest(_x-_width/2, _y _height/4, true)) {
_x -= xspeed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.terrain.hitTest(_x _width/2, _y _height/4, true)) {
_x = xspeed;
}
}
if (Key.isDown(Key.SPACE)) {
yspeed = -5;
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
}
if ((!_root.terrain.hitTest(_x, _y _height/2 1, true)) or (yspeed<0)) {
_y = yspeed;
} else {
yspeed = 0;
}
}
}
效果如下(物體移動后按空格鍵試試!!):
大家通過測試上面影片,按空格鍵會不會跳呢??而且跳的還很高啊!!假如你總按空格鍵還可以跳出屏幕呢!哈哈!!!所以下面大家也該知道干什么了!!給跳的范圍做一個限制,你非常聰明的。
onClipEvent (load) {
gravity = 0.2;
yspeed = 0;
xspeed = 1;
jumping = 0;
}
onClipEvent (enterFrame) {
if (_root.go) {
if (Key.isDown(Key.LEFT)) {
if (!_root.terrain.hitTest(_x-_width/2, _y _height/4, true)) {
_x -= xspeed;
}
}
if (Key.isDown(Key.RIGHT)) {
if (!_root.terrain.hitTest(_x _width/2, _y _height/4, true)) {
_x = xspeed;
}
}
if ((Key.isDown(Key.SPACE)) and (!jumping)) {
yspeed = -5;
jumping = 1;
}
yspeed = gravity;
while (_root.terrain.hitTest(_x, _y _height/2, true)) {
_y--;
yspeed = 0;
jumping = 0;
}
if ((!_root.terrain.hitTest(_x, _y _height/2 1, true)) or (yspeed<0)) {
_y = yspeed;
} else {
yspeed = 0;
jumping = 0;
}
}
}
最終演示效果:
最終一個小游戲就做好了!!不難吧!!希望大家支持我!支持本站。謝謝!差點忘了!!以上所有演示動畫的源文件提供給大家!!!
點擊這里下載源文件(還記得解壓密碼嗎??mid.lt263.com/mb)]