How to script click and drag puzzle pieces into place?

Started by OnlyOneKenobi, Wed 26/11/2003 09:15:27

Previous topic - Next topic

OnlyOneKenobi

Hey everyone, I'm not sure if this belongs here, but here goes

in my game I have a puzzle where you need to piece together a letter that was torn into several pieces. I started out by making a room with a layout of the completed letter, and then added the torn pieces as objects.

I tried different ways of getting the pieces to move, firstly by scripting it that when the player clicks on a certain piece, the object is turned off and mouse cursor then looks like the object the player clicked on. Then the player has to click on the correct hotspot to move the object into place, and the cursor returns to normal.

Originally what I wanted was the ability to click on an object and move it while holding the mouse button down, eventually having moved all 15 of the pieces into the correct places, the player should have an assembled letter, which would then be added into their inventory.

Can someone help me with scripting this or give me an idea what to do, I have no programming background and can only do basic scripting...

After

The easiest way to drag the pieces would surely be to move the object clicked on in step with the mouse while the button is down.
As for deciding whether they are in position, you could test each piece individually after each drag, and decide whether it's within the target area just by using its coordinates. Then automatically make it pixel-perfect when they are all close enough to be considered solved.
Just remember that the object coordinates are the bottom left of the graphic, so you have to offset your target areas if you want to give the player some slack.

Sorry, I don't have time for ready-made code right now.

OnlyOneKenobi

Thank you! Could you perhaps tell me what the right code would be for having the object move along with the mouse as long as the button is pressed? I've been through so many tutorials and forums but I cannot see what the right code would be for the object to move along with the mouse...  ???

Scorpiorus

#3
Dragging with left click:

Room script:

at the top of room script:

int button_pressed=0, ob=-1;


inside room's repeatedly execute:

 if (IsButtonDown(LEFT)) {
   if (button_pressed==0) {
     button_pressed = 1;
     ob = GetObjectAt(mouse.x, mouse.y);
   }
 } else {
   button_pressed=0;
   if (ob!=-1) {
     SetObjectBaseline(ob, 0);    
     ob=-1;
   }
 }
   

 if (ob != -1) {
   int width  = GetGameParameter(GP_SPRITEWIDTH, GetObjectGraphic(ob), 0, 0);
   int height = GetGameParameter(GP_SPRITEHEIGHT, GetObjectGraphic(ob), 0, 0);
   SetObjectBaseline(ob, game.room_height);
   SetObjectPosition(ob, mouse.x-width/2, mouse.y+height/2);
 }

How does it turn out?

OnlyOneKenobi

Thank you!!  ;D the click and drag works perfectly, now just one more question : How do I define a hotspot so that if the right object is dragged onto the right hotspot, the object stays there?

Scorpiorus

#5
Well, there are different ways. One could be to associate each object number with the appropriate hotspot number. They could have the same numbers:

object 0 is not used as there is no hotspot with number 0 (just ignore it)
object 1 <=> hotspot 1
object 2 <=> hotspot 2
object 3 <=> hotspot 3
object 4 <=> hotspot 4
etc...

first declare a new variable on top of the room script:

int correct = 0;

it is set to 1 if the puzzle is solved, otherwise it holds - '0'.


next all you need to add checking inside [EDIT]room's[/EDIT] repeatedly execute:

int i = 1; correct = 1;
while (i < _THE_NUMBER_OF_OBJECTS_YOU_HAVE_) {
correct=correct*(GetHotspotAt(GetObjectX(i), GetObjectY(i))==i);
Display("object number %d (correct=%d)", i, correct); //debug
i++;
}


if (correct == 1) {
 Display("Puzzle has just been solved!");
//NewRoom... or whatever
// ...
}



I wrote the script off hand, so see and tell me if it doesn't work

~Cheers

OnlyOneKenobi

Thanks for the help so far,

unfortunately when combining the two scripts in the room script, it doesn't quite work. The first script Scorpiorus gave me works fine for dragging the objects around but when I add the second part which defines which part is over the correct hotspot, I lose the dragging functionality... if needed, I'll include what the script looks like so far, I've been battling for weeks now  :-X  :( :( :(

Squinky

Yeah, you should probably slap the code up here. Sometimes problems are as simple as a missing bracket...

Scorpiorus


OnlyOneKenobi

Okay so after a few more days of struggling, this is what the code looks like now, I'm really lost!! Same problem as mentioned before. Cannot drag pieces anymore... help would be appreciated  8)

 // script for room: Repeatedly execute
int correct = 1;

int i = 1; correct = 1;
while (i < 15) {
correct=correct*(GetHotspotAt(GetObjectX(i), GetObjectY(i))==i);
Display("object number %d (correct=%d)", i, correct); //debug
i++;
}


if (correct == 1) {
Display("Puzzle has just been solved!");
// ...
}  
int button_pressed=0, ob=-1;

if (IsButtonDown(LEFT)) {
if (button_pressed==0) {
button_pressed = 1;
ob = GetObjectAt(mouse.x, mouse.y);
}
} else {
button_pressed=0;
if (ob!=-1) {
SetObjectBaseline(ob, 0);
ob=-1;
}
}


if (ob != -1) {
int width = GetGameParameter(GP_SPRITEWIDTH, GetObjectGraphic(ob), 0, 0);
int height = GetGameParameter(GP_SPRITEHEIGHT, GetObjectGraphic(ob), 0, 0);
SetObjectBaseline(ob, game.room_height);
SetObjectPosition(ob, mouse.x-width/2, mouse.y+height/2);
}


Scorpiorus

#10
Take a look at the test game: http://www.geocities.com/scorpiorus82/puzzle.zip

EDIT: an AGS 3.1.2 SP1 - compatible version: http://www.geocities.com/scorpiorus82/drag_puzzle_ags312sp1.zip

There is an on_puzzle_solved() function. Put in it whatever you want to happen when the puzzle is solved.

OnlyOneKenobi

Thank you Scorpiorus you're my hero!  :D :D I'll list you in my credits!!

Scorpiorus

You are welcome! Good luck with your game! :)

~Cheers

SMF spam blocked by CleanTalk