Page 1 of 1

Sheep Herder Academy lesson 3 broken

Posted: Tue Apr 23, 2013 5:00 pm
by ianpiper
Hi,

I've had a lot of trouble getting this working. I have tried it in the commercial demo and the community edition and the result is the same. In desperation I even copied the code from the tutorial. Turns out that code has a number of flaws. It may help other new users to see what was wrong. I've added the code at the end of this posting because I couldn't add the text file to my posting.

The first problem with the code as written at the end of the lesson is that there is no mouseDown handler. Without a mouseDown handler the app doesn't respond to mouseDown events. So I added back in the mouseDown code from a previous lesson:

Code: Select all

on mouseDown
   if the cIsSheep of the target is true then
      put true into sImASheep
   end if
end mouseDown
And now the behaviour is different. I can create sheep, drag one to the pen and drop it. But when I mouse up the sheep disappears but the pen is stuck to the mouse. Now I can move the mouse/pen around and click on sheep and they do indeed disappear. But this is not the expected behaviour. So I look at the mouseUp handler, and it runs mouseRelease. In the latter, the code shown at the end of the lesson again differs from the code earlier in the lesson. The line

Code: Select all

put false into sImASheep
is missing (it was in the first line of on mouseRelease earlier in the lesson). If I put this back in, then I see loads of errors of the form "intersects: threshold must be a non-negative number, "bounds", "pixels" or "opaque pixels". It turns out that the lines with code like this:

Code: Select all

if intersect (the target, Graphic "pen", 255) and the cIsSheep of the target is true then
should have quotes around the number 255, like this:

Code: Select all

if intersect (the target, Graphic "pen", "255") and the cIsSheep of the target is true then
When I add these quotes the lesson code works as expected.

Phew! So, what am I trying to say here? Simple: lessons like this must be right or you will drop learners off by the wayside. I left this tutorial for several days because I was having such a hard time trying to understand what was amiss.

A related peeve here is that there is a lot of inconsistency in coding syntax. The quoting is one example, and I initially thought it didn't matter whether things have quotes around them. Turns out it does matter. So how about spacing? We see variously ("sheep"& x) and ("Sheep"&x), intersect (the target, graphic "pen",255) and intersect (button("sheep"&x),graphic"pen","255") and so on. So does white space matter anywhere?
How about text case? Is (the target, Graphic "pen" the same as (the target, graphic "pen" ? Finally, I see "the button" and just plain old "button" used interchangeably - do they mean the same thing? It's great if the language has this flexibility and ability to discriminate, but you have to understand the confusion that this creates in the mind of a new user, especially when syntax is picky elsewhere.

I'm finally starting to enjoy using the environment, but I am going through a completely unnecessary additional learning curve in figuring out where it's sloppy syntax variation and where it's just plain wrong code.


Ian.
--

===== faulty code copied from the end of lesson 3 follows ====

Code: Select all

Local sLevel, sImASheep
on levelIncrease
--put empty into sLevel
add 1 to sLevel
sheepGenerate
end levelIncrease
on sheepGenerate
lock screen
repeat with x = 1 to sLevel
repeat
clone button "templateSheep"
set the name of the last button to ("sheep"& x)
set the loc of the last button to random(320), random(480)
set the visible of the last button to true
if intersect (button("sheep"&x),graphic"pen","255") is false and intersect(button("sheep"&x),group"groupControls","0") is false then
exit repeat
end if
delete button ("sheep"&x)
end repeat
if the top of button ("Sheep"&x) < the top of this card then
set the top of button ("Sheep"&x) to the top of this card
end if
if the left of button ("Sheep"&x) < the left of this card then
set the left of button ("Sheep"&x) to the left of this card
end if
if the right of button ("Sheep"&x) > the right of this card then
set the right of button ("Sheep"&x) to the right of this card
end if
if the bottom of button ("Sheep"&x) > the bottom of this card then
set the bottom of button ("Sheep"&x) to the bottom of this card
end if
end repeat
unlock screen
end sheepGenerate
on mouseMove
if sImASheep is true then 
set the loc of the target to the mouseLoc
if intersect (the target, Graphic "pen", 255) and the cIsSheep of the target is true then
set the backgroundcolor of the graphic "pen" to "red"
else
set the backgroundcolor of the graphic "pen" to "blue"
end if
end if
end mouseMove
on mouseUp
mouseRelease
end mouseUp
on mouseRelease
if intersect (the target, graphic "pen",255) and the cIsSheep of the target is true then
sheepDelete the target
end if
set the backgroundcolor of the graphic "pen" to "blue"
end mouseRelease
function sheepLeft
local tCount
repeat with x = 1 to the number of buttons of me
if the cIsSheep of button x of me is true then
add 1 to tCount
end if
end repeat
return tCount
end sheepLeft
on sheepDelete pTarget
local tCounts
delete pTarget
put field "sheepcount" + 1 into field "sheepcount"
put sheepLeft() into tCounts
if tCounts < 2 then
levelIncrease
end if
end sheepDelete
==== And here is a working version ====

Code: Select all

Local sLevel, sImASheep
on levelIncrease
add 1 to sLevel
sheepGenerate
end levelIncrease
on sheepGenerate
lock screen
repeat with x = 1 to sLevel
repeat
clone button "templateSheep"
set the name of the last button to ("sheep"& x)
set the loc of the last button to random(320), random(480)
set the visible of the last button to true
if intersect (button("sheep"&x),graphic"pen","255") is false and intersect(button("sheep"&x),group"groupControls","0") is false then
exit repeat
end if
delete button ("sheep"&x)
end repeat
if the top of button ("Sheep"&x) < the top of this card then
set the top of button ("Sheep"&x) to the top of this card
end if
if the left of button ("Sheep"&x) < the left of this card then
set the left of button ("Sheep"&x) to the left of this card
end if
if the right of button ("Sheep"&x) > the right of this card then
set the right of button ("Sheep"&x) to the right of this card
end if
if the bottom of button ("Sheep"&x) > the bottom of this card then
set the bottom of button ("Sheep"&x) to the bottom of this card
end if
end repeat
unlock screen
end sheepGenerate

on mouseDown
   if the cIsSheep of the target is true then
      put true into sImASheep
   end if
end mouseDown

on mouseMove
if sImASheep is true then 
set the loc of the target to the mouseLoc
if intersect (the target, Graphic "pen", "255") and the cIsSheep of the target is true then
set the backgroundcolor of the graphic "pen" to "red"
else
set the backgroundcolor of the graphic "pen" to "blue"
end if
end if
end mouseMove
on mouseUp
mouseRelease
end mouseUp
on mouseRelease
   put false into sImASheep
if intersect (the target, graphic "pen","255") and the cIsSheep of the target is true then
sheepDelete the target
end if
set the backgroundcolor of the graphic "pen" to "blue"
end mouseRelease
function sheepLeft
local tCount
repeat with x = 1 to the number of buttons of me
if the cIsSheep of button x of me is true then
add 1 to tCount
end if
end repeat
return tCount
end sheepLeft
on sheepDelete pTarget
local tCounts
delete pTarget
put field "sheepcount" + 1 into field "sheepcount"
put sheepLeft() into tCounts
if tCounts < 2 then
levelIncrease
end if
end sheepDelete

Re: Sheep Herder Academy lesson 3 broken

Posted: Thu Apr 25, 2013 4:13 pm
by jonnyh1994
Another thing to add is that I can't seem to move my sheep! I've gone to the point of copying the code on the website and your code and they just won't move when I use the cursor on normal mode! Have no idea how to fix this either :(

Re: Sheep Herder Academy lesson 3 broken

Posted: Thu Apr 25, 2013 6:14 pm
by LCNeil
Dear ianpiper,

I'm sorry to hear of the issue you have been experiencing.

Thank you for bringing to our attention the errors in the documentation. We will have these corrected promptly.

The intersect issue that you have been experiencing was a bug that was introduced with the release of LiveCode 6.0. This has now been corrected and the intersect function should work as expected.

This fix along with many others is included in the latest version of LiveCode (6.0.1)

Numbers in this function can either be enclosed in double quotes or not. If you were to use another of the intersects parameters in place of the numbers (e.g. opaque pixels) then this would be required to be in double quotes.

LiveCode is also very forgiving when it comes to its syntax. There are multiple ways to do/write the same thing and you have picked out some great examples of these. This also applies to the capitalisation of characters and use of spaces.

Some examples that I can think of are-

on mouseDown
grab me
end mouseDown

This can be written as

on mouseDown
grab <--------space------------> me
end mouseDown

on mouseDown
GrAb Me
end mouseDown

on mouseDown
grab button 1
end mouseDown

on mouseDown
grab button "nameofthebutton"
end mouseDown

on mouseDown
grab button ID 1004
end mouseDown

on mouseDown
grab btn 1
end mouseDown

Having these multiple option available is extremely handy when coding but I can understand the frustration it can cause for a new programmer. As this is the case, we will focus on trying to keep a consistent syntax in future academies.

@johhnyh1994

If possible, could you copy your script into a reply or send your stack to support@runrev.com and I will take a look to see what could be causing this issue for you.

Kind Regards,

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
--

Re: Sheep Herder Academy lesson 3 broken

Posted: Mon May 27, 2013 12:38 pm
by William Jamieson
My sheep are extremely lazy too. Cant get them to do anything. Did you find out how to get the sheep to move?

I am running the program in the Livecode development environment and testing it on Android. I got Sheep Herder to resize very smoothly to any screensize and all else is working except the fact that I cant download the course materials so I do not yet have the graphics needed. So right now its "smiley face" herder. But with both environments, neither the mouseup functions are working nor the touchmove functions for android. I have them put in an if statement that determines the environment, but I have tried them outside of the if statement as well. So please post if you have found any solution to the drag function.

Thank you!

Re: Sheep Herder Academy lesson 3 broken

Posted: Tue Jun 04, 2013 1:11 pm
by LCNeil
Dear William Jamieson,

Thank you for your request,

Are you able to run your stack successfully within the desktop environment? (e.g. sheep move as expected)

If possible, could you include your sheep herder stack to a reply and I will happily investigate why the mouseup/touchmove commands are not working for yourself.


Kind Regards,

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
--

Re: Sheep Herder Academy lesson 3 broken

Posted: Sun Jul 07, 2013 5:15 pm
by William Jamieson
Sorry for the late reply. I used a couple feedback variables and put in a few if statements and made it work :)