From 8859758c145ec9635782b270b4092b4d865e7f12 Mon Sep 17 00:00:00 2001 From: Juraj Michalek Date: Sat, 26 Dec 2015 15:35:31 +0100 Subject: [PATCH] drag and drop snapping --- index.html | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index f049eca..e537ddc 100644 --- a/index.html +++ b/index.html @@ -30,14 +30,16 @@ state.create = function() { this.character.animation.add('walking', [ 0, 1 ], 0.2, true); this.character.animation.add('idle', [ 2, 3, 4, 5, 4, 3 ], 0.2, true); - this.redirector = new Kiwi.GameObjects.Sprite(this, this.textures.oneWay, 2*64, 3*64); + this.redirector = new Kiwi.GameObjects.Sprite(this, this.textures.oneWay, 9*64, 2*64); this.redirector.box.hitbox = new Kiwi.Geom.Rectangle( 20, 20, 50, 50 ); - this.redirector.input.enableDrag(); + this.redirector.input.enableDrag(true); + this.redirector.input.onDragStopped.add( this.stoppedDrag, this ); - this.redirector2 = new Kiwi.GameObjects.Sprite(this, this.textures.oneWay, 6*64, 3*64); + this.redirector2 = new Kiwi.GameObjects.Sprite(this, this.textures.oneWay, 9*64, 3*64); this.redirector2.cellIndex = 6; this.redirector2.box.hitbox = new Kiwi.Geom.Rectangle( 20, 20, 50, 50 ); - this.redirector2.input.enableDrag(); + this.redirector2.input.enableDrag(true); + this.redirector2.input.onDragStopped.add( this.stoppedDrag2, this ); this.finishMarker = new Kiwi.GameObjects.Sprite(this, this.textures.finishMarker, 6*64, 4*64); this.finishMarker.box.hitbox = new Kiwi.Geom.Rectangle( 20, 20, 50, 50 ); @@ -114,6 +116,28 @@ state.resetCharacter = function () { this.character.y = 64 - 8; } +state.stoppedDrag = function() { + if (this.redirector.x % 64 > 32) { + this.redirector.x += 64; + } + if (this.redirector.y % 64 > 32) { + this.redirector.y += 64; + } + this.redirector.x = this.redirector.x - this.redirector.x % 64; + this.redirector.y = this.redirector.y - this.redirector.y % 64; +} + +state.stoppedDrag2 = function() { + if (this.redirector2.x % 64 > 32) { + this.redirector2.x += 64; + } + if (this.redirector2.y % 64 > 32) { + this.redirector2.y += 64; + } + this.redirector2.x = this.redirector2.x - this.redirector2.x % 64; + this.redirector2.y = this.redirector2.y - this.redirector2.y % 64; +} + state.activateScene = function () { this.character.physics.velocity.y = this.velocityY; }