If conditional prevents placement of object

Greetings,

I have two nested if loops that generate desktopOval objects. No problem. However, when I put in an if/then conditional to jump out of the inner loop, nothing is displayed; not even earlier objects. I tried oval.refresh. laserDot() is in a set. Here is a bit of the code:

For i, For x

for x=laser(i).left to laser(i).left + 200 paint out 200 ovals (later this will be undefined) therefore why the color.blue conditional

Var d As DesktopOval=New laserDot

laserDot(n).fillcolor=Laser(i).colour
laserDot(n).Width=1
laserDot(n).Height=1
laserDot(n).Visible=true

‘oval is placed on the screen
laserDot(n).left=x+laser(i).Width/2 '~301
laserDot(n).top=laser(i).top+ slope*(x-laser(i).left)+laser(i).Height/2

‘check for a color.blue pixel at the location of the most currently generated oval

‘this does work! However, the if conditional prevents any oval from being painted

pixelColor=rgbs.Pixel(laserDot(n).left, laserDot(n).top)
blue=pixelColor.Blue.ToString.ToInteger

‘the process is to stop if the line of ovals encounter a blue border
if blue>253 then continue For i
Do loops don’t work, same issue.

next x
next i

Thanks

A thought. I had a similarly mysterious thing happen and it resolved itself when the Var declaration was placed outside the loop.

Probably not “mysterious”, since if you declare within the loop, it will be scrubbed and re-instantiated every time through. :slight_smile:

1 Like

Step through the code, and/or add

System.DebugLog Str(Blue)

right after

blue=pixelColor.Blue.ToString.ToInteger

and whatever the problem is will probably become apparent.

Not mysterious at all. Thats how the language is defined. Any variable you Var within a loop ceases to exist when you exit the loop.

2 Likes

That’s not relevant here, because the window holds a reference to any UI elements that are placed into them.

This is probably the issue. Instead of referring to laserDot(n), refer to d:

d.Fillcolor = Laser(i).Color

That guarantees that you’re always manipulating the object you intend to, eliminating the possibility of slippage if n is not the correct value.

1 Like

You wouldn’t happen to have a blue line on the left would you? It seems that if that were the case, the first laser dot would hit the left blue area and prevent drawing as you said.

Personally, I think the OP should humor Tim (and me), and try doing the Vars outside the loops. :slight_smile:

1 Like

I agree that it’s generally good practice, but I don’t think it’s the issue here. The variable created in the Var reference is literally never used.

The glaring error in this code is the missing definition of n. The value assignment of this variable is not shown in the code – and definitely isn’t incremented as the loop creates more and more LaserDot objects – so this is probably where the error is. See my post above that shows how to revise the code to properly reference the object you are creating.

I suggest OP to create a sample project that shows the problem. Zip it and upload here. Sometimes that helps to find the problem.

2 Likes

This won’t compile.

There is nothing in the code the OP posted that suggests where/how he is adding d to laserDot. Also, my comment was directed to Keith. Should have quoted him.

Not to mention that the OP is using “laserDot” as both a class name and an array name.

this code example is incompleate and chaotic, you should rewrite.

instead of DesktopOval maybe use OvalShape for drawing because you read color from rgb surface.
https://documentation.xojo.com/api/graphics/ovalshape.html

You are soooo right! Learned my lesson about var. I had a similar problem earlier in the code, but didn’t make the connection!

I’m sure somewhere in the millions of words of documentation the locality of var.

Thanks for your help! Have a great programming new year.

Carl

Case closed. It’s all about the locality of var!! Thanks for your help.