How to double click? [SOLVED]


(Darrin) #1

Any suggestions on how to do a double click? Just set a timer between clicks? How many milliseconds is the double click on windows?

One other question, is double tapping a phone the same length of time?

Thanks.

Darrin


(MartĂ­ Angelats i Ribera) #2

I think you’ll have to use an event listener.It will not be syncronized with the Engine so be a bit carefull about it.

FP.stage.mouseChildren = false;
FP.stage.doubleClickEnabled = true;
FP.stage.addEventListener(MouseEvent.DOUBLE_CLICK, doubleClick);

It will uaw the OS timing for double click, but i don’t know how it works with the tactil screens.

Sure you can use the “isClicked” to try to do something similar, but if you are fast enough you’ll see you han make the game think is a simple click. And there’s also the issue with the timming. With this you don’t have to worry about the time you have to skip, you’ll use the OS one.

PD: As I said above, this will not be called in the update, so i recomend this:

public var doubleClicked:uint = 0;

override public function added():void
{
	FP.stage.mouseChildren = false;
	FP.stage.doubleClickEnabled = true;
	FP.stage.addEventListener(MouseEvent.DOUBLE_CLICK, dbClick);
}
override public function removed():void
{
	FP.stage.removeEventListener(MouseEvent.DOUBLE_CLICK, dbClick);
}
override public function update():void
{
	while (doubleClicked > 0)
	{
		doubleClick();
		doubleClicked--;
	}
}
protected function dbClick(e:Event):void
{
	doubleClicked++;
}


public function doubleClick():void
{
	//handle here
}

(Darrin) #3

Thanks Marti, I will give it a try later today.


(Darrin) #4

Marty

Thanks. So it works fine. The problem is my code. :stuck_out_tongue: I have a problem with changing states of the object with a single click before the second click is detected. Currently it works intermittently even though I catch the doubleclick every time.

So I’ve been reading about the cost events. Apparently they are heavy usage. So I have 64 tiles and I don’t think it is best practice to put 64 double event listeners on them. Or is it okay? I couldn’t find any tests.

I then read about something called a callback which could speed things up. Have you used these?

Or I can go to a better single click system, checking state. Still trying to decide. Anyway your code works well. Thank you for that. This seems to be a bigger question about double clicking in games than I was original aware of.

Darrin


(MartĂ­ Angelats i Ribera) #5

Here there are a fews things:

  1. This code is nice for one or a few Entities. For more, that code can end up using way too much resources.
  2. You are handelling a MouseEvent. Callbacks are for “custom” events. They are basically a function that you pass as parameter that you call when something happens.
  3. You can make a single clicking without too much worries. You don’t even have to use the MouseEvents. The problem is the double click becouse usually is faster than a few frames and can be undetected. Also the time between click may be different (with MouseEvent you get the OS one)

Here i let you a code that i think it may help you

package  
{
	import flash.events.MouseEvent;
	import net.flashpunk.FP;
	/**
	 * ...
	 * @author Copying
	 */
	public class DoubleClick 
	{
		public static var clicked:Boolean = false;
		public static var x:Number = 0;
		public static var y:Number = 0;
		
		public static function event(e:MouseEvent = null)
		{
			x = FP.camera.x + e.stageX;
			y = FP.camera.y + e.stageY;
			clicked = true;
		}
	}

}

Some functions in your world class

override public function begin():void
{
	FP.stage.addEventListener(MouseEvent.DOUBLE_CLICK, DoubleClick.event);
}

override public function end():void
{
	FP.stage.removeEventListener(MouseEvent.DOUBLE_CLICK, DoubleClick.event);
}

override public function update()
{
	super.update();
	
	//this HAVE to go after super.update() (after the entities check at that frame)
	DoubleClick.clicked = false;
}

And in your entities update()

override public function update():void
{
	if (DoubleClick.clicked && collidePoint(x, y, DoubleClick.x, DoubleClick.y))
	{
		//handle here
	}
}

basically here you handle it once and then the entities can check if they are double clicked.


(Darrin) #6

So I’m marking this solved even though I decided to go with the single click resolution. I did use your code but thought the other was a bit more elegant for multiple platforms. Thanks again. I will be using this on the next game though. Great stuff. Thanks again.