[SOLVED] A dragging glitch - code included

howdy folks,

so im just working away on OPENFL to recreate a iOS game i made in, urrrggghhh, UNITY :wink:
but have ran into this strange thing when making a Player.hx class and using the Main.hx to call it in.
it works, to some degree, using the MouseHandler demo in OPENFL. thank you.
but it then just start spazzing out, a lot. when dragging each time, it gets worse and worse

ive also decided to just use generated shapes rather than images just now, just for ease really. i may go to images later on, but to get it down first is key :wink:

Main.hx

package;
/*
*@author lewis lepton 2015
*/
import openfl.display.Sprite;
import openfl.Lib;
import openfl.events.Event;

class Main extends Sprite {
	private var player:Player;
	var inited:Bool;

	function resize(e) {
		if (!inited) init();
	}

	public function init () {
		if (inited) return;
		inited = true;
		player = new Player();
		player.x = 300;
		player.y = 300;

		addChild(player);
	}

	public function new() {
		super();
		addEventListener(Event.ADDED_TO_STAGE, added);
	}
	
	function added(e){
		removeEventListener(Event.ADDED_TO_STAGE, added);
		stage.addEventListener(Event.RESIZE, resize);
	}
}

Player.hx

package;
/*
*@author lewis lepton 2015
*/
// import motion.Actuate;
import openfl.display.Sprite;
import openfl.events.MouseEvent;

class Player extends Sprite {
	public var playerDragOffsetX:Float;
	public var playerDragOffsetY:Float;

	public function new() {
		super();
		this.graphics.beginFill(0xE85151);
		this.graphics.drawCircle(0,0,30);
		this.graphics.endFill();
		
		this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
	}

	public function onMouseDown(event:MouseEvent):Void {
		playerDragOffsetX = this.x - event.localX;
		playerDragOffsetY = this.y - event.localY;
		stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}

	public function onMouseMove(event:MouseEvent):Void {

		this.x = event.localX + playerDragOffsetX;
		this.y = event.localY + playerDragOffsetY;
	}

	public function onMouseUp(event:MouseEvent):Void {
		// Actuate.tween(this, 1, {x:this.x + 5, y:this.y + 5});
		stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
		stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
	}
}

try event.stageX and event.stageY instead of event.localX and event.localY, that will give you absolute coordinates instead of being relative to the hit target :slight_smile:

thanks for getting back. :wink:
but, its actually the same as my one before. its still glitching it away from the cursor

cheers

EDIT
AH WAIT. my mistake i didnt see another localX & localY i needed to change. ALL IS GOOD YA :wink:

1 Like