palanolho

[Question] Trigger sprite events manually

Recommended Posts

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

 

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

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.
}

 

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 ?

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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 :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.