palanolho Posted June 11, 2018 Share Posted June 11, 2018 Hi everyone, I'm trying to do something that I don't know if it's possible nor what is the best/correct way to do it. Basically, I have a sprite that has "pointer..." events associated. Example: .on('pointerup', doSomething) This sprite is draggable and I can drop it in some other places. However, at some point, the sprite may lose it's "interactivity" (it's on purpose) and I would like the trigger the effect of "pointerup" so that the sprite is handled as if the user triggered that event (otherwise the sprite gets stuck on the locations the pointer was at the moment when the sprite lost its interactivity. So, my question is: Is it possible to trigger an event for a given sprite manually? or is there a better way to achieve what I'm trying to do? Many thanks in advance, - Miguel Quote Link to comment Share on other sites More sharing options...
Exca Posted June 12, 2018 Share Posted June 12, 2018 sprite.emit("pointerup"). If you need to have some event associated with it, then sprite.emit("pointerup", eventObject). Quote Link to comment Share on other sites More sharing options...
palanolho Posted June 12, 2018 Author Share Posted June 12, 2018 Thanks @Exca, will give it a try. Do you know if there's any more general "touch" method that would allow to click on a given location of the screen? I'm not really sure if I will have access to the sprite being dragged and I want to make sure the event is triggered on an "empty space" many thanks in advance Quote Link to comment Share on other sites More sharing options...
Exca Posted June 12, 2018 Share Posted June 12, 2018 You could listen for the window events? That way the region will be the whole browser instead of only canvas. Usually when I do dragging it's something like this: container.addListener("pointerdown", onPointerDown); function onPointerDown(){ window.addEventListener("touchmove", onPointerMove); window.addEventListener("mousemove", onPointerMove); window.addEventListener("touchend", onPointerUp); window.addEventListener("mouseup", onPointerUp); } function onPointerUp(){ //Do something } function onPointerUp(){ //Clear listeners and do something. } Quote Link to comment Share on other sites More sharing options...
botmaster Posted June 12, 2018 Share Posted June 12, 2018 1 hour ago, palanolho said: Thanks @Exca, will give it a try. Do you know if there's any more general "touch" method that would allow to click on a given location of the screen? I'm not really sure if I will have access to the sprite being dragged and I want to make sure the event is triggered on an "empty space" many thanks in advance Keep in mind that "fake" interaction events do not have credentials and cannot be used in replacement of true user interaction. For example, on Ios the WebAudioAPI can only be activated upon a true user interaction event, faking such event will have no effect. Quote Link to comment Share on other sites More sharing options...
palanolho Posted June 12, 2018 Author Share Posted June 12, 2018 4 hours ago, botmaster said: Keep in mind that "fake" interaction events do not have credentials and cannot be used in replacement of true user interaction. For example, on Ios the WebAudioAPI can only be activated upon a true user interaction event, faking such event will have no effect. botmaster Oh I know that, and that is not the case (I think?!) Imagin you have an inventory with slots and "items" on those slots. There are some equipment slots outside the inventory where you can drag&drop the item from the inventory slots into the equipment slots. However, I also have a timer and the player has a certain amount of time to equip whatever he wants to equip and change equipment. Once the time ends, the items lose their "interactivity" and the player won't be able to equip them anymore. The problem is that at the point the time expires, the player may be in the middle of dragging an item into a slot. and since the item loses its interactivity, it gets stuck where the "pointer" was when it lost its interactivity. So basically what I want to do when this happens is to move the item back into the slot as if the user was not dragging it. I know the original location of the item and the my "pointerup" function already makes validations regarding this and moves the item to the correct location if it's "dropped" on an invalid location. That's why I was trying to trigger the "pointerup" event on an invalid location to trigger this behaviour. Do you know any better/simpler way to do this ? Quote Link to comment Share on other sites More sharing options...
Exca Posted June 13, 2018 Share Posted June 13, 2018 Move your pointerup-logic to another function and then call that function from both cases. That way you dont have to fake interaction in between. Also makes it a lot nicer to add some extended features later on if needed (for example noticing that mouse pointerup should have some animation that cancelation timer shouldnt have etc). Quote Link to comment Share on other sites More sharing options...
palanolho Posted June 13, 2018 Author Share Posted June 13, 2018 Oh dum me... That's what I have! I have a function that is executed on "pointer up" and I totally forgot that lol. I can even out some validations to check "if the time is over" and in that case drop the sprite back on its original location. Thanks for the reminder Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.