-
-
Save tks2shimizu/51df52a9fb9681884174461b13d3ac8e to your computer and use it in GitHub Desktop.
Cocos2d-x(JS) Chipmunk #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var world; | |
var shapeArray = []; | |
var gameLayer; | |
var HelloWorldLayer = cc.Layer.extend({ | |
ctor:function () { | |
this._super(); | |
//重力の設定 | |
world = new cp.Space(); | |
world.gravity = cp.v(0, -100); | |
//デバッグ情報 | |
var debugDraw = cc.PhysicsDebugNode.create(world); | |
debugDraw.setVisible(true); | |
this.addChild(debugDraw); | |
//毎フレーム更新処理 | |
this.scheduleUpdate(); | |
//物体の追加 | |
this.addBody(240, 10, 480, 20, false, res.ground_png, "ground"); | |
this.addBody(204, 32, 24, 24, true, res.brick1x1_png, "destroyable"); | |
this.addBody(276, 32, 24, 24, true, res.brick1x1_png, "destroyable"); | |
this.addBody(240, 56, 96, 24, true, res.brick4x1_png, "destroyable"); | |
this.addBody(240, 80, 48, 24, true, res.brick2x1_png, "solid"); | |
this.addBody(228, 104, 72, 24, true, res.brick3x1_png, "destroyable"); | |
this.addBody(240, 140, 96, 48, true, res.brick4x2_png, "solid"); | |
this.addBody(240, 188, 24, 48, true, res.totem_png, "totem"); | |
//タッチイベント | |
cc.eventManager.addListener(touchListener, this); | |
return true; | |
}, | |
update:function (dt) { | |
//物理シミュレーション | |
world.step(dt); | |
}, | |
addBody:function (posX, posY, width, height, isDynamic, spriteImage, type) { | |
//物体の種類設定 | |
var body; | |
if (isDynamic) { | |
body = new cp.Body(1, cp.momentForBox(1, width, height)); | |
} else{ | |
body = new cp.Body(Infinity, Infinity); | |
} | |
//物体の位置 | |
body.setPos(cp.v(posX, posY)); | |
//物理空間への配置 | |
if(isDynamic){ | |
world.addBody(body); | |
} | |
//物理特性 | |
var shape = new cp.BoxShape(body, width, height); | |
shape.setFriction(1); | |
shape.setElasticity(0); | |
shape.name = type; | |
world.addShape(shape); | |
shapeArray.push(shape); | |
} | |
}); | |
var HelloWorldScene = cc.Scene.extend({ | |
onEnter:function () { | |
this._super(); | |
gameLayer = new HelloWorldLayer(); | |
this.addChild(gameLayer); | |
} | |
}); | |
var touchListener = cc.EventListener.create({ | |
event: cc.EventListener.TOUCH_ONE_BY_ONE, | |
swallowTouches: true, | |
onTouchBegan: function (touch, event) { | |
//全ての物体に対してループする | |
for (var i = shapeArray.length-1; i >= 0; i--) { | |
if (shapeArray[i].pointQuery(cp.v(touch.getLocation().x, touch.getLocation().y)) != undefined) { | |
if (shapeArray[i].name == "destroyable") { | |
//タップされた時は、物体を削除する | |
world.removeBody(shapeArray[i].getBody()); | |
world.removeShape(shapeArray[i]); | |
shapeArray.splice(i, 1); | |
} | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment