Koschi Posted March 19, 2014 Share Posted March 19, 2014 Hi there, i am new to using the Phaser Engine and have some problems with it. First the code: <!doctype html><html> <head> <meta charset="UTF-8" /> <title>hello phaser!</title> <script src="phaser.js"></script> </head> <body> <script type="text/javascript"> var game = new Phaser.Game(1200, 750, Phaser.CANVAS, '', { preload: preload, create: create, update: update, render: render }); var leftEmitter;var rightEmitter; var speed = 1; function preload() { game.load.image('ball1', 'assets/sprites/red_ball.png');game.load.image('ball2', 'assets/sprites/blue_ball.png');game.load.image('carrier', '_CMDB/_assets/0000001.png'); } function create() { ship = game.add.sprite(game.world.centerX, game.world.centerY+200, 'carrier');ship.body.allowRotation = true;ship.body.immovable = true; ship.body.setPolygon( 447, -119, 447, -99, 366, -99, 446, -96, 447, -77, 385, -75, 398, -72, 398, -53, 378, -51, 365, -3, 342, 0, 282, -53, 244, -59, 206, -38, 0, -90, 0, -121, 213, -176, 248, -155, 280, -163, 339, -217, 361, -217, 378, -166, 395, -165, 399, -146, 388, -144, 445, -141, 446, -122, 366, -119 );ship.body.translate(0, 216);ship.anchor.setTo(0.5, 0.5); leftEmitter = game.add.emitter(500, game.world.centerY - 200);leftEmitter.bounce.setTo(0.5, 0.5);//leftEmitter.setXSpeed(400, 500);//leftEmitter.setYSpeed(-150, -50);leftEmitter.makeParticles('ball1', 0, 200, 1, true); rightEmitter = game.add.emitter(game.world.width - 500, game.world.centerY - 200);rightEmitter.bounce.setTo(0.5, 0.5);//rightEmitter.setXSpeed(-500, -400);//rightEmitter.setYSpeed(-50, 150);rightEmitter.makeParticles('ball2', 0, 200, 1, true); // explode, lifespan, frequency, quantityleftEmitter.start(false, 4000, 100);rightEmitter.start(false, 4000, 100); } function update() {game.physics.collide(leftEmitter, ship);game.physics.collide(rightEmitter, ship); ship.body.velocity.x = 0;ship.body.velocity.y = 0;ship.body.angularVelocity = 0; if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT)){ ship.body.angularVelocity = -100;ship.body.polygon.rotate(-2*Math.PI/180);}else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){ ship.body.angularVelocity = 100;ship.body.polygon.rotate(2*Math.PI/180);} if (game.input.keyboard.isDown(Phaser.Keyboard.UP)){ ship.body.velocity.copyFrom(game.physics.velocityFromAngle(ship.angle, -300));}else if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN)){ ship.body.velocity.copyFrom(game.physics.velocityFromAngle(ship.angle, 300));}} function render() {game.debug.renderPhysicsBody(ship.body);} </script> </body></html> If you run this code with the attached file you will "see" an spaceship wich can be controlled with the arrow keys. But if you rotate it the Polygon will not rotate proper with it. This is the First problem. Also the bouncing from that Polygon seems wrong, it you reload the page without moving the ship you can see that the blue and red balls doent really bounce from the hull. At some points they stop some centimeters before the hull beginns. Also when rotating the ships, some angles seem to have no bounce at all, the balls fly through the first line and bounce inside the ship from the second line on the other side of the hull. Can somebody give me an hint on this problems? greetings Link to comment Share on other sites More sharing options...
Recommended Posts