Power Points Javascript Help [Solved]

// You need to find and destroy 3 skeletons.
// Skeletons and items are summoned at points of power.
// Move to a point and say the spell: "VENI".
// To find the required points, use the wizard's map.
// 0s are bad points. Positive numbers are good.

var spell = "VENI";
// The map of points is a 2D array of numbers.
var wizard = hero.findNearest(hero.findFriends());
var powerMap = wizard.powerMap;
var enemy  = hero.findNearestEnemy();
var item  = hero.findNearestItem();

// This function converts grid into x-y coordinates.
function convert(row, col) {
    return {x: 16 + col * 12, y: 16 + row * 12};
}

// Loop through the powerMap to find positive numbers.
// First, loop through indexes of rows.
for (var i = 0; i < powerMap.length; i++) {
    // Each row is an array. Iterate through it.
    for (var j = 0; j < powerMap[i].length; j++) {
        // Get the value of the i row and j column.
        var pointValue = powerMap[i][j];
        // If it's a positive number:
        if (pointValue > 0) {
            var point = convert(i,j)
            hero.jumpTo(point);
            hero.say("VENI");
            if (enemy) {
                hero.attack(enemy);
            }
            else if (item){
                hero.jumpTo(item.pos);
            }
            }
        }
            // Use convert to get XY coordinates.
            
            // Move there, say "VENI" and be prepared!
            
    }
}

MY hero does not do anything!
Also,
Screenshot 2021-02-25 at 12.00.44 PM
There is this bar at the bottom of the level that is not at the bottom of other levels!
Thanks

Hello, could you please send me the link to the level?

1 Like

CodeCombat - Coding games to learn Python and JavaScript?

1 Like

Thanks, @milton.jinich !

1 Like

MoveXY to the point, because your hero can’t always jump.

1 Like

@staff, Could you all help with the blue bar?

yeah, move to point.x, point.y

1 Like

Also you must first move to an item and second attack the enemy

1 Like

Move to (point.x, point.y). Then, attack the enemy while it’s health is greater than 0, you don’t want to attack the enemy when it is dead. In your else statement, move to the item’s position, don’t jump, you can’t always jump.

1 Like

Ok, I will be disconnected till tomorrow, so @abc , I am sure you can help without me, as I am a worse helper.

1 Like

Now the hero moves but, he still does not attack or go to items

// You need to find and destroy 3 skeletons.
// Skeletons and items are summoned at points of power.
// Move to a point and say the spell: "VENI".
// To find the required points, use the wizard's map.
// 0s are bad points. Positive numbers are good.
var spell = "VENI";
// The map of points is a 2D array of numbers.
var wizard = hero.findNearest(hero.findFriends());
var powerMap = wizard.powerMap;
var enemy = hero.findNearestEnemy();
var item = hero.findNearestItem();
// This function converts grid into x-y coordinates.
function convert(row, col) {
    return {
        x: 16 + col * 12,
        y: 16 + row * 12
    };
}
// Loop through the powerMap to find positive numbers.
// First, loop through indexes of rows.
for (var i = 0; i < powerMap.length; i++) {
    // Each row is an array. Iterate through it.
    for (var j = 0; j < powerMap[i].length; j++) {
        // Get the value of the i row and j column.
        var pointValue = powerMap[i][j];
        // If it's a positive number:
        if (pointValue > 0) {
            var point = convert(i, j);
            hero.moveXY(point.x, point.y);
            hero.say("VENI");
            if (enemy) {
                while (enemy.health > 0) {
                    hero.attack(enemy);
                }
            } else if (item) {
                hero.moveXY(item.pos.x, item.pos.y);
            }
        }
    }    // Use convert to get XY coordinates.
         // Move there, say "VENI" and be prepared!
}

Thanks @abc See you tomorrow @Anonym
Also there is error without a line “cannot read property ‘pos’ of undefined”

1 Like

You haven’t defined enemy or item right before this.

1 Like

I defined them at the top!

But at the top defines them one time, and it is not defined in the for loop.

1 Like

That was the issue! Thanks @abc

1 Like

Glad to help. Congratulations on solving the level.

1 Like

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.