Don't Touch Them (JavaScript)

I’m stuck on the level Don’t Touch Them. I read through another thread but it dod not solve the problem and the thread cut off before someone really helped. Can someone please help me? There are no code errors that pop up and I hit the goals but the level does not end. Here’s the code I used:

function createGenerator(spawnType, x, y, spawnAI) {
var generator = game.spawnXY(“generator”, x, y);
generator.spawnType = spawnType;
generator.spawnAI = spawnAI;
}

// Scouts are aggressive and munchkins are just walking.
createGenerator(“scout”, 12, 12, “AttacksNearest”);
createGenerator(“scout”, 68, 56, “AttacksNearest”);
createGenerator(“munchkin”, 12, 56, “Scampers”);
createGenerator(“munchkin”, 68, 12, “Scampers”);

var player = game.spawnPlayerXY(“duelist”, 40, 34);
player.maxHealth = 1000;
player.attackDamage = 20;
player.maxSpeed = 20;

// These are our goals. Notice we save them in variables!
var spawnMunchkinsGoal = game.addManualGoal(“Let 6 munchkins spawn.”);
var dontTouchGoal = game.addManualGoal(“Don’t attack munchkins.”);
var defeatScoutsGoal = game.addManualGoal(“Defeat 6 scouts.”);
// game properties used to count new and defeated ogres.
game.spawnedMunchkins = 0;
game.defeatedScouts = 0;
game.successGoal = 0;
ui.track(game, “spawnedMunchkins”);
ui.track(game, “defeatedScouts”);

function onSpawn(event) {
game.spawnedMunchkins += 1;
}

function onDefeat(event) {
var unit = event.target;
if (unit.type == “scout”) {
game.defeatedScouts += 1;
}
if (unit.type == “munchkin”) {
// dontTouchGoal is failed if a munchkin is defeated.
game.setGoalState(dontTouchGoal, false);
player.say(“Oops.”);
}
}

function checkGoals() {
// If game.defeatedScouts is greater than 5:
if (game.defeatedScouts > 5) {
// Set defeatScoutsGoal state as successful…
game.setGoalState(defeatScoutsGoal, true);
}
// If game.spawnedMunchkins is greater than 5:
if (game.spawnMunchkins > 5) {
// Set spawnMunchkinsGoal state as successful…
game.setGoalState(spawnMunchkinsGoal, True);
}
// If both other goals are completed.
if (spawnMunchkinsGoal.success) {
if (defeatScoutsGoal.success) {
// Set dontTouchGoal state as successful…
game.setGoalState(dontTouchGoal, true);
}
}
}

game.setActionFor(“munchkin”, “spawn”, onSpawn);
game.setActionFor(“munchkin”, “defeat”, onDefeat);
game.setActionFor(“scout”, “defeat”, onDefeat);

while (true) {
checkGoals();
}

1 Like

please format your code properly using this

I did format it properly but when I pasted it in this chat the formats were removed. See screenshot:

1 Like

function createGenerator(spawnType, x, y, spawnAI) {
var generator = game.spawnXY(“generator”, x, y);
generator.spawnType = spawnType;
generator.spawnAI = spawnAI;
}

// Scouts are aggressive and munchkins are just walking.
createGenerator(“scout”, 12, 12, “AttacksNearest”);
createGenerator(“scout”, 68, 56, “AttacksNearest”);
createGenerator(“munchkin”, 12, 56, “Scampers”);
createGenerator(“munchkin”, 68, 12, “Scampers”);

var player = game.spawnPlayerXY(“duelist”, 40, 34);
player.maxHealth = 1000;
player.attackDamage = 20;
player.maxSpeed = 20;

// These are our goals. Notice we save them in variables!
var spawnMunchkinsGoal = game.addManualGoal(“Let 6 munchkins spawn.”);
var dontTouchGoal = game.addManualGoal(“Don’t attack munchkins.”);
var defeatScoutsGoal = game.addManualGoal(“Defeat 6 scouts.”);
// game properties used to count new and defeated ogres.
game.spawnedMunchkins = 0;
game.defeatedScouts = 0;
ui.track(game, “spawnedMunchkins”);
ui.track(game, “defeatedScouts”);

function onSpawn(event) {
game.spawnedMunchkins += 1;
}

function onDefeat(event) {
var unit = event.target;
if (unit.type == “scout”) {
game.defeatedScouts += 1;
}
if (unit.type == “munchkin”) {
// dontTouchGoal is failed if a munchkin is defeated.
game.setGoalState(dontTouchGoal, false);
player.say(“Oops.”);
}
}

function checkGoals() {
// If game.defeatedScouts is greater than 5:
if (game.defeatedScouts > 5) {
// Set defeatScoutsGoal state as successful…
game.setGoalState(defeatScoutsGoal, true);
}
// If game.spawnedMunchkins is greater than 5:
if (game.spawnMunchkins > 5) {
// Set spawnMunchkinsGoal state as successful…
game.setGoalState(spawnMunchkinsGoal, True);
}
// If both other goals are completed.
if (spawnMunchkinsGoal.success) {
if (defeatScoutsGoal.success) {
// Set dontTouchGoal state as successful…
game.setGoalState(dontTouchGoal, true);
}
}
}

game.setActionFor(“munchkin”, “spawn”, onSpawn);
game.setActionFor(“munchkin”, “defeat”, onDefeat);
game.setActionFor(“scout”, “defeat”, onDefeat);

while (true) {
checkGoals();
}Preformatted text

Click on this button:
2020-12-10_13-46-03
Then type your code in here:
2020-12-10_13-46-46

So this code:

while True:
hero.say(“hi”)

Should be:

while True:
    hero.say("hi")

You are supposed to replace the line “type or paste code here” with your code.

I just did what you said, I used pasye. When I pasted the code it looked proper. Then I posted it and it took away the indentations.

Do you see the difference between these two codes? One is formatted and the other is not.

I do see the difference and that is what it looks like when I paste it.

Screenshot 2020-12-10 at 5.35.52 PM

Writing the above like that ends up looking like this:

I see the difference. See my screenshoot:

// Use manual goals to specify which ogres to defeat.

function createGenerator(spawnType, x, y, spawnAI) {
    var generator = game.spawnXY("generator", x, y);
    generator.spawnType = spawnType;
    generator.spawnAI = spawnAI;
}

// Scouts are aggressive and munchkins are just walking.
createGenerator("scout", 12, 12, "AttacksNearest");
createGenerator("scout", 68, 56, "AttacksNearest");
createGenerator("munchkin", 12, 56, "Scampers");
createGenerator("munchkin", 68, 12, "Scampers");

var player = game.spawnPlayerXY("duelist", 40, 34);
player.maxHealth = 1000;
player.attackDamage = 20;
player.maxSpeed = 20;

// These are our goals. Notice we save them in variables!
var spawnMunchkinsGoal = game.addManualGoal("Let 6 munchkins spawn.");
var dontTouchGoal = game.addManualGoal("Don't attack munchkins.");
var defeatScoutsGoal = game.addManualGoal("Defeat 6 scouts.");
// game properties used to count new and defeated ogres.
game.spawnedMunchkins = 0;
game.defeatedScouts = 0;
ui.track(game, "spawnedMunchkins");
ui.track(game, "defeatedScouts");

function onSpawn(event) {
    game.spawnedMunchkins += 1;
}

function onDefeat(event) {
    var unit = event.target;
    if (unit.type == "scout") {
        game.defeatedScouts += 1;
    }
    if (unit.type == "munchkin") {
        // dontTouchGoal is failed if a munchkin is defeated.
        game.setGoalState(dontTouchGoal, false);
        player.say("Oops.");
    }
}

function checkGoals() {
    // If game.defeatedScouts is greater than 5:
    if (game.defeatedScouts > 5) {
        // Set defeatScoutsGoal state as successful..
        game.setGoalState(defeatScoutsGoal, true);
    }
    // If game.spawnedMunchkins is greater than 5:
    if (game.spawnMunchkins > 5) {
        // Set spawnMunchkinsGoal state as successful..
        game.setGoalState(spawnMunchkinsGoal, True);
    }
    // If both other goals are completed.
    if (spawnMunchkinsGoal.success) {
        if (defeatScoutsGoal.success) {
            // Set dontTouchGoal state as successful..
            game.setGoalState(dontTouchGoal, true);
        }
    }
}

game.setActionFor("munchkin", "spawn", onSpawn);
game.setActionFor("munchkin", "defeat", onDefeat);
game.setActionFor("scout", "defeat", onDefeat);

while (true) {
    checkGoals();
}

1 Like

Thank you! I finally was able to post it properly. Can you look at it and help me? I have been stuck here for two days. They do not even have the answers this level online like some of the other levels if you can’t figure it out.

1 Like

When you play the level, only click on the scouts. Do not click on the munchkins.

That’s what I do. I get over 6 scouts on my board but it just keeps going:

The screenshot shows that I killed 7 there.

I don’t see what is wrong.

@jdad, I found it!
Replace true with True, it needs to be capitalized.

I don’t get it. I kill more than 7 scouts. I do not attack the scouts, it says ouch if I do so I know I didn’t. Thank you for trying to help me!

I replaced the True with true and it still will not work

You are supposed to replace true with True.