Hello, Can you explain to me why it tells me that “attackEnemy”, is not a function? However,I declared the function well, it seems to me, can you tell me my mistake? Thanks in advance
// Les péons tentent de vous voler vos pièces!
// Ecrivez une fonction pour les rembarrer avant qu'ils ne puissent vous prendre vos pièces.
auto pickUpCoin() {
auto coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
int main() {
// Ecrivez la fonction attaqueEnnemi ci-dessous.
// Trouvez les ennemis les plus proches et attaquez-les s'ils existent!
auto attackEnemy = hero.findNearestEnemy();
if (attackEnemy) {
hero.attack(attackEnemy);
}
while(true) {
attackEnemy(); // ∆ Décommentez cette ligne après avoir écrit la fonction attaqueEnnemi.
pickUpCoin();
}
return 0;
This is the enemy itself, it’s not a function, to attack the enemy, you need to do hero.attack(enemyVariable) and in this case enemyVariable is attackEnemy, so instead of doing attackEnemy() you do hero.attack(attackEnemy)
Welcome to the forum @Loctaw! This is a friendly place where you can ask help on levels, report bugs, or just chat with other coders! Have a great time
Thanks for your help ! I’m a beginner in programming, and I’m trying to understand, sorry if I ask questions that may be futile in your eyes. I modified my code by placing it above “int main()” when I was recommended to put it below, and it worked. Is there something to modify for entry below “int main()” or the recommendation was wrong?
And thank you for the welcome!
auto pickUpCoin() {
auto coin = hero.findNearestItem();
if(coin) {
hero.moveXY(coin.pos.x, coin.pos.y);
}
}
auto attackEnemy() {
auto enemy = hero.findNearestEnemy();
if (enemy) {
hero.attack(enemy);
}
}
int main() {
// Ecrivez la fonction attaqueEnnemi ci-dessous.
// Trouvez les ennemis les plus proches et attaquez-les s'ils existent!
while(true) {
attackEnemy(); // ∆ Décommentez cette ligne après avoir écrit la fonction attaqueEnnemi.
pickUpCoin();
}
return 0;
}