教程首页 | 基础教程 | 专题教程| 视频教程 | 高级应用 | 源码 |素材 | 书籍

有关碰撞检测的一个应用

2008-12-25 15:03:33 来源: 作者:FlashGun
  •   这是闪客FlashGun制作的一个比较有趣的碰撞检测例子,我们可以通过玩这个游戏学到很多编写脚本的知识。

原文件下载

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

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

  首先:在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) = ballscale

Set Property ("/ball", Y Scale) = ballscale
Set 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+1
Set Variable: "collisionleft" = False
Set Variable: "collisionright" = False
Set Variable: "collisiontop" = False
Set Variable: "collisionbottom" = "False" 
Set Variable: "xbat" = GetProperty ( "/dragtrack", _x )

Comment: Bat_x 的位置

If (xbat < 60)
Set Variable: "xbat" = "60"
End If

If (xbat > 340)
Set Variable: "xbat" = "340"
End If

Set Property ("/bat_y", X Position) = xbat Comment: Bat_y 的位置
Set Variable: "ybat" = GetProperty ("/dragtrack", _y )

If (ybat < 60)
Set Variable: "ybat" = "60"
End If

If (ybat > 340)
Set Variable: "ybat" = "340"
End If

Set Property ("/bat_x", Y Position) = ybat

Comment: 碰撞检测

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-65
Set Variable: "dx2" = 335-ballx
Set Variable: "dy1" = bally-65
Set Variable: "dy2" = 335-bally
Comment: dxy 是batx 与ball在y轴上的差值
Comment: dyx 是baty 与ball在x轴上的差值
Comment: dxy,dyx 用来决定球反弹时的角
Set Variable: "dxy" = bally-ybat
Set Variable: "dyx" = ballx-xbat
Comment: 以下检测leftBat碰撞
If (dx1>0 and dx1<5 and dxy<50 and dxy>-50) 
Set Variable: "collisionleft" = true
Begin 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 Target
End If

Comment: 以下检测TopBat碰撞
If (dy1>0 and dy1<5 and dyx<50 and dyx>-50) 
Set Variable: "collisiontop" = true
Begin Tell Target ("/bat_y/bat_y_top")
Go to and Play ("y")
End Tell Target
End If

Comment: 以下检测BottomBat碰撞
If (dy2>0 and dy2<5 and dyx<50 and dyx>-50)
Set Variable: "collisionbottom" = true
Begin Tell Target ("/bat_y/bat_y_bottom")
Go to and Play ("y")
End Tell Target
End If

Comment: 以下让球运动

If (collisionleft=true)
Set Variable: "x_step" = x_speed
Set Variable: "angle" = dxy/anglenum
Set Variable: "y_step" = y_step+angle
End If

If (collisionright=true)
Set Variable: "gamespeed" = gamespeed+1
Set Variable: "x_step" = -x_speed
Set Variable: "angle" = dxy/anglenum
Set Variable: "y_step" = y_step+angle
End If

If (collisiontop=true)
Set Variable: "y_step" = y_speed
Set Variable: "angle" = dyx/anglenum
Set Variable: "x_step" = x_step+angle
End If

If (collisionbottom=true)
Set Variable: "y_step" = -y_speed
Set Variable: "angle" = dyx/anglenum
Set Variable: "x_step" = x_step+angle
End If

Set Property ("/ball", X Position) = ballx+x_step
Set 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) = ballscale
End If 

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

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

(完)

评分
0
共有0人参加评分
请你提交评分
呢称:
共有0条评论
免责条款 - 商务合作 - 网站地图 - 帮助 - 联系我们 - 自动访问本站
Copyright ?2008 like-flash.com All Rights Reserved 浙ICP备08009225号