Jump to content

Conditional dragging


jmdcodejunkie
 Share

Recommended Posts

I am trying to build a game where you are limited to a certain number of items in a stack, instead of generating the number of items, I track how many are present, and I use a spawner object that generates clones on dragStart to simulate dragging from a pile. what I want to do is if the player attempts to drag from the pile when there are no more items, then the item will tint to be darker and drag will be prevented. but I want to re-enable dragging if we drop an item into the area, thus adding to the stack. I was wondering if there was a way to prevent a drag conditionally onDragStart, or do I have to toggle drag on/off with conditionals outside of the onDragStart and onDragEnd events.

Link to comment
Share on other sites

I know that, but thats not conditional... I am looking more for something like

sprite.events.onDragStart(function(sprite, eventPointer) {
   if(sprite.data.count <= 0) {
      //code to prevent this drag from starting...
   }
   else {
      // allow drag to proceed
   }
});

if we use sprite.input.disableDrag(), then there is never another chance to check if the count is less than zero. I have to put a second check somewhere else to call sprite.input.enableDrag() before this event (onDragStart) even gets called...

one common practice used that I have seen is that the event would have a propagate property or a canceled flag... or the event function would be checked to see if it returns true or false... haven't tested a return of true or false but I don't think it works that way... its common with onClick event handlers in javascript, I know that.

Link to comment
Share on other sites

27 minutes ago, jmdcodejunkie said:

I know that, but thats not conditional... I am looking more for something like


sprite.events.onDragStart(function(sprite, eventPointer) {
   if(sprite.data.count <= 0) {
      //code to prevent this drag from starting...
   }
   else {
      // allow drag to proceed
   }
});

if we use sprite.input.disableDrag(), then there is never another chance to check if the count is less than zero. I have to put a second check somewhere else to call sprite.input.enableDrag() before this event (onDragStart) even gets called...

one common practice used that I have seen is that the event would have a propagate property or a canceled flag... or the event function would be checked to see if it returns true or false... haven't tested a return of true or false but I don't think it works that way... its common with onClick event handlers in javascript, I know that.

I can't think of any way to do this that doesn't involve some sort of check outside of the onDragStart.

This doesn't exactly answer your original question, but why not just remove the sprite when the pile is empty? It seems to me if there's none left in the pile, there shouldn't be anything there for the person to try and drag in the first place. When you drop a new one into the pile, just add the sprite back. To me that sounds like the simplest solution, but that is based on my limited knowledge of your game.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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