由浅入深学习Flash制作高射炮游戏_Flash教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:Flash实例“蝶恋花”制作过程深入剖析
通过这个蝴蝶在花丛中翻飞的实例我们可以学习Flash图层、引导线运动、帧与补间动画等技术。主要使用工具:箭头工具(选择工具)、任意变形工具、铅笔工具。请大

  主要是利用Flash Actionscript一步一步学习Flash高射炮简单游戏的制作过程,最终效果只是一个简单的演示,假如你有爱好可以继续深入学习!

  开篇前,先把所有的演示动画的源程序提供给大家:

  点击这里下载本教程中所有演示动画的源文件(解压密码:www.mb5u.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教程