Page 1 of 2

Drag and drop question.

Posted: Mon Sep 29, 2008 9:16 am
by Omar319
Hi,
I've already made a drag and drop game but I have a small problem.
I drag a field into a box and if it matches the target the score goes up in a separate field. But there is a way around this by dragging it into the correct target and constantly clicking so the score goes up. Is there any way to lock them when they are in the target location?
Image

Posted: Mon Sep 29, 2008 9:47 am
by Mark
Post your script, Omar

Mark

Posted: Tue Sep 30, 2008 11:25 am
by Omar319

Code: Select all

global my_old_position

on mouseDown
  set the layer of me to the num of controls
  put the loc of me into my_old_position
  grab me
end mouseDown

on mouseUp
  if within (img "Yellow" ,the loc of me) then
    add 1 to field "Answer"
  else
    move me to my_old_position
    subtract 1 from field "Answer"
  end if
end mouseUp
I'd like to lock the field once it's within the target location unless the reset button's pressed.

Thanks,
Omar :)

Posted: Thu Oct 02, 2008 7:38 pm
by Mark
Hi Omar,

I posted a script here, but I have noticed that it doesn't work. I am not sure what your problem is, but I guess that the "move" command is too slow. Why don't you set the loc at once?

set the loc of me to my_old_position

This line set the loc instantly, too quick for the user to interfere with additional mouseClicks.

Best,

Mark

Drag and Drop the easy way

Posted: Thu Oct 02, 2008 10:41 pm
by Jerry Daniels
Omar, is there some reason you're not using the built-in drag and drop that rev 2.9+ offers? It's documented in the User Guide.

Posted: Fri Oct 03, 2008 9:56 am
by malte
Almost hate to say it, but this screams for animationEngine. <g>

If you are interested in an example how to do this with AE, I'd cook it up. If not, please ignore this post.

Cheers,

Malte

Posted: Fri Oct 03, 2008 2:27 pm
by Randy Hengst
Hi Malte,

I've been following this tread and have AE, but haven't begun to work it. I'd be interested in seeing drag drop with AE.

Posted: Fri Oct 03, 2008 9:17 pm
by malte
Hi Randy,

I'll cook up a stack. :) Family is demanding this weekend (need to harvest our apples), so it is unlikely to be before monday though.

Cheers,

Malte

Posted: Sat Oct 04, 2008 12:58 am
by Randy Hengst
Hi Malte,

Thanks for your willingness to do this. I'll look forward to seeing it, but there's no hurry. I, too, have plenty of family events this weekend.

take care,
randy

Posted: Sat Oct 04, 2008 11:38 pm
by malte
Hi,

couldn't resist. :-)

the stack is here:

http://derbrill.de/revstack/AEDrag.rev

It requires animationEngine to be loaded and in use. Open up animationengine and type

start using stack "animationEngine" into the messagebox.

I created a stack, 1 button,3 flds, 3 grcs. LockText of flds=true, traversalOn=false. Each field owns a custom property calle cInitialPositian, which holds the loc, the field should snap back to. The script in the reset button calls a handler "resetGame" in the card script. The card owns this script:

Code: Select all

--> all handlers

-- quick AE drag and drop Demo

on openCard
    resetGame
end openCard

on resetGame
    if "animationEngine" is not among the lines of the stacksinuse then
        if "animationEngine" is among the lines of revLoadedStacks("application") then
            start using stack "animationEngine"
        else
            answer "This stack needs animationEngine to run!"
            exit resetGame
        end if
    end if
    aeLockMoves
    aeMoveTo the long id of fld "fld,red",the cInitialPosition of fld "fld,red",200,"inout"
    aeMoveTo the long id of fld "fld,green",the cInitialPosition of fld "fld,green",200,"inout"
    aeMoveTo the long id of fld "fld,yellow",the cInitialPosition of fld "fld,yellow",200,"inout"
    aeUnlockMoves
    set the constrainRectangular of fld "fld,red" to the rect of this card
    set the constrainRectangular of fld "fld,green" to the rect of this card
    set the constrainRectangular of fld "fld,yellow" to the rect of this card
end resetGame

on constrainRectangularExit
    local tColor
    put item 2 of the short name of the target into tColor
    if the mouseloc is within the rect of grc ("grc,"&tColor) then
        set the constrainRectangular of the target to empty
        aeMoveTo the long id of the target,the loc of grc ("grc,"&tColor),300,"overshoot"
    else
        aeMoveTo the long id of the target,the cInitialPosition of the target,350,"bounce"
    end if
end constrainRectangularExit
If you have any questions, please feel free to ask.

Cheers,

Malte

Posted: Sun Oct 05, 2008 2:00 am
by Randy Hengst
Malte,

Very helpful. Thanks for taking the time to do this.

take care,
randy

Posted: Sun Oct 05, 2008 12:16 pm
by malte
Glad it helps randy. I really must write more tutorials. However, that works best when I see a situation where I think AE might help, or if something is requested.

Cheers,

Malte

Posted: Sun Oct 05, 2008 2:06 pm
by SparkOut
Yes thank you Malte, it's true this situation screamed for AE, as you say. I haven't worked with AE enough to realise the simplicity and efficiency of code that can be realised with this engine. Brilliant.

Posted: Mon Oct 06, 2008 9:27 am
by malte
Thanks for the kind words sparkout. :)

I try my best to keep AE in a good shape. I rely on it in almost every project I start, so it is high on my priority list to make it work efficiently.

All th best,

Malte

Posted: Mon Oct 06, 2008 12:47 pm
by Mark
Nice example, Malte. Thanks!

Now, what's the logic behind the constrainRectangularExit message? When does it get sent and what triggers it?

Mark