Search the Community
Showing results for tags 'resoluteconstraint'.
-
I'm working on a modified type of pinball, where the flipper appears where the player taps and holds, and flips when the tap is released. I'm not very far along, but I have gotten the flipper to appear where the screen is tapped. However, I'm quite puzzled by how to proceed with revoluteConstraint, getting the flipper to hinge at a certain point. Again, I need to tap the screen to get a flipper, hold the tap and flip the flipper on release. <!doctype html> <html> <head> <meta charset="UTF-8" /> <title>Flipt</title> <script src="assets/phaser.min.js"></script> </head> <body> <script type="text/javascript"> window.onload = function() { var game = new Phaser.Game(800, 600, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render }); function preload() { game.load.image('flipper', 'assets/sprites/flipper.png'); game.load.image('ball', 'assets/sprites/ball-transparent.png'); // Load our physics data exported from PhysicsEditor game.load.physics('physics_data', 'assets/physics/sprites.json'); } var flipper; var cursors; var constraint; function create() { game.stage.backgroundColor = "#f2f2f2"; game.physics.startSystem(Phaser.Physics.P2JS); game.physics.p2.setImpactEvents(true); var flipperCollisionGroup = game.physics.p2.createCollisionGroup(); var ballCollisionGroup = game.physics.p2.createCollisionGroup(); flipper = game.add.sprite(300, 400, 'flipper'); game.physics.p2.enable(flipper, true); flipper.body.clearShapes(); flipper.body.loadPolygon('physics_data', 'flipper'); flipper.body.kinematic = true; // Modify a few body properties flipper.body.setZeroDamping(); //flipper.body.fixedRotation = true; ground = new p2.Body(); game.physics.p2.world.addBody(ground); pivotCenter = [0, 0]; offsets = [0, 0]; constraint = game.physics.p2.createRevoluteConstraint(flipper, pivotCenter, ground, offsets); constraint.upperLimit = Phaser.Math.degToRad(0); constraint.lowerLimit = Phaser.Math.degToRad(35); constraint.setMotorSpeed(12); constraint.enableMotor(); game.input.onDown.add(toggleFlipper, this); cursors = game.input.keyboard.createCursorKeys(); } function toggleFlipper(pointer) { if (flipper.alive) { flipper.kill(); } flipper.reset(pointer.x, pointer.y); } function update() { flipper.body.setZeroVelocity(); if (cursors.left.isDown) { flipper.body.rotateLeft(50); } else if (cursors.right.isDown) { flipper.body.rotateRight(50); } } function render() { } }; // end window.onload </script> </body> </html> sprites.json