I have been wrestling with this one off and on for a week or so. I won’t explain all the different things I was able to accomplish…and the other difficulties, just the present one. Of which, my present difficulty is not moving. (But when I take the below code and make it the upper part of an ‘else’ statement, it does run).
Thanks for the feedback.
while(true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
// buildXY a "fence" 20 meters to enemy's left.
var enemyPos = enemy.pos;
var x = enemy.pos.x;
var y = enemy.pos.y;
hero.buildXY("fence", x - 20, y);
var heroPos = hero.pos;
var heroPosX = hero.pos.x;
var heroPosY = hero.pos.y;
hero.moveXY(heroPosX, heroPosY - 12);
}
}
Hi, first of all, all the var
s are a bit unnecessary; you could do the build and the move in one line each. Anyway, that’s not the problem, the problem is you don’t have an else on the bottom bit. You might have already tried that, if so change you glasses to ones with a small range, 5m will be alright. You might have loaded the level with the twilight glasses already loaded. Which would cause a problem.
I hope this helps
-Danny
Thanks. I have updated the code, and yes, I had him working with an else statement before, but still had other troubles. Here is my updated code, but he still sits there:
while(true) {
var enemy = hero.findNearestEnemy();
if (enemy) {
// buildXY a "fence" 20 meters to enemy's left.
hero.buildXY("fence", enemy.pos.x - 20, enemy.pos.y);
hero.moveXY(hero.pos.x, hero.pos.y - 12);
} else {
hero.moveXY(hero.pos.x, hero.pos.y - 12);
hero.findNearestEnemy();
}
}
I don’t know why your hero isn’t moving, but your code is still wrong.
Why are you moving after you make a fence and when you can’t see an enemy? If you have the intended glasses, you shouldn’t need to. Next, the distance to move is 10 not 12.
With those changes your code works on my account, if your’s still doesn’t work please send a screenshot of your equipment and, if possible, of the level screen itself.
-Danny
Good question(s). I don’t recall if I had the 12 on purpose or not…its been a while. I had put the move method immediately following the build method, since I had removed the else, and never took it back out. I will try to run this with the advice given today and see if I can get it working, and will report back. Thanks again.