diff --git a/examples/wip/springs 1.js b/examples/wip/springs 1.js new file mode 100644 index 00000000..a2c20224 --- /dev/null +++ b/examples/wip/springs 1.js @@ -0,0 +1,92 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.image('backdrop', 'assets/pics/remember-me.jpg'); + game.load.image('box', 'assets/sprites/block.png'); + +} + +var box1; +var box2; +var s; + +var cursors; + +function create() { + + game.world.setBounds(0, 0, 1920, 1200); + game.add.sprite(0, 0, 'backdrop'); + +// game.physics.gravity.y = -9.78; + game.physics.gravity[1] = -9.78; + + // game.physics.onBodyAdded.add(addedToWorld, this); + + box1 = game.add.sprite(200, 200, 'box'); + box1.physicsEnabled = true; + // box1.body.fixedRotation = true; + // box1.body.mass = 3; + + box2 = game.add.sprite(400, 200, 'box'); + box2.physicsEnabled = true; + // box2.body.fixedRotation = true; + + s = new Phaser.Physics.Spring(game, box1.body.data, box2.body.data, 2, 10, 1, null, null, [ 0, 70 ], [ 0, 70 ]); + + game.physics.addSpring(s); + + // var s = new p2.Spring(box1, box2, { + // restLength : 1, + // stiffness : 10, + // localAnchorA : [0,0.5], + // localAnchorB : [0,0.5], + // }); + // world.addSpring(s); + + game.camera.follow(box2); + + cursors = game.input.keyboard.createCursorKeys(); + + // game.physics.defaultRestitution = 0.8; + +} + +function addedToWorld(body, world) { + + console.log('Body added for', body); + +} + +function update() { + + box2.body.setZeroVelocity(); + + if (cursors.left.isDown) + { + box2.body.moveLeft(400); + } + else if (cursors.right.isDown) + { + box2.body.moveRight(400); + } + + if (cursors.up.isDown) + { + box2.body.moveUp(400); + } + else if (cursors.down.isDown) + { + box2.body.moveDown(400); + } + +} + +function render() { + + // game.debug.renderText('x: ' + box2.body.velocity.x, 32, 32); + // game.debug.renderText('y: ' + box2.body.velocity.y, 32, 64); + +} + diff --git a/src/physics/Spring.js b/src/physics/Spring.js index 5d8d8b84..417f6923 100644 --- a/src/physics/Spring.js +++ b/src/physics/Spring.js @@ -38,22 +38,22 @@ Phaser.Physics.Spring = function (game, bodyA, bodyB, restLength, stiffness, dam damping: damping }; - if (typeof worldA !== 'undefined') + if (typeof worldA !== 'undefined' && worldA !== null) { options.worldAnchorA = [ game.math.px2p(worldA[0]), game.math.px2p(worldA[1]) ]; } - if (typeof worldB !== 'undefined') + if (typeof worldB !== 'undefined' && worldB !== null) { options.worldAnchorB = [ game.math.px2p(worldB[0]), game.math.px2p(worldB[1]) ]; } - if (typeof localA !== 'undefined') + if (typeof localA !== 'undefined' && localA !== null) { options.localAnchorA = [ game.math.px2p(localA[0]), game.math.px2p(localA[1]) ]; } - if (typeof localB !== 'undefined') + if (typeof localB !== 'undefined' && localB !== null) { options.localAnchorB = [ game.math.px2p(localB[0]), game.math.px2p(localB[1]) ]; }