游戏本身还是比较简单的。
但是在检测碰撞时
涉及到了 不规则形状的碰撞
===============
知识点如下:
1:点pt 对象的x y 属性,必须时x 与y ,不是_x 与 _y
2:movieClip.getBounds()
该方法得到元件的四个极值 xMin xMax yMin yMax
3:movieClip.localToGlobal(pt)
可以将任何给定的x和y左边从相对于这个特定的mc左上角的值
转换到相对于舞台左上角的坐标。
=========================
1:新建两个元件 m 与ball
并在实例名称分别命名:m 与 ball
2:代码开始在主时间轴
1stop();
2ball.onPress = function() {
3 this.startDrag();//开始拖拽
4};
5ball.onRelease = function() {
6 this.stopDrag();//结束拖拽
7};
8//声明2个点对象,因为每个点对象接受x,y,但极值有四个。所以创建2个点对象。
9var ptMin:Object = new Object();
10var ptMax:Object = new Object();
11var thisBounds:Object = new Object();
12//这个对象里获取了ball元件的极值
13thisBounds = ball.getBounds();
14m.onEnterFrame = function() {
15 //为点对象添加x y 属性一定要在onEnterFrame事件中
16 ptMin.x = thisBounds.xMin;
17 ptMin.y = thisBounds.yMin;
18 ptMax.x = thisBounds.xMax;
19 ptMax.y = thisBounds.yMax;
20 //将相对于ball内的左上角的值,转变为相对于舞台的左上角的值
21 ball.localToGlobal(ptMin);
22 ball.localToGlobal(ptMax);
23 if (this.hitTest(ptMin.x, ptMin.y, true)) {
24 trace("minx miny");
25 } else if (this.hitTest(ptMin.x, ptMax.y, true)) {
26 trace("minx maxy");
27 } else if (this.hitTest(ptMax.x, ptMin.y, true)) {
28 trace("maxx miny");
29 } else if (this.hitTest(ptMax.x, ptMax.y, true)) {
30 trace("maxx maxy");
31 }
32};
2ball.onPress = function() {
3 this.startDrag();//开始拖拽
4};
5ball.onRelease = function() {
6 this.stopDrag();//结束拖拽
7};
8//声明2个点对象,因为每个点对象接受x,y,但极值有四个。所以创建2个点对象。
9var ptMin:Object = new Object();
10var ptMax:Object = new Object();
11var thisBounds:Object = new Object();
12//这个对象里获取了ball元件的极值
13thisBounds = ball.getBounds();
14m.onEnterFrame = function() {
15 //为点对象添加x y 属性一定要在onEnterFrame事件中
16 ptMin.x = thisBounds.xMin;
17 ptMin.y = thisBounds.yMin;
18 ptMax.x = thisBounds.xMax;
19 ptMax.y = thisBounds.yMax;
20 //将相对于ball内的左上角的值,转变为相对于舞台的左上角的值
21 ball.localToGlobal(ptMin);
22 ball.localToGlobal(ptMax);
23 if (this.hitTest(ptMin.x, ptMin.y, true)) {
24 trace("minx miny");
25 } else if (this.hitTest(ptMin.x, ptMax.y, true)) {
26 trace("minx maxy");
27 } else if (this.hitTest(ptMax.x, ptMin.y, true)) {
28 trace("maxx miny");
29 } else if (this.hitTest(ptMax.x, ptMax.y, true)) {
30 trace("maxx maxy");
31 }
32};
结束:你可以拖动ball,当碰到了矩形m的时候。会trace相应的信息。
目视远方,脚踏实地。我在醒着。
欢迎您光临醒着的flash技术博客。