文章搜索:
打开支付宝首页搜索“510617664”,即可领红包
  首页--> 计算机--> FLASH教程
 
由浅入深学习Flash制作高射炮游戏
http://www.xxqqss.com 学习轻松网 点击量:4943
 
主要是利用Flash Actionscript一步一步学习Flash高射炮简单游戏的制作过程,最终效果只是一个简单的演示,如果你有兴趣可以继续深入学习!开篇前,先把所有的演示动画的源程序提供给大家:点击这里下载本教程中所有演示动画的源文件

第一步:首先简单的制作一个鼠标动画,绘制一个鼠标的图,自己定。然后选择第一帧输入下面代码:

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

效果如下:

第二步:绘制一个坦克,分成两部分,如下面:

按此在新窗口浏览图片

把下面的命名实例名为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;
    };

效果如下:



来源:网页教学网  作者:闪电儿


打印】 【关闭
免责声明 :本站刊载此文不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。对本文有任何异议,请发送fiyeadwyv@163.com
最热门点击排行
 
版权所有 2006-2021:学习轻松网 Copyright http://www.xxqqss.com All rights reserved 浙ICP备14013707号-1
广告联系电话: 13958357030 联系QQ:9792414 工具条下载