Jump to content

[Question] Trigger sprite events manually


palanolho
 Share

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

 

Link to comment
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

 

Link to comment
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.
}

 

Link to comment
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.

Link to comment
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 ?

Link to comment
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).

Link to comment
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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...