PDA

View Full Version : simple drag and drop game



spes
September 15th, 2008, 06:32 AM
Hey, I'm no programmer and not so good in actionscript (in AS3). I can't seem to find anything out there that could help me with what I'm trying to do. Which is a simple "drag and drop on to a target" kind of game where there are several draggable objects and drop targets on screen. So here's what I need help with:

1) the ability to drag any objects and can be dropped on any drop targets.

2) the ability to drag any objects and can only be dropped on certain drop targets.

3) the ability to drag any objects of the same kind, and drop it on certain drop targets.

4) if the correct kind of object is dropped on the correct drop target, an action will execute (immediately or at a later time).


The only solution I can imagine is by labeling certain objects into classes (ie: small object, big object etc.) and telling the target to allow these certain classes to be dropped on them. But I have absolutely no idea how to even start that.

Can anyone direct me on how to do this? A link or keywords I should be searching for online? Thank you in advance! :)

theCodeBot
September 15th, 2008, 06:57 AM
Hey, I'm no programmer and not so good in actionscript (in AS3). I can't seem to find anything out there that could help me with what I'm trying to do. Which is a simple "drag and drop on to a target" kind of game where there are several draggable objects and drop targets on screen. So here's what I need help with:

1) the ability to drag any objects and can be dropped on any drop targets.

2) the ability to drag any objects and can only be dropped on certain drop targets.

3) the ability to drag any objects of the same kind, and drop it on certain drop targets.

4) if the correct kind of object is dropped on the correct drop target, an action will execute (immediately or at a later time).


The only solution I can imagine is by labeling certain objects into classes (ie: small object, big object etc.) and telling the target to allow these certain classes to be dropped on them. But I have absolutely no idea how to even start that.

Can anyone direct me on how to do this? A link or keywords I should be searching for online? Thank you in advance! :)

For dragging the objects, look into startDrag and stopDrag. There are slightly better ways to do this, but they're a pain to code. In terms of dropping them, set it so that when you stop dragging the object, figure out what drop targets it's touching.
epending on the targets, it may or may not be allowed to jump into the target like you dropped it there.

The code's a little long, but basically each object would have a name to determine it's type, ex "smallBox_1" for the first small one, and the object would check for "smallBox" in the name, which in this case there is.

spes
September 15th, 2008, 11:15 PM
For dragging the objects, look into startDrag and stopDrag. There are slightly better ways to do this, but they're a pain to code. In terms of dropping them, set it so that when you stop dragging the object, figure out what drop targets it's touching.
epending on the targets, it may or may not be allowed to jump into the target like you dropped it there.

The code's a little long, but basically each object would have a name to determine it's type, ex "smallBox_1" for the first small one, and the object would check for "smallBox" in the name, which in this case there is.


Hey, thanx for the insights!
I did run into something similar to determine the type of object by its name but it was for specific items for specific drop target.. the example they gave is by naming the objects box01, box02 etc.. and drop targets named smallBoxbox01, smallBoxbox02, etc. So it only allows specific objects to drop onto a specific drop target:

"smallBox" + event.target

event.target being the name of my object that is currently dragged. But that would only mean I can drop box01 onto smallBoxbox01.


Following your example, how do I tell the drop target to check for "smallBox" in the name of the objects so that if it detects it is true, it will allow that object to be dropped on it?

Thanks again! I appreciate the help. :)

theCodeBot
September 16th, 2008, 06:46 AM
Hey, thanx for the insights!
I did run into something similar to determine the type of object by its name but it was for specific items for specific drop target.. the example they gave is by naming the objects box01, box02 etc.. and drop targets named smallBoxbox01, smallBoxbox02, etc. So it only allows specific objects to drop onto a specific drop target:

"smallBox" + event.target

event.target being the name of my object that is currently dragged. But that would only mean I can drop box01 onto smallBoxbox01.


Following your example, how do I tell the drop target to check for "smallBox" in the name of the objects so that if it detects it is true, it will allow that object to be dropped on it?

Thanks again! I appreciate the help. :)

Run a for loop when you stop dragging the object. This for loop will use hitTestObject to see if it is hitting any of the targets. Each time it notices it is touching one, since you're already looking at both the dragged object and the target, read in both of their name properties and do some substring magic (as in, read "smallBox" is in the object, and if "smallBox" is in the drop target, do the drop)