tidelake Posted April 9, 2016 Share Posted April 9, 2016 The following code produces a flipper and four balls that seem to interact as they should. They all collide with each other and the game world bounds. The problem is that the motor isn't working, nor can I figure out how to limit the rotation of the flipper. The flipper goes to the location of the mouseDown event. It is not fixed to a place on the board. This is my intention. I want to use a flipper wherever the player taps and holds. Thank you. window.onload = function() { var game = new Phaser.Game(800, 300, 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, flipper_joint, flipper_constraint; var cursors, input_body; var constraint, input_constraint; var constraint_count = 0; var ball1, ball2; function create() { game.stage.backgroundColor = "#f2f2f2"; game.world.setBounds(0, 0, 800, 300); game.physics.startSystem(Phaser.Physics.P2JS); // Turn on impact events for the world, // without this we get no collision callbacks game.physics.p2.setImpactEvents(true); game.physics.p2.updateBoundsCollisionGroup(); game.physics.p2.gravity.y = 10; cursors = game.input.keyboard.createCursorKeys(); // Create our collision groups. One for the flipper, one for the balls var flipper_collision_group = game.physics.p2.createCollisionGroup(); var ball_collision_group = game.physics.p2.createCollisionGroup(); // create joint for flipper flipper_joint = game.add.sprite(200, 150, ''); game.physics.p2.enable(flipper_joint, true); flipper_joint.body.setCircle(25); flipper_joint.body.data.gravityScale = 0; flipper_joint.body.clearCollision(true, true); flipper = game.add.sprite(200, 150, 'flipper'); game.physics.p2.enable(flipper, true); flipper.body.clearShapes(); flipper.body.loadPolygon('physics_data', 'flipper'); flipper.body.data.gravityScale = 0; flipper.body.setCollisionGroup(flipper_collision_group); flipper.body.angle = -35; flipper_constraint = game.physics.p2.createRevoluteConstraint( flipper_joint, [0,0], flipper, [-60,-30] ); input_body = game.add.sprite(0, 0, '', 0); game.physics.p2.enable(input_body, true); input_body.body.setCircle(20); input_body.body.setCollisionGroup(flipper_collision_group); var balls = game.add.group(); balls.enableBody = true; balls.physicsBodyType = Phaser.Physics.P2JS; for (var i = 0; i < 4; i++) { var ball = balls.create(game.world.randomX, game.world.randomY, 'ball'); ball.body.setCircle(25); ball.body.setCollisionGroup(ball_collision_group); ball.body.collides([ball_collision_group, flipper_collision_group]); } // set upper and lower limits for angle in the constraint // NOTE not working flipper_constraint.upperLimitEnabled = true; flipper_constraint.lowerLimitEnabled = true; flipper_constraint.lowerLimit = 0; flipper_constraint.upperLimit = -35; // Not working flipper_constraint.setMotorSpeed(100); } function update() { input_body.body.x = game.input.x; input_body.body.y = game.input.y; if (game.input.activePointer.isDown) { if (constraint_count == 0) { input_constraint = game.physics.p2.createRevoluteConstraint( flipper.body, [0,0], input_body, [0,0], 1000 ); flipper_constraint.enableMotor(); constraint_count = 1; } else if (constraint_count == 1) { flipper_constraint.disableMotor(); game.physics.p2.removeConstraint(input_constraint); constraint_count = 0; } } else { if (constraint_count == 1) { game.physics.p2.removeConstraint(input_constraint); } constraint_count = 0; } } function render() { } }; // end window.onload Link to comment Share on other sites More sharing options...
Recommended Posts