FLASH下载

设为首页
繁體中文
闪客留言

 首页 | FLASH动画 | FLASH专辑 | FLASH短片 | FLASH游戏 | FLASH歌曲 | FLASH教程 | Flash播放器代码
您当前的位置:flash下载 -> FLASH教程 -> AS教程 -> 文章内容  
栏目导航 相关文章
· FLASH基础 · FLASH实例教程
· FLASH技巧 · AS教程


有关碰撞检测的一个应用

作者:转载  来源:[闪客]  发布时间:2006-5-22 21:32:16  编辑人:[FLASH教程]

减小字体 增大字体


原文件下载

  鉴于闪客的更新不够快,推出的教程不够多。本人毛遂自荐,推出一个有关的教程,由于现在又开始上班了,并且近来比较忙,所以现在只有一个上集,请多包涵。

  我们开始。先看看这个程序(比较粗糙,罪过罪过)

  首先:在Flash中建立一个空的组件,名为:dragtrack”。用来捕捉鼠标的当前位置

     在Flash中建立两个挡板,名为:X_bat,Y_bat ;还有一个球:ball

     (刚捕捉来的鼠标坐标就是给它的)。

  下面我们进入正题:

  1。在程序的第一帧开始托拽 "dragtrack"

Start Drag ("/dragtrack", lockcenter)

  2。初始化数据:第一帧

Comment: 设置初始值Set Variable: "gamespeed" = "10"Set Variable: "ballscale" = "100"Set Property ("/ball", X Scale) = ballscaleSet Property ("/ball", Y Scale) = ballscaleSet Variable: "x_step" = "1"Set Variable: "y_step" = ".4"Set Variable: "x_speed" = x_step Set Variable: "y_speed" = y_step

  3。重点部分,即检测判断碰撞部分。   

  首先,简要介绍我要用到的检测碰撞的原理:Flash场景中的两个挡板 X_bat,Y_bat,他们的位置是一个定值,如下图:X_bat,即X方向上的挡板,只能在y轴上上下移动。

  这样:我们就取得了一组数据。再取得球的坐标数据,将两者相减得到一个数dx,如果dx比我们设定的值小,那么球就碰上了挡板。

Action:

Set Variable: "loopcount" = "1"Set Variable: "anglenum" = "40" 模拟碰撞时的角度Loop While (loopcount < gamespeed) Set Variable: "loopcount" = loopcount+1Set Variable: "collisionleft" = FalseSet Variable: "collisionright" = FalseSet Variable: "collisiontop" = FalseSet Variable: "collisionbottom" = "False" Set Variable: "xbat" = GetProperty ( "/dragtrack", _x )Comment: Bat_x 的位置If (xbat < 60)Set Variable: "xbat" = "60"End IfIf (xbat > 340)Set Variable: "xbat" = "340"End IfSet Property ("/bat_y", X Position) = xbat Comment: Bat_y 的位置Set Variable: "ybat" = GetProperty ("/dragtrack", _y )If (ybat < 60)Set Variable: "ybat" = "60"End IfIf (ybat > 340)Set Variable: "ybat" = "340"End IfSet Property ("/bat_x", Y Position) = ybatComment: 碰撞检测Set Variable: "ballx" = GetProperty ( "/ball",_x ) Set Variable: "bally" = GetProperty ( "/ball",_y) If (ballx<5 or ballx>395 or bally<5 or bally>395) (球如果不在规定范围内)Set Variable: "ballx" = "200"Set Variable: "bally" = "200"Set Variable: "gamespeed" = "10"Set Variable: "ballscale" = "100"End If下面取差值Set Variable: "dx1" = ballx-65Set Variable: "dx2" = 335-ballxSet Variable: "dy1" = bally-65Set Variable: "dy2" = 335-ballyComment: dxy 是batx 与ball在y轴上的差值Comment: dyx 是baty 与ball在x轴上的差值Comment: dxy,dyx 用来决定球反弹时的角Set Variable: "dxy" = bally-ybatSet Variable: "dyx" = ballx-xbatComment: 以下检测leftBat碰撞If (dx1>0 and dx1<5 and dxy<50 and dxy>-50) Set Variable: "collisionleft" = trueBegin Tell Target ("/bat_x/bat_x_left")Go to and Play ("x")End Tell Target End If Comment: 以下检测RightBat碰撞 If (dx2>0 and dx2<5 and dxy<50 and dxy>-50 ) Set Variable: "collisionright" = true Begin Tell Target ("/bat_x/bat_x_right")Go to and Play ("x")End Tell TargetEnd IfComment: 以下检测TopBat碰撞If (dy1>0 and dy1<5 and dyx<50 and dyx>-50) Set Variable: "collisiontop" = trueBegin Tell Target ("/bat_y/bat_y_top")Go to and Play ("y")End Tell TargetEnd IfComment: 以下检测BottomBat碰撞If (dy2>0 and dy2<5 and dyx<50 and dyx>-50)Set Variable: "collisionbottom" = trueBegin Tell Target ("/bat_y/bat_y_bottom")Go to and Play ("y")End Tell TargetEnd IfComment: 以下让球运动If (collisionleft=true)Set Variable: "x_step" = x_speedSet Variable: "angle" = dxy/anglenumSet Variable: "y_step" = y_step+angleEnd IfIf (collisionright=true)Set Variable: "gamespeed" = gamespeed+1Set Variable: "x_step" = -x_speedSet Variable: "angle" = dxy/anglenumSet Variable: "y_step" = y_step+angleEnd IfIf (collisiontop=true)Set Variable: "y_step" = y_speedSet Variable: "angle" = dyx/anglenumSet Variable: "x_step" = x_step+angleEnd IfIf (collisionbottom=true)Set Variable: "y_step" = -y_speedSet Variable: "angle" = dyx/anglenumSet Variable: "x_step" = x_step+angleEnd IfSet Property ("/ball", X Position) = ballx+x_stepSet Property ("/ball", Y Position) = bally+y_step End Loop如果球速过大较难玩,所以当球速达到一定速度时,不可再增大If (gamespeed > 20)Set Variable: "gamespeed" = "25"Set Variable: "ballscale" = ballscale - 5 If (ballscale < 10)Set Variable: "ballscale" = "10"End If Set Property ("/ball", X Scale) = ballscale Set Property ("/ball", Y Scale) = ballscaleEnd If 

  4。Go to and Play (2) 第三帧

  相信大家能够明白,中间都有说明,如果还有什么不明白的,可写信给我 Email:zhshg@21cn.com

(完)

[] [返回上一页] [打 印]
下一篇文章:简单的3D旋转按钮
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [查看评论>>>]