Skip to content

Instantly share code, notes, and snippets.

@theRemix
Last active June 3, 2020 18:32
Show Gist options
  • Save theRemix/4d440c35ae486cf24933ad2f4bd0fd4e to your computer and use it in GitHub Desktop.
Save theRemix/4d440c35ae486cf24933ad2f4bd0fd4e to your computer and use it in GitHub Desktop.
Heaps.io Interactive onDrag
package entities;
import h2d.Scene;
import h2d.Graphics;
import h2d.Interactive;
import hxd.Event;
class Box extends Graphics{
var int:Interactive;
var last:{x:Float, y:Float} = { x:0, y:0 };
var offset:{x:Float,y:Float};
public function new(scene:Scene, name:String, color:Int, x:Int, y:Int, w:Int, h:Int) {
super(scene);
this.x = x;
this.y = y;
beginFill(color);
drawRect(0, 0, w, h);
endFill();
int = new Interactive(w, h, this);
int.onPush = click;
int.onRelease = release;
}
function click(e:Event) {
bring_to_front();
int.startDrag(drag);
offset = {x:e.relX, y:e.relY};
last.x = x;
last.y = y;
}
function release(e:Event) {
int.stopDrag();
}
function drag(e:Event)
{
x += e.relX - offset.x;
y += e.relY - offset.y;
}
function bring_to_front()
{
parent.children.push(parent.children.splice(parent.children.indexOf(this), 1)[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment