Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by mkreitler

Hi all,

This is a great engine with a great community. I'm hoping someone here can help me with a newb problem.

I'm trying to let the user draw a rectangle by dragging the mouse, but my coordinates are off by the origin of my game window. I've tried adding ig.game.screen.x and ig.game.screen.y, but these are always 0.

Here's some relevant code:

update: function() {
        if (ig.input.pressed('mouseLeft')) {
            // set the starting point
            this.mouseStart.x = ig.input.mouse.x;
            this.mouseStart.y = ig.input.mouse.y;	
	}
    
	if (ig.input.state('mouseLeft')) {
            this.mouseNow.x = ig.input.mouse.x;
            this.mouseNow.y = ig.input.mouse.y;
	}
},

draw: function() {
        // Draw all entities and backgroundMaps
	this.parent();
		
        var dashGapArray = [8, 5];
        ig.system.context.strokeStyle = 'rgb(255,0,0)';
        ig.system.context.lineWidth = 1.5;
        ig.system.context.beginPath();
        
        var x0 = this.mouseStart.x + ig.game.screen.x;
        var y0 = this.mouseStart.y + ig.game.screen.y;
        var x1 = this.mouseNow.x + ig.game.screen.x;
        var y1 = this.mouseStart.y + ig.game.screen.y;
        var x2 = this.mouseNow.x + ig.game.screen.x;
        var y2 = this.mouseNow.y + ig.game.screen.y;
        var x3 = this.mouseStart.x + ig.game.screen.x;
        var y3 = this.mouseNow.y + ig.game.screen.y;
        
        ig.system.context.dashedLine(x0, y0, x1, y1, dashGapArray, 0);
        ig.system.context.dashedLine(x1, y1, x2, y2, dashGapArray, 0);
        ig.system.context.dashedLine(x2, y2, x3, y3, dashGapArray, 0);
        ig.system.context.dashedLine(x3, y3, x0, y0, dashGapArray, 0);
        
        ig.system.context.closePath();
        ig.system.context.stroke();
     
		// Add your own drawing code here
		var x = ig.system.width/2,
			y = ig.system.height/2;
		
		this.font.draw( 'Screen @(' + ig.game.screen.x + ', ' + ig.game.screen.y + ')', x, y, ig.Font.ALIGN.CENTER );
}

I'm sure it's a newb mistake. Any help is appreciated!

1 decade ago by mkreitler

Never mind, I figured it out.

ig.system.getDrawPos() converts game coordinates to canvas coordinates.

Handy!
Page 1 of 1
« first « previous next › last »