Help with a Maze

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Help with a Maze

Post by phebreze » Sun Jun 26, 2016 7:42 pm

Hello, I'm currently trying to create a maze where you drag a ball to the finish line to go to the next card. I have a series of rectangles on top of one another for the path, and an image called "theBlue.png" in the background where I want an intersection of the ball being dragged and "theBlue.png" to reset the coordinates of the ball. Attached is an image of what it looks like.

Basically, what I'm trying to do is have an intersection of the red ball with the blue background image trigger a reset of the ball. I tried to do this with the mouseStillDown handler, but to no avail. Any ideas for what code could allow this to work?

Thanks! #firstpost!
Attachments
Capture.PNG
Last edited by phebreze on Sun Jun 26, 2016 10:22 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Help with a Maze

Post by dunbarx » Sun Jun 26, 2016 10:07 pm

Hi.

Maybe think of this the other way. If the ball is not within the rect of any of your rectangle graphics, you detect that and destroy the computer. Trap the "mouseMove" message and loop through the several graphics, testing for "within".

Craig Newman

phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Re: Help with a Maze

Post by phebreze » Sun Jun 26, 2016 10:21 pm

dunbarx wrote:Hi.

Maybe think of this the other way. If the ball is not within the rect of any of your rectangle graphics, you detect that and destroy the computer. Trap the "mouseMove" message and loop through the several graphics, testing for "within".

Craig Newman
What would some example code for this look like? I'm not too familiar with LiveCode, sorry!

phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Re: Help with a Maze

Post by phebreze » Mon Jun 27, 2016 12:12 am

dunbarx wrote:Hi.

Maybe think of this the other way. If the ball is not within the rect of any of your rectangle graphics, you detect that and destroy the computer. Trap the "mouseMove" message and loop through the several graphics, testing for "within".

Craig Newman
Additionally, what do you mean by "trapping" the "mouseMove" command and "looping through the several graphics?"

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Help with a Maze

Post by dunbarx » Mon Jun 27, 2016 1:52 am

Hmmm. you need to learn the basics of LC.

That said, do this. Make a button and a field on a new card. Now make a freehand graphic that mimics a maze. Please practice and learn how to draw such a thing. You can make it simpler than your current maze, just as long as it contains just a few twists and turns. Put this into the card script:

Code: Select all

on mouseMove
   put "" into fld 1
   if the optionKey is down then set the loc of btn 1 to the mouseLoc
   if intersect(grc 1,btn 1,255) then put "Crash" into fld 1
end mouseMove
Hold down the optionKey (why is that there at all?) and see what that button is up to.

Now look up all of these messages and functions. Try to see what each line does (there are only three, you know) and write me back with what you learn, discover and are thrilled about.

Craig

phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Re: Help with a Maze

Post by phebreze » Mon Jun 27, 2016 3:05 am

dunbarx wrote:Hmmm. you need to learn the basics of LC.

That said, do this. Make a button and a field on a new card. Now make a freehand graphic that mimics a maze. Please practice and learn how to draw such a thing. You can make it simpler than your current maze, just as long as it contains just a few twists and turns. Put this into the card script:

Code: Select all

on mouseMove
   put "" into fld 1
   if the optionKey is down then set the loc of btn 1 to the mouseLoc
   if intersect(grc 1,btn 1,255) then put "Crash" into fld 1
end mouseMove
Hold down the optionKey (why is that there at all?) and see what that button is up to.

Now look up all of these messages and functions. Try to see what each line does (there are only three, you know) and write me back with what you learn, discover and are thrilled about.

Craig
Well, funny thing is, this is part of a large final project for the summer course I'm taking on LiveCode. I just wasn't quite sure what to do in this situation and was a little bit confused by the wording.

When I executed the code, holding down the alt key brought the button to the location of my cursor, and an intersection of the button and the cursor filled the field with "Crash."

Line one dealt with clearing the field on mouseMove.
Line two dealt with bringing the button to the cursor while the alt key is held down.
Line three dealt with detecting the collision.

I got that. I'm just confused as to how I could implement this into the maze. Also, what's 255?

EDIT: I put some code into the card script that seemed to work as long as each rectangle had the same name. However, I couldn't find a way to do this without releasing the mouse. Here was the code:

Code: Select all

on mouseMove
if intersect(grc "theBall",grc "Rectangle") then
   else
      set the loc of grc "theBall" to 21,255
      end if
end mouseMove
I'm trying to find a way where while dragging the ball, touching the blue will reset the location, instead of dragging the ball and releasing on the blue to reset the location.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Help with a Maze

Post by dunbarx » Mon Jun 27, 2016 4:53 am

You are among the only people who have ever taken be at my word and actually examined the code as I asked.

So can you see your way to making your maze with a single, though admittedly complex polyline? It will take just a little patience. But the advantage is enormous, in that only a single control is present, instead of that suite of rectangles you had. This means that only a single pair of controls need be tested for intersection.

If you had done it with all those rectangles, you would have had to (pseudo):

Code: Select all

rrepeat with y = 1 to the number of graphics
if intersect("ball",grc y,"255") then resetBall
end repeat
The "255" is what you were supposed to learn about when you read ALL of the dictionary entry for the native function "intersect". Get going. If you are industrious, lose that parameter entirely in my handler, and see what happens.

The pseudo "resetBall" is just whatever you want to do when you get a collision. Might it be something like "set the loc of grc "ball" to startingLoc"?

Craig

phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Re: Help with a Maze

Post by phebreze » Mon Jun 27, 2016 4:18 pm

dunbarx wrote:You are among the only people who have ever taken be at my word and actually examined the code as I asked.

So can you see your way to making your maze with a single, though admittedly complex polyline? It will take just a little patience. But the advantage is enormous, in that only a single control is present, instead of that suite of rectangles you had. This means that only a single pair of controls need be tested for intersection.

If you had done it with all those rectangles, you would have had to (pseudo):

Code: Select all

rrepeat with y = 1 to the number of graphics
if intersect("ball",grc y,"255") then resetBall
end repeat
The "255" is what you were supposed to learn about when you read ALL of the dictionary entry for the native function "intersect". Get going. If you are industrious, lose that parameter entirely in my handler, and see what happens.

The pseudo "resetBall" is just whatever you want to do when you get a collision. Might it be something like "set the loc of grc "ball" to startingLoc"?

Craig
Hmm. Whenever I execute that code, my program stops responding.

EDIT: The line that I made has a box around it, and even if the ball goes off of the line, it's still in this "box," so the new code won't execute. What can I do to get around this?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Help with a Maze

Post by dunbarx » Mon Jun 27, 2016 5:30 pm

Execute the "rrepeat" loop? With that extra "R"? (typo).

That was only to show you what you might have to go through if you did not make the maze from a single graphic polyLine. It was "pseudo" after all, though a little sloppy, I admit. I think the compiler would have been justified in throwing an error.

I want you to do this with one polyline, Do you have any issues with that? You may, and please enumerate them. My point is one of simplified coding, not structural issues with your project. Let me know...

Craig

phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Re: Help with a Maze

Post by phebreze » Mon Jun 27, 2016 5:45 pm

dunbarx wrote:Execute the "rrepeat" loop? With that extra "R"? (typo).

That was only to show you what you might have to go through if you did not make the maze from a single graphic polyLine. It was "pseudo" after all, though a little sloppy, I admit. I think the compiler would have been justified in throwing an error.

I want you to do this with one polyline, Do you have any issues with that? You may, and please enumerate them. My point is one of simplified coding, not structural issues with your project. Let me know...

Craig

I'm saying that the line I made has a box around it, and even when the ball isn't on the line, if it is still in that box, then it registers as still being on the line. Am I constructing the line wrong?

Also, I put the code in without the typo and adjusted everything to the names of my objects.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Help with a Maze

Post by richmond62 » Mon Jun 27, 2016 6:21 pm

newMaze.png
Have fun!
Attachments
maze.livecode.zip
Here's the stack
(48.75 KiB) Downloaded 154 times

phebreze
Posts: 7
Joined: Sun Jun 26, 2016 7:35 pm

Re: Help with a Maze

Post by phebreze » Mon Jun 27, 2016 7:45 pm

richmond62 wrote:
newMaze.png
Have fun!
Hey, thanks for this! I hope you don't mind, but I rewrote the code from the ground up how I would write it because this is my final project, but this helped me understand this much better! I think the arrowKey handler was the way to go on this. Thank you!

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10099
Joined: Fri Feb 19, 2010 10:17 am

Re: Help with a Maze

Post by richmond62 » Mon Jun 27, 2016 8:15 pm

You're more than welcome :)

It would be both fun and informative of you could post your rejigged version here.

Here's my take:
beachy.jpg
https://www.dropbox.com/s/nbf70thqcamr2 ... e.zip?dl=0

You will see that this uses a graphic object in such a way that the 'crab' always faces in the right direction.

Post Reply