当前位置:主页>FLASH AS 编程>AS游戏篇>文章内容
  • 由浅入深学习Flash制作高射炮游戏
  • 来源:网页教学网 作者:闪电儿 2007-05-08 【
  主要是利用Flash Actionscript一步一步学习Flash高射炮简单游戏的制作过程,最终效果只是一个简单的演示,如果你有兴趣可以继续深入学习!开篇前,先把所有的演示动画的源程序提供给大家:点击这里下载本教程中所有演示动画的源文件(解压密码:www.webjx.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;
    };

效果如下:





上一篇:45度角地图编辑器0.5   下一篇:flash网游开发日志
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)