flash小游戏及flash片头等等的flash资源源码都是新丁flash资源网为大家提供娱乐和学习以及为相关flash工作者提供方面之门!
  首页 | 片头欣赏 | Banner 欣赏 | Flash 特效 | 网站欣赏 | 技术文档 | 视频教程 | 网页模板 | flash小游戏 | 源码下载 | 相关下载 | 声音下载 | 
  flash实例应用   您当前的位置是:首页 >> 技术文档 >> flash实例应用 >> 详细内容
由浅入深学习Flash制作高射炮游戏(续)
加入时间:2007-9-23  浏览:26   录入:启能Flash资源网   来源:网络
接着上篇:由浅入深学习Flash制作高射炮游戏我们制作一个完整的游戏。

  上篇讲到了,可以设置一定角度发炮弹了!这时接着做,首先我们把炮弹去掉,只要炮弹出来舞台左、右和下我们就将该MC去掉。

  代码:

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/30;
        this._y += this.diry/30;
        if ((this._x<0) or (this._x>500) or (this._y>350)) {
            this.removeMovieClip();
        }
    };
}

  效果(速度明显变快了):

  然后再继续完善,设置同一时间开火的次数。

Mouse.hide();
gravity = 2;
fired = 0;
max_firepower = 3;
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() {
    if (fired<max_firepower) {
        fired++;
        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/30;
            this._y += this.diry/30;
            if (this._y>350) {
                this.removeMovieClip();
                fired--;
            }
        };
    }
}

  效果(你这时连续按鼠标试试!)

  在舞台上加上一个地面。

Mouse.hide();
gravity = 2;
fired = 0;
max_firepower = 3;
attachMovie("crosshair", "crosshair", 1);
attachMovie("tank", "tank", 2, {_x:295, _y:255});
attachMovie("ground", "ground", 3, {_x:0, _y:200});
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() {
    if (fired<max_firepower) {
        fired++;
        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/30;
            this._y += this.diry/30;
            if ((this._y>350) or (ground.hitTest(this._x, this._y, true))) {
                this.removeMovieClip();
                fired--;
            }
        };
    }
}

  效果如下:

  然后再加上一个敌人。

Mouse.hide();
gravity = 2;
fired = 0;
max_firepower = 3;
place_enemy();
attachMovie("crosshair", "crosshair", 1);
attachMovie("tank", "tank", 2, {_x:295, _y:255});
attachMovie("ground", "ground", 3, {_x:0, _y:200});
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() {
    if (fired<max_firepower) {
        fired++;
        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/30;
            this._y += this.diry/30;
            if ((this._y>350) or (ground.hitTest(this._x, this._y, true))) {
                this.removeMovieClip();
                fired--;
            }
        };
    }
}
function place_enemy() {
    enemy_placed = attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:0, _y:350});
    enemy_placed.yspeed = 0;
    enemy_placed.onEnterFrame = function() {
        this.yspeed += gravity/10;
        this._x++;
        while (_root.ground.hitTest(this._x+this._width/2, this._y+this._height, true)) {
            this._y--;
            this.yspeed = 0;
        }
        if (!_root.ground.hitTest(this._x+this._width/2, this._y+this._height+1, true)) {
            this._y += this.yspeed;
        } else {
            this.yspeed = 0;
        }
        if (this._x>500) {
            this.removeMovieClip();
            place_enemy();
        }
    };
}

  效果如下:

  最后完成。

Mouse.hide();
gravity = 2;
fired = 0;
max_firepower = 3;
place_enemy();
attachMovie("crosshair", "crosshair", 1);
attachMovie("tank", "tank", 2, {_x:295, _y:255});
attachMovie("ground", "ground", 3, {_x:0, _y:200});
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() {
    if (fired<max_firepower) {
        fired++;
        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/30;
            this._y += this.diry/30;
            if ((this._y>350) or (ground.hitTest(this._x, this._y, true))) {
                this.removeMovieClip();
                fired--;
            }
            if (enemy.hitTest(this._x, this._y, true)) {
                this.removeMovieClip();
                enemy.removeMovieClip();
                fired--;
                place_enemy();
            }
        };
    }
}
function place_enemy() {
    enemy_placed = attachMovie("enemy", "enemy", _root.getNextHighestDepth(), {_x:0, _y:350});
    enemy_placed.yspeed = 0;
    enemy_placed.onEnterFrame = function() {
        this.yspeed += gravity/10;
        this._x++;
        while (_root.ground.hitTest(this._x+this._width/2, this._y+this._height, true)) {
            this._y--;
            this.yspeed = 0;
        }
        if (!_root.ground.hitTest(this._x+this._width/2, this._y+this._height+1, true)) {
            this._y += this.yspeed;
        } else {
            this.yspeed = 0;
        }
        if (this._x>500) {
            this.removeMovieClip();
            place_enemy();
        }
    };
}

  最终简单游戏:

  本教程中所用到所有源文件下载:点击这里下载源文件

关于我们 | 联系方式 | 广告赞助 | 免责声明 | 站点留言 | 友情链接 | 推荐站点
Copyright 2007-2008 新丁flash资源网 All Rights Reserved
本站版权所有 新丁flash资源网 保留所有权利 未经许可请勿任意转载或复制使用 
请使用1024*768分辨率浏览本站以达到浏览新丁flash资源网的最佳视觉效果
新丁flash资源网中所有的解压密码均为:www.qnflash.com
粤ICP备08010396号