Drag&Drop Entity


(Gil) #1

I’m fairly new to AS3/Flashpunk and I’m currently working on a project where I want my entities to be click&draggable to a specific portion of the screen. In other words…

I can click and drag “Entity” around the left half of the screen whereas if i attempt to drag the entity further to the right of the screen with the mouse, the object will simply halt at the farthest available point on the screen (left half).

Although I’m asking what presumably seems like a lot to ask, I still haven’t even been able to make the entity click&draggable. Through the use of Input(Mouse.Down) and x = world.mouseX, y = world.mouseY; I was only able to make it so that wherever I clicked on the screen, the entity would follow. It wouldn’t be a click and drag but more like the entity teleporting around the screen in accordance to my mouse clicks.

Please help me with these issues, I have a great appreciation for the Flashpunk Engine and its special features and utilities but my knowledge of these things is ofcourse very limited.

Thank You!


(Jonathan Stoler) #2

There may be an easier way to do this, but should work.

  1. Check for mouse clicks.
  2. If the mouse click is within your entity (for a square, simply if x >= entity.x && x <= entity.x + entity.width and the same for y, using height), then:
  3. Figure out where the mouse is relative to the entity’s origin. So if the mouse if 5 pixels to the right and 3 pixels above, store both values. Store the fact that the mouse is down and the entity is therefore movable.
  4. If the “mouse is down/entity movable” boolean is set, set the entity’s x and y coordinates to the mouse’s x and y coordinates, plus the offset (offsets can be negative). Before setting them, be sure to check these coordinates to make sure they don’t go past whatever your “farthest available point” is.
  5. When the mouse is released, unset the movable boolean.

A few notes:

  • You’ll want to use mousePressed in order to set the movable property. If you use mouseDown, then this means you can “pick up” other entities while dragging one entity, since you need to hold the mouse button down to drag.
  • You can subclass Entity and make something like DraggableEntity so you don’t have to write this code every single time you want a new Entity with this functionality.
  • You might be able to simply set the entity’s origin to wherever the mouse is relative to the entity, and then you wouldn’t have to deal with offsets. I’m not sure how this will work in practice - it might result in some weird behavior, and unfortunately I can’t test it right now.

(azrafe7) #3

@JonathanStoler explained it really well, but I had just started writing it when he posted, so here it is: a very basic DraggableEntity (came up with the same name huh?!).

To use it:

  • subclass it and don’t forget to set your hitbox/mask
  • override onStartDrag() and onEndDrag() if you need a callback in those situations
  • constraint its position in the update() function by checking the isDragging property
  • set the draggable property to false if you don’t want it to be draggable

Example: zip | swf


(Gil) #4

I thank the both of you so much, this is definitely what I was looking for and thanks to the both of you my project will no longer be on what seemed to be at the time, an indefinite hiatus.

Edit: @azrafe7

I was even able to use FP.clampInRect from your example to manipulate where my entities could be draggable on the screen (Instead of it being within the game screen I made it so that the entity could only be draggable within the left side of the screen which was the first part of my inquiry). Will work tomorrow on collisions! Thanks again!


(Abel Toy) #5

You could also offset the Entity x and y depending on where the drag is started.


(Zachary Lewis) #6

@azrafe7 You’re driving me crazy with these Dropbox links! It’s not your fault that I can’t access Dropbox at work, but you can just drag and drop the files into the forum and it’ll upload and host that mess for you. That would be real swell. :sweat_smile:


(azrafe7) #7

Sorry for that, I know I can just use the forum builtin feature, but there are a couple of reasons why I’m using dropbox links:

  1. I can modify the same files without having to change the links after the first upload (creating a new revision)
  2. I have all my snippets in one centralized place

But if number one can be solved in some way, I’ll be glad to convert my habits.


(Zachary Lewis) #8

You do you, mang. I just know that if it’s on my server, I’ll always have access to the version you shared.

— Sent from Mailbox for iPhone