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
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…
@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 .-.
@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 >.>
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.
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)
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.
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)