getXPosition/getYPosition don't always work

I have seen several issues in student projects where the getXPosition and getYPosition set variables to 0 as opposed to their actual values.

We found a fix to use getProperty instead of getPosition, but it seems like there is an error in AppLab. Sometimes the getPosition works, other times it doesn’t.

In this game - in the initial run of the game, you can move around and “eat” the objects. Calling the function to reset the board after the first round, all the getX and getY set the variables to 0. We switched iceCreamImage to getProperty both at the top and in the showFood function on lines 348 and 349.

So, if you play the game linked below, you’ll notice after winning or losing the first round - the only food item you can “eat” is the Ice Cream and the other object x and y positions show 0 in the watchers.

1 Like

Here is what I found so far. It works the first time through, but not the second and third. That means the code that runs the first time is different from what runs the second etc.

When I look at the code I see huge amounts of duplicated code. This program should be about 1/3rd the size.

So job 1 is to eliminate the duplication. Make some functions.

This is an example of what can actually happen in a real programming situation. You can spend the rest of your life looking for bugs in code that doesn’t have any bugs. You won’t find one. What you need to do is look at the duplicated code that does.

In other words, if the bug is in the second or third duplication and you are looking at the first version you won’t find the bug.

Update: I cleaned up the code and getXPosition/getYPosition do always work.

It looks like the main character was rolling over from top to bottom, left right, etc. I changed that to just stop at the edge.

One thing I would definitely recommend against is setting the x, y position with setProperty.

1 Like