jmdcodejunkie

Conditional dragging

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.

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Well you can try 

sprite.events.onDragStart(function(_sprite, eventPointer) {
   if (_sprite.data.count <= 0) {
      _sprite.input.stopDrag(eventPointer);
   }
   else {
      // allow drag to proceed
   }
});

 

Share this post


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

  • Recently Browsing   0 members

    No registered users viewing this page.