Here’s a simplified version of some code that doesn’t work well for me. This is on level 1, but I presume would have the same basic problem on other levels. Essentially what happens is that after I teleport, the hero thinks the nearest item is from pre-teleport location rather than post-teleport location.
But I have the item finding at the top of the loop and the move to the teleport location at the bottom of the loop. So shouldn’t the hero recalculate the nearest item AFTER teleporting? Or is something wrong with my logic? (probably!)
loop:
item = self.findNearest(self.findItems())
if item:
self.moveXY(item.pos.x,item.pos.y)
if self.pos.x > 40:
if self.pos.y > 40:
x = 76
y = 51
else:
x = 76
y = 19
else:
if self.pos.y > 40:
x = 5
y = 49
else:
x = 5
y = 19
self.moveXY(x,y)
Yeah, I mentioned in the “headhunter” thread that this seems to have started happening sometime yesterday. Code that I believe was substantially similar to this was working before that.
If no one sees a problem with this code then I can at least stop trying to fix something that is not wrong! But I’m also open to the fact that my code could be incorrect.
I wonder if what is happening is that the sequence goes like this:
go to teleport location
go to top of loop
find nearest item
teleport happens
In other words, maybe there is a delay such that you don’t teleport instantly when you move to the teleport location? Without knowing the back end I can only guess what is happening. But there definitely seems to be some “irregularity” associated with the teleport location. Seems like sometimes the teleport is fast and other times the hero sits on the teleport for some amount of time before teleporting? Hard to tell what is happening when things are moving though…
Originally I had the code symmetric (hence more concise) but it seems like the teleport locations are almost symmetric but not exactly (one Y is 49 and one is 51), so I coded to be as close to the middle of the “X” as possible, not knowing if it mattered or not.
coin = self.findNearestItem()
# Pick up the coin
self.moveXY(coin.pos.x, coin.pos.y)
the error is
coin (red) undefined is not a function
I have tried to change into another name penny. gold, nikel, round, tttttttt,…
no success
the same code several levels before worked without a problem
but in this level if I use the code from the level where only gold coins are mandatory it seems to work - but is not my target now - that code use array and while to loop through items
If there are no coins (or none within range of your glasses) then coin will not exist and you need “if coin:” to avoid trying to move towards a non-existent coin. Computers are SO literal. Same issue with enemies.
On levels where there are lots of coins or enemies you can often get away with not using that code though even then it’s good practice to check for existence of coin/enemy before trying to do something related to them.