[SOLVED]Disintegration Arrow Game dev 2 Java script

Hello! One of the goals is to get 22/22 arrows shot at the munchkins, it was going well but for some reason it randomly just stopped. The next goal was to get at least 50 munchkins, but it wouldn’t let me continue. here’s my code, thanks a lot!! >:)

// Destroy at least 50 defeated ogres.

// This spawns and configures an archer.
function spawnArcher(x, y) {
    var archer = game.spawnXY("archer", x, y);
    archer.behavior = "Defends";
    archer.attackDamage = 1000000000000;
}

// This spawns and configures an ogre.
function spawnMunchkin(x, y) {
    var ogre = game.spawnXY("munchkin", x, y);
    ogre.behavior = "AttacksNearest";
}

// Spawns some archers in a row.
function spawnArcherWall() {
    spawnArcher(30, 12);
    spawnArcher(30, 23);
    spawnArcher(30, 34);
    spawnArcher(30, 45);
    spawnArcher(30, 56);
}

// Spawns an ogre wave with a random offset for variety.
function spawnOgreWave() {
    var offset = game.randomInteger(-6, 6);
    spawnMunchkin(80, 16 + offset);
    spawnMunchkin(80, 22 + offset);
    spawnMunchkin(80, 28 + offset);
    spawnMunchkin(80, 34 + offset);
    spawnMunchkin(80, 40 + offset);
    spawnMunchkin(80, 46 + offset);
    spawnMunchkin(80, 52 + offset);
}

function onDefeat(event) {
    var unit = event.target;
    // Increase the game.defeated counter by 1.
    game.defeated += 1;
    // Use unit.destroy() to destroy it.
    unit.destroy();
}

// Set "munchkin"s "defeat" event handlers to onDefeat.
game.setActionFor("munchkin", "defeat", onDefeat);

game.defeated = 0;
game.spawnTime = 0;
// Add a manual goal.
var goal = game.addManualGoal("Defeat 77 ogres.");
ui.track(game, "defeated");

function checkSpawnTimer() {
    if (game.time > game.spawnTime) {
        spawnOgreWave();
        game.spawnTime += 1;
    }
}

function checkGoal() {
    // If the game.defeated counter is greater than 77:
    if (game.defeated > 7) {
     game.setGoalState(goal, true);   
    }
        // Set the goal as successfully completed.
        
}

spawnArcherWall();
while (true) {
    checkSpawnTimer();
    checkGoal();
}

Thanks!! -Amira

Look here, I am pretty sure you didn’t mean for it to be 7. I think you want it to be 77.

1 Like

O wait it worked! Never mind, thanks sooooooo much!!!

1 Like

Hello! sorry to bother you once again, but Just a quick question. What method would I do if I were to assign an event handler on the player’s “collect” event? I keep doing the normal player.on(“collect”, onCollect); but for some reason it isn’t working for me. any tips?

player.on("collect", onCollect) should work. Do you have a function onCollect()?

1 Like

I believe that you have no onCollect() function, or you haven’t properly defined that function.

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