最近在做个flash的打砖块的游戏。
游戏本身还是比较简单的。
但是在检测碰撞时
涉及到了 不规则形状的碰撞
===============
知识点如下:
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:代码开始在主时间轴
   
游戏本身还是比较简单的。
但是在检测碰撞时
涉及到了 不规则形状的碰撞
===============
知识点如下:
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:代码开始在主时间轴
 1 stop();
stop();
2 ball.onPress = function() {
ball.onPress = function() {
3 this.startDrag();//开始拖拽
    this.startDrag();//开始拖拽
4 };
};
5 ball.onRelease = function() {
ball.onRelease = function() {
6 this.stopDrag();//结束拖拽
    this.stopDrag();//结束拖拽
7 };
};
8 //声明2个点对象,因为每个点对象接受x,y,但极值有四个。所以创建2个点对象。
//声明2个点对象,因为每个点对象接受x,y,但极值有四个。所以创建2个点对象。
9 var ptMin:Object = new Object();
var ptMin:Object = new Object();
10 var ptMax:Object = new Object();
var ptMax:Object = new Object();
11 var thisBounds:Object = new Object();
var thisBounds:Object = new Object();
12 //这个对象里获取了ball元件的极值
//这个对象里获取了ball元件的极值
13 thisBounds = ball.getBounds();
thisBounds = ball.getBounds();
14 m.onEnterFrame = function() {
m.onEnterFrame = function() {
15 //为点对象添加x y 属性一定要在onEnterFrame事件中
    //为点对象添加x y 属性一定要在onEnterFrame事件中
16 ptMin.x = thisBounds.xMin;
    ptMin.x = thisBounds.xMin;
17 ptMin.y = thisBounds.yMin;
    ptMin.y = thisBounds.yMin;
18 ptMax.x = thisBounds.xMax;
    ptMax.x = thisBounds.xMax;
19 ptMax.y = thisBounds.yMax;
    ptMax.y = thisBounds.yMax;
20 //将相对于ball内的左上角的值,转变为相对于舞台的左上角的值
    //将相对于ball内的左上角的值,转变为相对于舞台的左上角的值
21 ball.localToGlobal(ptMin);
    ball.localToGlobal(ptMin);
22 ball.localToGlobal(ptMax);
    ball.localToGlobal(ptMax);
23 if (this.hitTest(ptMin.x, ptMin.y, true)) {
    if (this.hitTest(ptMin.x, ptMin.y, true)) {
24 trace("minx miny");
        trace("minx miny");
25 } else if (this.hitTest(ptMin.x, ptMax.y, true)) {
    } else if (this.hitTest(ptMin.x, ptMax.y, true)) {
26 trace("minx  maxy");
        trace("minx  maxy");
27 } else if (this.hitTest(ptMax.x, ptMin.y, true)) {
    } else if (this.hitTest(ptMax.x, ptMin.y, true)) {
28 trace("maxx  miny");
        trace("maxx  miny");
29 } else if (this.hitTest(ptMax.x, ptMax.y, true)) {
    } else if (this.hitTest(ptMax.x, ptMax.y, true)) {
30 trace("maxx  maxy");
        trace("maxx  maxy");
31 }
    }
32 };
};
 stop();
stop();2
 ball.onPress = function() {
ball.onPress = function() {3
 this.startDrag();//开始拖拽
    this.startDrag();//开始拖拽4
 };
};5
 ball.onRelease = function() {
ball.onRelease = function() {6
 this.stopDrag();//结束拖拽
    this.stopDrag();//结束拖拽7
 };
};8
 //声明2个点对象,因为每个点对象接受x,y,但极值有四个。所以创建2个点对象。
//声明2个点对象,因为每个点对象接受x,y,但极值有四个。所以创建2个点对象。9
 var ptMin:Object = new Object();
var ptMin:Object = new Object();10
 var ptMax:Object = new Object();
var ptMax:Object = new Object();11
 var thisBounds:Object = new Object();
var thisBounds:Object = new Object();12
 //这个对象里获取了ball元件的极值
//这个对象里获取了ball元件的极值13
 thisBounds = ball.getBounds();
thisBounds = ball.getBounds();14
 m.onEnterFrame = function() {
m.onEnterFrame = function() {15
 //为点对象添加x y 属性一定要在onEnterFrame事件中
    //为点对象添加x y 属性一定要在onEnterFrame事件中16
 ptMin.x = thisBounds.xMin;
    ptMin.x = thisBounds.xMin;17
 ptMin.y = thisBounds.yMin;
    ptMin.y = thisBounds.yMin;18
 ptMax.x = thisBounds.xMax;
    ptMax.x = thisBounds.xMax;19
 ptMax.y = thisBounds.yMax;
    ptMax.y = thisBounds.yMax;20
 //将相对于ball内的左上角的值,转变为相对于舞台的左上角的值
    //将相对于ball内的左上角的值,转变为相对于舞台的左上角的值21
 ball.localToGlobal(ptMin);
    ball.localToGlobal(ptMin);22
 ball.localToGlobal(ptMax);
    ball.localToGlobal(ptMax);23
 if (this.hitTest(ptMin.x, ptMin.y, true)) {
    if (this.hitTest(ptMin.x, ptMin.y, true)) {24
 trace("minx miny");
        trace("minx miny");25
 } else if (this.hitTest(ptMin.x, ptMax.y, true)) {
    } else if (this.hitTest(ptMin.x, ptMax.y, true)) {26
 trace("minx  maxy");
        trace("minx  maxy");27
 } else if (this.hitTest(ptMax.x, ptMin.y, true)) {
    } else if (this.hitTest(ptMax.x, ptMin.y, true)) {28
 trace("maxx  miny");
        trace("maxx  miny");29
 } else if (this.hitTest(ptMax.x, ptMax.y, true)) {
    } else if (this.hitTest(ptMax.x, ptMax.y, true)) {30
 trace("maxx  maxy");
        trace("maxx  maxy");31
 }
    }32
 };
};结束:你可以拖动ball,当碰到了矩形m的时候。会trace相应的信息。
目视远方,脚踏实地。我在醒着。
欢迎您光临醒着的flash技术博客。
 
                    
                     
                    
                 
                    
                
 
         ball.onPress
ball.onPress 
 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号