由淺入深學習Flash制作高射炮游戲_Flash教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:Flash實例“蝶戀花”制作過程深入剖析
      通過這個蝴蝶在花叢中翻飛的實例我們可以學習Flash圖層、引導線運動、幀與補間動畫等技術。主要使用工具:箭頭工具(選擇工具)、任意變形工具、鉛筆工具。請大

        主要是利用Flash Actionscript一步一步學習Flash高射炮簡單游戲的制作過程,最終效果只是一個簡單的演示,假如你有愛好可以繼續深入學習!

        開篇前,先把所有的演示動畫的源程序提供給大家:

        點擊這里下載本教程中所有演示動畫的源文件(解壓密碼:www.wf0088.com)

        第一步:首先簡單的制作一個鼠標動畫,繪制一個鼠標的圖,自己定。然后選擇第一幀輸入下面代碼:

      Mouse.hide();
      attachMovie("crosshair", "crosshair", 1);
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };

        效果如下:

        第二步:繪制一個坦克,分成兩部分,如下面:

      由淺入深學習Flash制作高射炮游戲

        把下面的命名實例名為tank

        代碼如下:

      Mouse.hide();
      attachMovie("crosshair", "crosshair", 1);
      attachMovie("tank", "tank", 2, {_x:230, _y:350});
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };
      tank.onEnterFrame = function() {
      mousex = _xmouse-this._x;
      mousey = (_ymouse-this._y)*-1;
      angle = Math.atan(mousey/mousex)/(Math.PI/180);
      if (mousex<0) {
      angle = 180;
      }
      if (mousex>=0 && mousey<0) {
      angle = 360;
      }
      this.cannon._rotation = angle*-1;
      };

        效果(無角度限制):

        第三步:我這里設置轉動的一定的角度。

      Mouse.hide();
      attachMovie("crosshair", "crosshair", 1);
      attachMovie("tank", "tank", 2, {_x:230, _y:350});
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };
      tank.onEnterFrame = function() {
      mousex = _xmouse-this._x;
      mousey = (_ymouse-this._y)*-1;
      angle = Math.atan(mousey/mousex)/(Math.PI/180);
      if (mousex<0) {
      angle = 180;
      }
      if (mousex>=0 && mousey<0) {
      angle = 360;
      }
      if (angle>160) {
      angle = 160;
      }
      if (angle<20) {
      angle = 20;
      }
      this.cannon._rotation = angle*-1;
      };

        效果如下:

        然后是計算開火的目標:

      Mouse.hide();
      attachMovie("crosshair", "crosshair", 1);
      attachMovie("tank", "tank", 2, {_x:230, _y:350});
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };
      tank.onEnterFrame = function() {
      mousex = _xmouse-this._x;
      mousey = (_ymouse-this._y)*-1;
      angle = Math.atan(mousey/mousex)/(Math.PI/180);
      if (mousex<0) {
      angle = 180;
      }
      if (mousex>=0 && mousey<0) {
      angle = 360;
      }
      if (angle>160) {
      angle = 160;
      }
      if (angle<20) {
      angle = 20;
      }
      firepower = Math.sqrt(mousex*mousex mousey*mousey);
      if (firepower>200) {
      firepower = 200;
      }
      this.cannon._rotation = angle*-1;
      };

        開火的制作:

      Mouse.hide();
      attachMovie("crosshair", "crosshair", 1);
      attachMovie("tank", "tank", 2, {_x:230, _y:350});
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };
      tank.onEnterFrame = function() {
      mousex = _xmouse-this._x;
      mousey = (_ymouse-this._y)*-1;
      angle = Math.atan(mousey/mousex)/(Math.PI/180);
      if (mousex<0) {
      angle = 180;
      }
      if (mousex>=0 && mousey<0) {
      angle = 360;
      }
      if (angle>160) {
      angle = 160;
      }
      if (angle<20) {
      angle = 20;
      }
      firepower = Math.sqrt(mousex*mousex mousey*mousey);
      if (firepower>200) {
      firepower = 200;
      }
      this.cannon._rotation = angle*-1;
      };
      function onMouseDown() {
      angle = tank.cannon._rotation-1
      start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);
      start_ball_y = tank._y 48*Math.sin(angle*Math.PI/180);
      attachMovie("cannonball", "cannonball", 3, {_x:start_ball_x, _y:start_ball_y});
      }

        效果如下:

        開火出來炮彈:

      Mouse.hide();
      attachMovie("crosshair", "crosshair", 1);
      attachMovie("tank", "tank", 2, {_x:230, _y:350});
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };
      tank.onEnterFrame = function() {
      mousex = _xmouse-this._x;
      mousey = (_ymouse-this._y)*-1;
      angle = Math.atan(mousey/mousex)/(Math.PI/180);
      if (mousex<0) {
      angle = 180;
      }
      if (mousex>=0 && mousey<0) {
      angle = 360;
      }
      if (angle>160) {
      angle = 160;
      }
      if (angle<20) {
      angle = 20;
      }
      firepower = Math.sqrt(mousex*mousex mousey*mousey);
      if (firepower>200) {
      firepower = 200;
      }
      this.cannon._rotation = angle*-1;
      };
      function onMouseDown() {
      angle = tank.cannon._rotation-1;
      start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);
      start_ball_y = tank._y 48*Math.sin(angle*Math.PI/180);
      cannonball_fired = attachMovie("cannonball", "cannonball_" _root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
      cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*firepower;
      cannonball_fired.diry = Math.sin(angle*Math.PI/180)*firepower;
      cannonball_fired.onEnterFrame = function() {
      this._x = this.dirx/50;
      this._y = this.diry/50;
      };
      }

        效果如下:

        最后再加以具體的限制一下炮彈:

      Mouse.hide();
      gravity = 2;
      attachMovie("crosshair", "crosshair", 1);
      attachMovie("tank", "tank", 2, {_x:230, _y:350});
      crosshair.onEnterFrame = function() {
      this._x = _xmouse;
      this._y = _ymouse;
      };
      tank.onEnterFrame = function() {
      mousex = _xmouse-this._x;
      mousey = (_ymouse-this._y)*-1;
      angle = Math.atan(mousey/mousex)/(Math.PI/180);
      if (mousex<0) {
      angle = 180;
      }
      if (mousex>=0 && mousey<0) {
      angle = 360;
      }
      if (angle>160) {
      angle = 160;
      }
      if (angle<20) {
      angle = 20;
      }
      firepower = Math.sqrt(mousex*mousex mousey*mousey);
      if (firepower>200) {
      firepower = 200;
      }
      this.cannon._rotation = angle*-1;
      };
      function onMouseDown() {
      angle = tank.cannon._rotation-1;
      start_ball_x = tank._x 48*Math.cos(angle*Math.PI/180);
      start_ball_y = tank._y 48*Math.sin(angle*Math.PI/180);
      cannonball_fired = attachMovie("cannonball", "cannonball_" _root.getNextHighestDepth(), _root.getNextHighestDepth(), {_x:start_ball_x, _y:start_ball_y});
      cannonball_fired.dirx = Math.cos(angle*Math.PI/180)*firepower;
      cannonball_fired.diry = Math.sin(angle*Math.PI/180)*firepower;
      cannonball_fired.onEnterFrame = function() {
      this.diry = gravity;
      this._x = this.dirx/50;
      this._y = this.diry/50;
      };
      }

        效果如下:

      分享:Flash8制作具有搜索功能的電話本
      利用Flash8ActionScript制作一個簡單的電話本搜索程序。先看效果(姓名中輸入Sven,Michel,Jack,Charly和Ana可以查看到結果):點擊這里下載源文件(解

      /所屬分類:Flash教程/更新時間:2008-03-05
      相關Flash教程