4
initDebugMode:
._debugNode = cc.PhysicsDebugNode.create(
.space);
.addChild(
._debugNode);
|
initBoxWithBody:
//物体的定义
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);
4、增加点击事件、碰撞检测监听
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
onEnter:
._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(
.collisionPost.bind(
.collisionSeparate.bind(
)
);
// 1 & 3 检测box和左右边界碰撞
)
);
// 1 & 4 检测box和左右BLOCK碰撞
)
);
|
collisionBegin :
( 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)
{
.gameOver();
}
return
true
;
collisionPre :
//console.log('collision pre');
;
collisionPost :
//console.log('collision post');
collisionSeparate :
//console.log('collision separate');
}
|