I have been stuck on this level for quite a while now…PLEASE HELP. This is my code so far and the picture and the link:
link_https://codecombat.com/play/level/drop-the-flag
My code
// Put flags where you want to build traps.
// When you’re not building traps, pick up coins!
while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
var flag = hero.findFlag("green");
hero.buildXY("fire-trap", flagX, flagY);
hero.pickUpFlag(flag);
}
else {
var item = hero.findNearestItem();
if (item) {
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
}
}
while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
var flag = hero.findFlag("green");
hero.buildXY("fire-trap", flagX, flagY);
hero.pickUpFlag(flag);
}
else {
var item = hero.findNearestItem();
if (item) {
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
}
}
}
Would you mind hilighting the part where you are talking about for me?
Problem is that you said if item instead of finding the flag so your item is undefined.
Look below:
while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
var flag = hero.findFlag(“green”);
hero.buildXY(“fire-trap”, flagX, flagY);
hero.pickUpFlag(flag);
}
So you didn’t define Item, cause you defined flag, so in your code you’re finding item instead of flag.
No everytime i reset it so that could try te code over again it automaticlly tells me its wrong…
hero.buildXY("fire-trap", flagX, flagY);
That this is wromg.
this is code on reset:
// Put flags where you want to build traps.
// When you’re not building traps, pick up coins!
while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
hero.buildXY("fire-trap", flagX, flagY);
hero.pickUpFlag(flag);
}
else {
var item = hero.findNearestItem();
if (item) {
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
}
}
while(true) {
var flag = hero.findFlag();
if (flag) {
// How do we get flagX and flagY from the flag’s pos?
// (Look below at how to get x and y from items.)
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
var flag = hero.findFlag(“green”);
hero.buildXY(“fire-trap”, flagX, flagY);
hero.pickUpFlag(flag);
}
else {
var item = hero.findNearestItem();
if (item) {
var itemPos = item.pos;
var itemX = itemPos.x;
var itemY = itemPos.y;
hero.moveXY(itemX, itemY);
}
}
}
problem is that you put var flag = hero.findFlag();
and then you put
if (flag) { var itemPos = item.pos
…
…
…
}
not the flag, so basically you created a variable and you’re looking for the
variable that you didn’t define
The wrong variable
Also @juraj_pechac tried to explain you that you have to find flag’s pos, not the items pos.
I just figured it out. My code was right i just was’nt putting my flags on the x. Sorry about that I am new to this whole thing. Thankyou soo much for your help.