1 decade ago by pokypine
Does Impact support dragging of entities with mouse/touch? For instance a simple sliding tile puzzle type game?
code snippet?
code snippet?
This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact
ig.module( 'game.main' ) .requires( 'impact.game', 'impact.font' ) .defines(function(){ MyGame = ig.Game.extend({ pos: {x:0, y:0}, m_pos:{x:0, y:0}, font: new ig.Font( 'media/fonts/tungsten-18.png' ), backdrop : new ig.Image('media/background/bg.png'), init: function() { // Initialize your game here; bind keys etc. ig.input.initMouse(); ig.input.bind( ig.KEY.MOUSE1, 'lbtn' ); }, update: function() { // Update all entities and backgroundMaps this.parent(); if (ig.input.pressed('lbtn')) { this.m_pos.x = ig.input.mouse.x; this.m_pos.y = ig.input.mouse.y; this.pos.old_y = this.pos.y; this.pos.old_x = this.pos.x; } if (ig.input.state('lbtn')) { this.pos.y = this.pos.old_y - -(ig.input.mouse.y - this.m_pos.y); this.pos.x = this.pos.old_x - -(ig.input.mouse.x - this.m_pos.x); } // Add your own, additional update code here }, draw: function() { // Draw all entities and backgroundMaps this.parent(); this.backdrop.draw(this.pos.x,this.pos.y); // Add your own drawing code here var x = ig.system.width/2, y = ig.system.height/2; this.font.draw( 'It Works!', this.pos.x + x, this.pos.y + y, ig.Font.ALIGN.CENTER ); } }); // Start the Game with 60fps, a resolution of 720x520, scaled // up by a factor of 2 ig.main( '#canvas', MyGame, 60, 740, 540, 1 ); });
init: function() { ... ig.input.bind(ig.KEY.MOUSE1, 'click'); ... }
inFocus: function() { return ( (this.pos.x <= ig.input.mouse.x <= this.pos.x + this.size.x) && (this.pos.y <= ig.input.mouse.y <= this.pos.y + this.size.y) ); } update: function() { ... if (ig.input.pressed('click') && this.inFocus()) { // do whatever you want when this entity is clicked } }