http://www.cocoachina.com/bbs/read.PHP?tid=220250
管理提醒:本帖被 superdragon 执行加亮操作(2014-08-27)
chipmunk整理了一下,分享给大家吧。
演示地址: http://121.40.100.196/box/
刚开始研究,抛砖引玉
简要说明:
1、初始化物理环境,增加边界
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
initPhysics:
function
() {
var
space =
this
.space ;
staticBody = space.staticBody;
//开启物体形状测试
//this.initDebugMode();
// Gravity
space.gravity = cp.v(0,-980);
//重力
space.sleepTimeThreshold = 0.5;
//休眠临界时间
space.collisionSlop = 0.5;
//
// Walls--四个边界
walls = [
new
cp.SegmentShape( staticBody,cp.v(0,0-1),cp.v(winSize.width,0),0-1 ),
// bottom
// top
// left
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,0)
// right
];
for
(
i=0; i < walls.length; i++ ) {
shape = walls<i>;
shape.setElasticity(1);
//弹性
shape.setFriction(0);
//摩擦
//space.addStaticShape( shape );
space.addShape( shape );
if
(i >= 2){
shape.setCollisionType(3);
}
shape.setLayers(1);
}
},</i>
|
2、物体形状测试
initDebugMode:
._debugNode = cc.PhysicsDebugNode.create(
.space);
.addChild(
._debugNode);
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
|
3、物体定义
//物体的定义
mass = 1;
BoxWidth = 32;
body =
cp.Body(mass,cp.momentForBox(mass,BoxWidth,BoxWidth) );
body.setPos( cc.p(winSize.width/2,winSize.height/2) );
.space.addBody( body );
shape =
cp.BoxShape( body,BoxWidth);
shape.setElasticity( 0.5 );
shape.setFriction( 0.3 );
shape.setCollisionType(1);
shape.setLayers(3);
.space.addShape( shape );
//创建一个箱子
v_texture = cc.textureCache.addImage(res.Box_png);
.Box = cc.Physicssprite.create(v_texture,cc.rect(0,BoxWidth));
.Box.setBody(body);
.Box,1);
.Box.setTag(101);
//上下移动
moveto1 = cc.Moveto.create(0.5,winSize.width / 2,
.Box.y + 40);
moveto2 = cc.Moveto.create(0.5,monospace!important; font-size:1em!important; min-height:inherit!important; color:black!important; background:none!important">.Box.y - 40);
.downUpAction = cc.RepeatForever.create(cc.Sequence.create(moveto1,moveto2));
.Box.runAction(
.downUpAction);
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
4、增加点击事件、碰撞检测监听
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
._super();
cc.sys.dumpRoot();
cc.sys.garbageCollect();
//事件处理
(
'touches'
in
cc.sys.capabilities ){
cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ALL_AT_ONCE,
ontouchesEnded:
(touches,event){
event.getCurrentTarget().processEvent( touches[0] );
}
);
}
else
if
'mouse'
cc.sys.capabilities ){
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown:
(event){
event.getCurrentTarget().processEvent( event );
}
);
}
//重置数据
.resetDatas();
//
.scheduleUpdate();
//添加碰撞监听事件
// 1 & 2 检测Box和上下BLOCK碰撞
.space.addCollisionHandler( 1,2,
.collisionBegin.bind(
),
.collisionPre.bind(
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
.collisionPost.bind(
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
.collisionSeparate.bind(
)
);
// 1 & 3 检测Box和左右边界碰撞
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,3,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
)
);
// 1 & 4 检测Box和左右BLOCK碰撞
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,4,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
)
);
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
5、碰撞检测
( arbiter,space ) {
shapes = arbiter.getShapes();
shapeA = shapes[0];
shapeB = shapes[1];
collTypeA = shapeA.collision_type;
collTypeB = shapeB.collision_type;
(collTypeB == 3){
console.log(
'Collision Type A:'
+ collTypeA );
'end Collision Type B:'
+ collTypeB );
.BoxDirectionX = -
.BoxDirectionX;
.space.addPostStepCallback(
() {
.updateBoxAndBlocks();
}.bind(
));
}
(collTypeB == 2 || collTypeB == 4)
{
//碰到上下墙壁 或者 左右出来的BLOCKS 就Gameover
.gameOver();
}
return
true
;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
collisionPre :
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,space ) {
//console.log('collision pre');
;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
collisionPost :
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,space ) {
//console.log('collision post');
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
collisionSeparate :
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,space ) {
//console.log('collision separate');
}