Posted By
|
Message
|
wizkidweb
Registered 11/12/2007
Points 143
|
21st May, 2008 at 10:09:14 -
I'm trying to make a draggable item, but I want it to be restricted to a specific area. Using the current expressions I'm using, I don't see any way to do this and make it look good. My code is below:
• (Drag Box): internal flag 0 is off
+ Mouse pointer is over (Drag Box)
+ User Clicks with Left Button
- (Drag Box) Set internal flag 0 on
- (Drag Box) Set Alterable Value A to XMouse-X("Drag Box")
- (Drag Box) Set Alterable Value B to YMouse-Y("Drag Box")
• (Drag Box): internal flag 0 is on
- (Drag Box) Set X position to XMouse-Alterable Value A("Drag Box")
- (Drag Box) Set Y position to YMouse-Alterable Value B("Drag Box")
• X Repeat while left mouse button is pressed
- Set internal flag 0 off
- Set Alterable Value A to 0
- set Alterable Value B to 0
How would I make this restricted? If it is not possible with this code, is there another type of dragging setup that I could use to restrict the position?
When you do things right, people won't be sure you've done anything at all.
- God (Futurama)
|
Pixelthief Dedicated klik scientist
Registered 02/01/2002
Points 3419
|
21st May, 2008 at 12:15:08 -
What do you want to happen when the player tries to move out the box out of the specified area? Do you want the box to be restricted to the corners but still held, or do you want it to be "dropped"?
Either way, for what I'm going to show, remember that the ORDER of the events is extremely important- TGF/MMF always reads events from TOP to BOTTOM, and draws the frame only once the very bottom has been reached. So think about how the code flows in each read/eval/print loop;
For *RESTRICTING*, what you do is you check if the X/Y values are > or < a value, and set it to that value:
*(Drag Box): internal flag 0 is on
+(Drag Box): (X Position) > (400)
=(Drag Box): Set X position to 400
*(Drag Box): internal flag 0 is on
+(Drag Box): (X Position) < (0)
=(Drag Box): Set X position to 0
*(Drag Box): internal flag 0 is on
+(Drag Box): (Y Position) > (400)
=(Drag Box): Set Y position to 400
*(Drag Box): internal flag 0 is on
+(Drag Box): (Y Position) < (0)
=(Drag Box): Set Y position to 0
Make sure to place that code BELOW all the other code you listed; it will check the position AFTER it is set by the "Flag 0 is on" event.
For simply making the box drop:
*(Drag Box): internal flag 0 is on
+(Compare Two General Values): (XMouse - Alterable Value A("Drag Box") > 400)
=(Drag Box): Set flag 0 off
=(Drag Box): Set A to 0
=(Drag Box): Set B to 0
......
You need to insert this BETWEEN the #1 & #2 events you listed; rather then check AFTER you set the position of the box, what you are doing here is PREDICTING where the box will land in the next step; if it is out of bounds, you deactivate the box and don't actually execute the step that would have landed it out of bounds. This is called "A Priori Collision Detection", something you can read about on wikipedia- likewise, you should REVERSE the position in the code of the #2 & #3 events (The "Flag 0 is on" and "X While left mouse" events); what is happening is that if the player DROPS the box, the game will count it as being held for a single additional frame, because the "drop" event is lower down in the code then the "set position" event. While this might not make any noticeable difference, sometimes it add a screwy chance to your game.
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456
|
Pixelthief Dedicated klik scientist
Registered 02/01/2002
Points 3419
|
21st May, 2008 at 15:34:26 -
I've always found that snapping it every 24 pixels is easier to work with as
X = X / 24 * 24
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456
|
Pixelthief Dedicated klik scientist
Registered 02/01/2002
Points 3419
|
21st May, 2008 at 19:48:04 -
yah, many tricks in TGF/MMF are executed by being aware of the fact that the compiled will keep everything as an INT during calculations, not just AFTER calculations. This is actually true of most programming languages anyway. So when it encounters
X / 24 * 24
It writes it as
Mull(Div(X,24),24)
and executes as
Div(x/24) = INT
Mull(that,24) = INT
thus if you have some equation that requires you to divide a number very close to 1, for example if you want to take a % of something, instead of writing
24.48% of 70 is .2448 * 70, such as
X = 2448/10000
Result = 70 * X
Which will NOT return "24.48% of 70", but rather 0, since .2448 gets rounded down to zero. Instead you need to write
X = 2448
Result = (70 * X) / 10000
easier in a regular programming language that lets you declare floats
Gridquest V2.00 is out!!
http://www.create-games.com/download.asp?id=7456
|
wizkidweb
Registered 11/12/2007
Points 143
|
21st May, 2008 at 22:12:06 -
Hey, thanks PixelThief. That's exactly what I needed. I can't believe I didn't think of that!
When you do things right, people won't be sure you've done anything at all.
- God (Futurama)
|
Assault Andy Administrator
I make other people create vaporware
Registered 29/07/2002
Points 5686
|
22nd May, 2008 at 05:33:57 -
I also use the method that pixeltheif described for 'snapping' objects. I discovered it a while ago and I've used it since. It's just faster to type out in the editor than using mod.
Creator of Faerie Solitaire:
http://www.create-games.com/download.asp?id=7792
Also creator of ZDay20 and Dungeon Dash.
http://www.Jigxor.com
http://twitter.com/JigxorAndy
|
|
|