Sheep Herding Trials

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
chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 4:35 pm

A) Debugging shows everything is fine except for pTarget line 81 where the error is: error in object expression. Have no idea how to correct this. What is Important is that on two shut downs I noticed a split second image of a background behind a background where suddenly THOUSANDS of button sheep lay underneath the grey background colour?

B) This is the Only line of code that make no sense to me out of the whole lot. And happens to be the only line that dishes up a fault (murphys law), I know what it is doing. Once created it gets deleted, fine, that is the sheep, but how on earth does LC know that this parameter is a sheep button when I can not see where it has been given that information in the first place.

I know everyone is busy, so no rush, but would eventually appreciate some guidance please.
Kind regards
chris

Code: Select all

local sLevel, sImASheep, 

on levelIncrease
   --put empty into sLevel
   add 1 to sLevel
   sheepGenerate
end levelIncrease

on sheepGenerate  -- Only just lines A B C of this generate new btns every mouseup
   lock screen
   repeat with x=1 to sLevel
      repeat
         clone btn "templateSheep" --A
         set the name of the last btn to ("sheep" & x)
         set the loc of last btn to random(320), random(480) --B
         set the visible of the last button to true --C
         if intersect (btn ("sheep" & x) , grc "pen" , "255" ) is false and intersect (btn ("sheep"&x), grc "groupedControls" , "0") is false then
            --above must be on one line
            exit repeat
            delete btn ("sheep" & x)
         end if
      end repeat
      if the top of button ("sheep"&x) < the top of this card then
         set the top of button ("sheep"&x) to the left 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, grc "pen" , 255) and the cIsSheep of the target is true then
         set the backgroundcolour of the grc "pen" to "red"
      else
         set the backgoundcolour of grc "pen" to "blue"
      end if
   end if
end mouseMove

on mouseUp
   mouseRelease
end mouseUp

on mouseRelease
   put false into sImASheep
   if intersect (the target, grc "pen" , 255) and the cIsSheep of the target is true then
      sheepDelete the target
   end if
   set the backgroundcolour of the grc "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 fld "sheepcount" +1 into fld "sheepcount"
   put sheepLeft() into tCounts
   if tCounts < 2 then
      levelIncrease
      end if
end sheepDelete

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sheep Herding Trials

Post by Klaus » Wed Dec 11, 2013 4:44 pm

Hi Chris,

A:

Code: Select all

...
if intersect (btn ("sheep" & x) , grc "pen" , "255" ) is false and intersect (btn ("sheep"&x), grc "groupedControls" , "0") is false then

  ## THIS will cause that...
  exit repeat

  ## ... this line will never be executed! So that's the reason for you "sheep farming" :-)
  delete btn ("sheep" & x)
end if
...
Best

Klaus

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 5:27 pm

Sorry Klaus I have no idea what you are saying. Firstly I can not see a fault in any of these lines. Secondly, they are EXACTLY what is in the pdf and the video. Now this does not make them right, but I can tell you that all I get from LC is lock after lock after lock. But I can not see what on earth I am supposed to do to correct this.
regards
chris
I see there is a relationship between this handler and pTarget, but yet to understand that.
Was this game coded in an earlier version of LC?

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sheep Herding Trials

Post by Klaus » Wed Dec 11, 2013 5:36 pm

Hi Chris,

I don't care where these lines come from, they just don't make any logical sense! 8)

Read the lines again and see if you can get it:

Code: Select all

...
repeat...
  if whatever = TRUE then
    ## This line prevents EVERYTHING AFTER this line from being executed!
    ## Because this not only leaves the REPEAT loop, but also this IF THEN clause!
    exit repeat
    delete btn xyz
  end if
end repeat
...
Is the same as this:

Code: Select all

...
repeat...
  if whatever = TRUE then
    exit repeat
  end if
end repeat
...
Best

Klaus

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 5:54 pm

You know Klaus please please believe me, when I was first writing this I thought that it was odd. BUT because of the authority by whom it was written I did not question it, (not at my early stage), I actually thought that any fault had to be mine or a bug. So it now works as expected. But when Neil replied to me with his comment about that this line HAD to be on one line and not two, I discounted any PROBABILITY that it was faulty. Yet now I should have listened to my instincts and tested it before posting by commenting it out. Klaus, thanks for persevering with me.

These lines seemed pointless when I first read them, but the influence of authority pre-cluded any possibility that they should not be there.

regards
chris

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sheep Herding Trials

Post by Klaus » Wed Dec 11, 2013 6:07 pm

chris25 wrote:... that this line HAD to be on one line and not two...
Sorry, no capisce?

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 6:48 pm

about 7 posts down from this one "sheep code errors"
regards
chris

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sheep Herding Trials

Post by Klaus » Wed Dec 11, 2013 6:58 pm

chris25 wrote:about 7 posts down from this one "sheep code errors"
???

Please provide a link, thanks!

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 7:51 pm

Hi Klaus
http://forums.runrev.com/viewtopic.php?f=7&t=18341

the original question I had and Neil's reply.

Kindest regards
chris

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sheep Herding Trials

Post by Klaus » Wed Dec 11, 2013 7:54 pm

Aha, yes, but that was never the question here so far? 8)

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 8:11 pm

Aha No...But if it's nonsense...why neil's response? Anyway no matter, it's fixed, it works, the whole thing that is. And I thank the LC team that I could actually learn so much from the instructions and exercise from their donation of a free game with PDF and video. And now with my new knowledge about javascript I can clearly understand much more in LC. Now to break it up and study it all over again. :)
Kind regards
chris

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 8:23 pm

Have just discovered what that line of code might have been for now, after testing the game thoroughly I see the sheep duplicating on top of the grouped controls and the 'pen'. So now maybe something need to be inserted for this. With the code re-inserted it allows you to insert 3 sheep and then it locks.

LCNeil
Livecode Staff Member
Livecode Staff Member
Posts: 1223
Joined: Wed Oct 03, 2012 4:07 pm

Re: Sheep Herding Trials

Post by LCNeil » Wed Dec 11, 2013 8:43 pm

Hi Chris,

I responded in the context of your question and was not looking at the underlying code. The script was breaking because it was over two lines and not formatted correctly.

With that being said, I cannot get my head around why the code would not be needed ( i think i have looked at sheep herder far too much and its melting my brain :D )

I have attached the full sheepherder stack that "should" work as expected. I have include this script-

Code: Select all

 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)
-As if that is not present then the sheep will generate within both the pen and control group (which we dont want)

Hopefully I'm right but it has been a long day :D

Kind Regards

Neil Roger
--
RunRev Support Team ~ http://www.runrev.com
LiveCode – Realize fast, compile-free coding

Attachments
Sheep Herder.livecode.zip
(24.77 KiB) Downloaded 181 times

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sheep Herding Trials

Post by Klaus » Wed Dec 11, 2013 9:09 pm

Hi Neil,

Code: Select all

...
if intersect button... then
    exit repeat
end if
delete button ("sheep"&x)
...
Yep, THAT makes sense and I guess this is how this is scripted in the PDF and video
(wherever on the LC website that may be hidden) :D


Best

Klaus

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Sheep Herding Trials

Post by chris25 » Wed Dec 11, 2013 9:22 pm

Hallo Neil, well the mystery deepens. I'll try and keep it short - know you're tired. Basically I changed all the handlers in your stack to desktop handlers since they were the names used for android/IOS; I commented out the "put 30 into sLevel", and basically changed only that code that was specific for the phone app. Yours still did not work on my desktop. (I even left the intersect untouched). Basically actually it's funny, the first sheep to appear in your version runs away from my mouse pointer? Then it goes into the pen and that's the end of the game. Now your code is exactly the same as mine except for the intersect that I have gotten rid of because it does weird and wonderful things such as lock LC alltogether, and issues a fault with the parameter pTarget in the sheepDelete function. Otherwise mine works so much better, except for the fact that the sheep appear everywhere.

My initial report, BUT will take this further tomorrow, and look into everything to see what is going on tomorrow, but this was just a preliminary finding.

KIndest regards
chris

Update:
I copied the script of your version; along with of course the few changes that I made including adding a mouse down handler. I thne put that script into my stack and deleted the one that I had written according to the PDF, (made duplicate of my original stack of course). I then started both games. Yours still went no where, but oddly, mine behaved almost perfectly. The only slight difference being that the mouse pointer could move the sheep before the mousedown was operated but then needed the mousedown to move properly, otherwise everything worked. Tis is all a bit strange but nevermind.
regards
chris

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”