Help! rich forager

loop {
var flag = this.findFlag();
var enemy = this.findNearestEnemy();
var item = this.findNearestItem();

if (flag) {
    // What happens when I find a flag?
    this.pickUpFlag(flag);
}
else if (enemy) {
    // What happens when I find an enemy?
    this.attack(enemy);
    this.attack(enemy);
}
else if (item) {
    // What happens when I find an item?
    var posi = pos.item;
    var fx = pos.x;
    var fy = pos.y;
    this.moveXY(fx, fy);
}

}
what have I done wrong? I thought I got it the first time but it keeps saying pos being undefined

This is javascript by the way. I just don`t know what I should do!

Hey.

This part is fairly incorrect:

var posi = pos.item;
var fx = pos.x;
var fy = pos.y;

You are getting the position of the item, not the item of “pos”:

var posItem = item.pos;

After that, you can just do:

var fx = posItem.x;
var fy = posItem.y;

Everything should work now!

Thanks!!! I couldnt solve it and after I changed it, all went smooth;; but my hero keeps dying!! Is it just I need to buy an item?? Its basically my first time coding so I`m pretty lost…

It’s the one where you need to collect the coins and kill guys?

Yeah, you kind if have to buy an armor so you have more health. Even if your algorithm is fine, you can’t really beat it… Kind of p2w in this case :slight_smile:

@foxpc Wow, I bought Hattori Hanzo for the extra speed/damage, using Tharin for this level, all my money was used on Hattori and I literally can’t continue the game without buying gems for more armor
GG Code Combat .-.

I was able to do it with Thunderfist, along sword, the heavy iron breastplate, and the polished bronze helmet. I survived with 11 health

@Anoner59 I have a Tarnished Bronze Breastplate, a Polished Bronze Helmet, and the Long Sword, only have 11 gems, it’s literally impossible to beat it without paying >.>

Which hero do you have?

@Anoner59 Tharin (20 characters pls)

Are you using “cleave”? It was impossible for me to do it with the samurai without cleave, but once I included cleave in my code, I finished the level.

@Anoner59 Only using attack and shield, don’t know how to incorporate cleave into the code

loop:
Enemy = self.findNearestenemy
if enemy:
if self.isReady(“cleave”):
self.cleave(enemy)
else:
self.attack(enemy)

Can someone tell me what I did wrong with this code?
I’m able to walk around and use cleave, but with cleave can’t kill the minion then it won’t auto attack nor will it use shield

loop:
flag = self.findFlag()
enemy = self.findNearestEnemy()
item = self.findNearestItem()

if flag:
    # What happens when I find a flag?
    self.pickUpFlag(flag)   
    
elif enemy:
    # What happens when I find an enemy?
        loop:
            if enemy:
            self.isReady("cleave")
            self.cleave(enemy)
            self.attack(enemy)
	        self.shield()
	
elif item:
    # What happens when I find an item?
    pos = item.pos
    x = pos.x;
    y = pos.y;
    self.moveXY(x, y)

There were a few problems. You have a second loop in there. Which it will never leave. That’s a no no. Also you weren’t checking if cleave was ready and had an extra if enemy in there. Also using shield is kind of pointless. I removed it. It only works if you stand still and attack will always be available so it wouldn’t ever fire. here it is cleaned up

loop:
    flag = self.findFlag()
    enemy = self.findNearestEnemy()
    item = self.findNearestItem()

    if flag:
        # What happens when I find a flag?
        self.pickUpFlag(flag)   

    elif enemy:
        # What happens when I find an enemy?
        if self.isReady("cleave"):
            self.cleave(enemy)
        else
            self.attack(enemy)

    elif item:
        # What happens when I find an item?
        pos = item.pos
        x = pos.x;
        y = pos.y;
        self.moveXY(x, y)

loop:
flag = self.findFlag()
enemy = self.findNearestEnemy()
item = self.findNearestItem()
yobro = item.pos
itemX = yobro.x
itemY = yobro.y

if enemy:
    if self.isReady("cleave"):
        self.cleave(enemy)
    else:
        self.attack(enemy)
else:
    if flag:
        self.pickUpFlag()
    else:
        self.moveXY(itemX, itemY)

My problem is that it keeps on saying that “item” is null where I bolded it. I can’t figure out the problem. If it is relevant, I am on google Chrome

“self.findNearestItem()” will sometimes return “null” if there isn’t an item on the screen or within range of your character’s line of sight. You will want to do an “if item:” check before accessing its properties and before trying to move to that location.

1 Like

loop:
flag = self.findFlag()
enemy = self.findNearestEnemy()
item = self.findNearestItem()

if flag:
    self.pickUpFlag(flag)
elif enemy:
    self.attack(enemy)
elif item:
        itemPos = item.pos
        itemX = itemPos.x
        itemY = itemPos.y
        self.moveXY(itemX, itemY)

do something like this:
loop:
flag = self.findFlag()
enemy = self.findNearestEnemy()
item = self.findNearestItem()

if flag:
    # What happens when I find a flag?
   self.pickUpFlag(flag)
elif enemy:
    # What happens when I find an enemy?
    if self.isReady("cleave") and self.distanceTo(enemy) <5:
        self.cleave(enemy)
    else:
        self.attack(enemy)
elif item:
    # What happens when I find an item?
    pos = item.pos
    x = pos.x
    y = pos.y
    self.moveXY(x, y)

I got the right coding, but every 2 battles, I die at the 3rd one! There has to be a easier way to survive this! Please make it do not involve any purchasing, also, here’s my code:
loop:
flag = self.findFlag()
if flag:
self.pickUpFlag(flag)
flag = self.findFlag()
enemy = self.findNearestEnemy()
item = self.findNearestItem()
if enemy:self.attack(enemy)