I want to start an action right after the mouse is released.
First I tried this:
if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
// start action
}
But then it also start when the mouse is pressed.
After that I tried this:
if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT && !this.app.mouse.isPressed(pc.MOUSEBUTTON_LEFT)) {
// start action
}
But then it never start.
Somebody an idea?
Hi @Albertos ,
You can use the MOUSEUP event and subscribe to that to get what you want, put this in your initialize method not in the update:
this.app.mouse.on(pc.EVENT_MOUSEUP, function(event){
if (event.button === pc.MOUSEBUTTON_LEFT) {
// left click button was released
}
}, this);
That is difficult since I want to use it in the update because of other scripting requirements.
I think the wasPressed method doesn’t work as you would expect.
If I was 28 years old, It means I’m not 28 years old anymore.
1 Like
Well, you can easily use it in your update method by assigning it in a script property like this:
this.app.mouse.on(pc.EVENT_MOUSEUP, function(event){
if (event.button === pc.MOUSEBUTTON_LEFT) {
this.mouseLeftReleased = true;
}
}, this);
And make sure to clear it at the end of your update loop:
MyScript.prototype.update = function(dt){
// update loop end
this.mouseLeftReleased = false;
};
1 Like
Thank you @Leonidas , that would indeed be the solution, but it seems that in my situation I can also use wasReleased .
1 Like
What is wasReleased when you use touch input?
So this line for touch input:
this.app.mouse.wasReleased(pc.MOUSEBUTTON_LEFT)
You have to listen for the touch end event.
Ah yes, offcourse. Thank you @yaustar .
Hmm, how can I do that inside the update?
saif
June 10, 2020, 8:15pm
#10
Albertos:
pc.MOUSEBUTTON_LEFT
Try this in update function, this should work
if (this.app.mouse.wasReleased((pc.MOUSEBUTTON_LEFT)))
console.log("Mouse was released");
Yes, thats working @saif , but I need the same also for touch input.
1 Like
You can’t in the update. You would have to store a flag from the event callback for touch end, and then check against the flag in update.
Same as this basically Mouse isPressed and wasPressed
saif
June 10, 2020, 8:37pm
#13
I have used this touch event on initialize i guess it would work fine in update too
this.app.touch.on(pc.EVENT_TOUCHEND, this.anyFunction, this);
But as @yaustar said it’s not a good practice to register any event again and again in update. You can do it on initialize and both mouse and touch function will work perfectly fine.
Okay, I just wanted to know. Then I have to add something like that.
It is only with certain conditions, so this will not be continuously checked in the update.
1 Like
saif
June 10, 2020, 8:42pm
#15
Fine then, do tell if touch function works for you
If I understand correctly this will be something like this?
Initialize:
this.isReleased = false;
this.app.touch.on(pc.EVENT_TOUCHEND, this.released, this);
Function:
Script.prototype.released = function() {
this.isReleased = true;
setTimeout(function(){
this.isReleased = false;
}, 100);
};
I would clear the flag at the end of update. Example: https://playcanvas.com/editor/scene/936587
// The touch event API can be found at http://developer.playcanvas.com/en/api/pc.TouchEvent.html
var TouchEnd = pc.createScript('touchEnd');
TouchEnd.attributes.add("cameraEntity", {type: "entity", title: "CameraEntity"});
// initialize code called once per entity
TouchEnd.prototype.initialize = function() {
if (this.app.touch) {
this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchEnd, this);
}
};
// update code called every frame
TouchEnd.prototype.update = function(dt) {
if (this._touchEndThisFrame) {
var cameraPanScript = this.cameraEntity.script.cameraPan;
cameraPanScript.startPan();
}
this._touchEndThisFrame = false;
};
TouchEnd.prototype.onTouchEnd = function(touchEvent) {
if (touchEvent.touches.length > 1) {
return;
}
this._touchEndThisFrame = true;
touchEvent.event.preventDefault();
};
1 Like
Just applied and it works fine! Thanks everyone.
1 Like