Sheep Herder Academy lesson 3 broken
Posted: Tue Apr 23, 2013 5:00 pm
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:
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
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:
should have quotes around the number 255, like this:
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 ====
==== And here is a working version ====
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
Code: Select all
put false into sImASheep
Code: Select all
if intersect (the target, Graphic "pen", 255) and the cIsSheep of the target is true then
Code: Select all
if intersect (the target, Graphic "pen", "255") and the cIsSheep of the target is true then
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
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