Skip to first unread message

Dario Hernandez

unread,
Jul 21, 2014, 6:51:56 PM7/21/14
to mitappinv...@googlegroups.com
Hi,

I first want to thank MIT for the App Inventor 2 this is an amazing time saver. Now to my question: I am using a sprite to collide with another sprite. I am using the drag handler. What I'm trying to do is if Sprite A (stationary) and Sprite B (User Drag Input) do not collide I want Sprite B to return to it's original coordinates. Can some please recommend a solution for the drag handler. 

My apologies if my terminology is incorrect.

Thanks in advance 

Abraham Getzler

unread,
Jul 21, 2014, 8:47:19 PM7/21/14
to mitappinv...@googlegroups.com
You need a global variable to remember whether or not you collided with another sprite.
Call the variable I_Collided, for example.

At Screen1.Initialize time, set I_Collided false.

When you collide, set I_Collided true and global Destination_x and y from the target's x and y.

When you start the drag, save starX and startY in global variables, for the original coordinates.

When you get TouchUp on the draged sprite, check I_Collided:
   If true, set its x and y to Destination_x and ..._y,
   otherwise set its x and y to Original_x and _y
set I_Collided = false to prepare for the next drag.

ABG


Dario Hernandez

unread,
Jul 21, 2014, 9:16:20 PM7/21/14
to
Thank you for the suggestion and I will get right to work on it. I will try your recommendation and hope that you don't mind if I hit a bump in the road and ask you a few question on this issue. 

Thanks

Dario Hernandez

unread,
Jul 21, 2014, 10:25:07 PM7/21/14
to mitappinv...@googlegroups.com

I tried your suggestion and I almost have it working to what I originally wanted. The bug is that when shape B returns to (StartX , StartY) it comes close to origin but slightly off. By the 4th or 5th drag it ends up off to a lower bottom corner. Do you have any suggestions?

Muchas Gracias

Abraham Getzler

unread,
Jul 22, 2014, 11:05:32 AM7/22/14
to mitappinv...@googlegroups.com
If you are dragging pieces around on a game board, the target coordinates
can be tightened up using snap-to calculations. 

For example, the target coordinates on a chess board
can be snapped to upper left corners of the squares
by running them thru this calculation ...

(for x coordinate)
subtract x value of board's left side (so x is between 0 and board width )
divide by width of one square (so x is measured in squares)
round x to nearest whole number
multiply by width of one square (reversing step 2)
add x value of board's left side (reversing step 1)

(for y coordinate)
similar to x coordinate, but you have to take into account
y goes in negative direction.

ABG

Dario Hernandez

unread,
Jul 22, 2014, 10:42:44 PM7/22/14
to mitappinv...@googlegroups.com
Thank You for your help :)
Reply all
Reply to author
Forward
0 new messages