Gregory Posted December 10, 2014 Share Posted December 10, 2014 Hi, I would like to draw lines when the user is dragging his mouse. I have a working solution bellow but it is really bad, especially because of this line, it's adding a billion sprites to the game so it becomes super slow really quickly this.sprite = this.game.add.sprite(0, 0, this.bmd);Code: 'use strict'; function Play() { this.isDragging = false; this.lastPoint = null; } Play.prototype = { create: function() { // create a new bitmap data object this.bmd = this.game.add.bitmapData(this.game.width, this.game.height); // draw to the canvas context like normal this.bmd.ctx.strokeStyle = 'rgb( 77, 77, 77)'; this.bmd.ctx.lineWidth = 10; this.bmd.ctx.lineCap = 'round'; this.bmd.ctx.fillStyle = '#ff0000'; this.sprite = this.game.add.sprite(0, 0, this.bmd); }, update: function() { if(this.game.input.mousePointer.isUp) { this.isDragging = false; this.lastPoint = null; } if (this.game.input.mousePointer.isDown) { console.log('down'); this.isDragging = true; this.bmd.ctx.beginPath(); var newPoint = new Phaser.Point(this.game.input.x, this.game.input.y); if(this.lastPoint) { this.bmd.ctx.moveTo(this.lastPoint.x, this.lastPoint.y); this.bmd.ctx.lineTo(newPoint.x, newPoint.y); } this.lastPoint = newPoint; this.bmd.ctx.stroke(); // That's the line I need to change this.sprite = this.game.add.sprite(0, 0, this.bmd); } } }; module.exports = Play;Ideally i would like to redraw my sprite while the mouse is moving but I could find how to do so in the documentation.Does anyone has an idea? Thanks! Link to comment Share on other sites More sharing options...
XekeDeath Posted December 10, 2014 Share Posted December 10, 2014 Look at using a graphics object instead of a billion sprites... http://docs.phaser.io/Phaser.Graphics.html You can draw lines from point to point, so at regular intervals draw a new line from the last point to the cursor... Edit: Just actually read your code...Lemme think again... Still think you could be better off with a graphics object instead of the bitmap data/sprite combination... Link to comment Share on other sites More sharing options...
Gregory Posted December 10, 2014 Author Share Posted December 10, 2014 Look at using a graphics object instead of a billion sprites... Just tried it and it works, however how can I set the lineCap to round? Here is how it looks: http://d.pr/i/1ksng/3R4nWLD1 Link to comment Share on other sites More sharing options...
laurie Posted December 10, 2014 Share Posted December 10, 2014 I'm not sure what advantages using Graphics may have over a BitmapData backed Sprite (I haven't looked much at the Graphics type) but if you find you need to go back to your original approach to get the effect you're after, I can offer a couple of pointers: The line you marked as needing to change should simply go away; you already have a Sprite that's using the BitmapData object to render, so you don't need to create additional ones. Instead, you need to mark the BitmapData instance as dirty so it knows to re-render itself: this.bmd.dirty = true. If you run into performance issues as a result of marking the bmd dirty on every update, you might also consider checking that the pointer has actually moved away from lastPoint before drawing and dirty-marking. Gregory 1 Link to comment Share on other sites More sharing options...
Gregory Posted December 10, 2014 Author Share Posted December 10, 2014 this.bmd.dirty = true; Awesome! This is exactly what I was looking for.Thank you Link to comment Share on other sites More sharing options...
Recommended Posts