Page 1 of 1

Simplify this?

Posted: Sat Oct 18, 2008 7:14 pm
by Josh
Hi all,

Compiles for debugging are taking forever because of my code. Can any show me how to shorten/tighten this up? It basically involves 14 (or 15) green squares presented in random positions onscreen with 1 (or none) red square (the "target"). I bet a function would work, but I don't know how to do that or if that is the best route. Thanks for any help.

---

if targetTrial is true then
put true into collision
repeat until collision=false
put false into grandcollision
put 0 into newlocx
put 0 into newlocy
repeat until ((newlocx > 50+item 1 of windowBoundingRect) and (newlocx < item 3 of windowboundingrect - 50) and \
(newlocy > 50 + item 2 of windowBoundingRect) and (newlocy < item 4 of windowboundingrect - 50))
put 50 + random(item 3 of windowboundingrect - 50) into newlocx
put 50 + random(item 4 of windowboundingrect - 50) into newlocy
end repeat
move grc "targetrectangle" of card "practicesearch" of stack "metavssetsize" to newlocx,newlocy in 1 millisec
if intersect(grc "targetrectangle", grc "rectangle1") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle2") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle3") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle4") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle5") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle6") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle7") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle8") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle9") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle10") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle11") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle12") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle13") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if intersect(grc "targetrectangle", grc "rectangle14") then
put true into collision
put true into grandcollision
else
put false into collision
end if
if grandcollision then put true into collision
end repeat
end if

Posted: Sun Oct 19, 2008 7:55 am
by Janschenkel
The extractable part seems to be (with <X> a number ranging from 1 to 14):

Code: Select all

if intersect(grc "targetrectangle", grc "rectangle<X>") then 
  put true into collision 
  put true into grandcollision 
else 
  put false into collision 
end if
The 'tricky' bit being that if you make this into a function, you would need a way to return two values: 'collision' and 'grandcollision'.

So let's go with a command rather than a function, pass the index number of the rectangle to it, and use two parameters as 'reference' parameters rather than 'value' parameters.

Code: Select all

...
move grc "targetrectangle" of card "practicesearch" of stack "metavssetsize" to newlocx,newlocy in 1 millisec
repeat with tIndex = 1 to 14
  checkCollision tIndex, collision, grandcollision
end repeat
...

on checkCollision pIndex, @pCollision, @pGrandCollision
  put "rectangle" & pIndex into tRectangleName
  if intersect(grc "targetrectangle", grc tRectangleName) then 
    put true into collision 
    put true into grandcollision 
  else 
    put false into collision 
  end if
end checkCollision
HTH,

Jan Schenkel.