网页教学网
 当前位置: 网页教学网 >> 动画制作 >> Flash实例教学 >> 阅读:Flash游戏制作基础:跟随鼠标的曲线
[ HTML ] [ FW ] [ DW ] [ FP ] [ JS ] [ XML ] [ CSS ] [ 图象 ] [ FLASH ] [ .NET ] [ ASP ] [ JSP ] [ PHP ] [ 数据 ] [ 系统 ] [ 安全 ] [ 素材 ] [ 建站 ] [ 主机 ] [ 入门 ] [ 技巧 ]

Flash游戏制作基础:跟随鼠标的曲线

http://www.webjx.com  更新日期:2007-07-15 23:13  出处:网页教学网  作者:闪电儿 收藏本文

Flash游戏制作基础,跟随鼠标的曲线,曲线和其它物体之间进行碰撞检测。网页教学网友情提示文章末尾提供Fla源文件的下载。

首先按Ctrl+J修改属性。

Flash游戏制作基础:跟随鼠标的曲线

创建一个MC,如下图。是放大到800%的效果。

Flash游戏制作基础:跟随鼠标的曲线

然后直接使用鼠标跟随,下面代码直接放到第一帧,创建轨迹。

tail_len = 2;
tail_nodes = 100;
nodes = new Array();
_root.attachMovie("the_head", "the_head", 1, {_x:250, _y:200});
_root.createEmptyMovieClip("the_tail", 2);
for (x=1; x<tail_nodes; x++) {
    nodes[x] = {x:the_head._x, y:the_head._y};
}
the_head.onEnterFrame = function() {
    this._x = _root._xmouse;
    this._y = _root._ymouse;
    the_tail.clear();
    the_tail.lineStyle(2, 0x00ff00);
    the_tail.moveTo(the_head._x, the_head._y);
    nodes[0] = {x:the_head._x, y:the_head._y};
    for (var x = 1; x<tail_nodes-1; ++x) {
        rotation = Math.atan2(nodes[x].y-nodes[x-1].y, nodes[x].x-nodes[x-1].x);
        pos_x = nodes[x-1].x+tail_len*Math.cos(rotation);
        pos_y = nodes[x-1].y+tail_len*Math.sin(rotation);
        nodes[x] = {x:pos_x, y:pos_y};
        the_tail.lineTo(pos_x, pos_y);
    }
};

  演示效果如下。

然后再建立一个MC设置如下,做一面墙来检测碰撞。

Flash游戏制作基础:跟随鼠标的曲线

添加一个物体,来实验碰撞检测。添加如下Action到主场景第一帧。

tail_len = 2;
tail_nodes = 100;
nodes = new Array();
_root.attachMovie("the_head", "the_head", 1, {_x:250, _y:200});
_root.createEmptyMovieClip("the_tail", 2);
_root.attachMovie("wall", "wall", 3, {_x:250, _y:200});
for (x=1; x<tail_nodes; x++) {
    nodes[x] = {x:the_head._x, y:the_head._y};
}
the_head.onEnterFrame = function() {
    this._x = _root._xmouse;
    this._y = _root._ymouse;
    the_tail.clear();
    the_tail.lineStyle(2, 0x00ff00);
    the_tail.moveTo(the_head._x, the_head._y);
    nodes[0] = {x:the_head._x, y:the_head._y};
    for (var x = 1; x<tail_nodes-1; ++x) {
        rotation = Math.atan2(nodes[x].y-nodes[x-1].y, nodes[x].x-nodes[x-1].x);
        pos_x = nodes[x-1].x+tail_len*Math.cos(rotation);
        pos_y = nodes[x-1].y+tail_len*Math.sin(rotation);
        nodes[x] = {x:pos_x, y:pos_y};
        if (wall.hitTest(pos_x, pos_y, true)) {
            the_tail.lineStyle(2, 0xff0000);
        }
        the_tail.lineTo(pos_x, pos_y);
    }
};

  效果如下。

点击这里下载源文件

关键词:
推荐给好友】【关闭
最新五条评论
查看全部评论
评论总数 0
您的评论
用户名: 新注册) 密 码: 匿名:
·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为