Problems With Goals


#1

I have the code I want in my game done. I have no problems running it besides getting the goals to trigger so the game completes. I don’t know which goal won’t trigger and why. Is there any way for anyone to help me?
There are a few functions that I commented out and there are some things I added into the functions (such as the game.munchkinsSpawned) as a replacement for constantly spawning enemies and to make the set number of defeats to 14. If you can help, thank you. Also note: This is for the Game Development 3 final project.
Here is my code:

// Spawn a hero with spawnHeroXY()
var hero = game.spawnHeroXY('samurai', 16, 33);
hero.maxSpeed = 13;
hero.maxHealth = 250;
hero.attackDamage = 37.5;
// Add at least one goal!
game.addSurviveGoal(); 
var scoutGoal = game.addManualGoal("Defeat 7 Scouts!");
var munchGoal = game.addManualGoal("Defeat 7 Munchkins!");
game.addManualGoal("Collect the gems!");
ui.track(game, "scoutsDefeated");
ui.track(game, "munchkinsDefeated");
game.addCollectGoal();
game.addDefeatGoal(14);
// Forests
game.spawnXY("forest", 4, 64);
game.spawnXY("forest", 12, 64);
game.spawnXY("forest", 20, 64);
game.spawnXY("forest", 28, 64);
game.spawnXY("forest", 36, 64);
game.spawnXY("forest", 44, 64);
game.spawnXY("forest", 52, 64);
game.spawnXY("forest", 60, 64);
game.spawnXY("forest", 68, 64);
game.spawnXY("forest", 76, 64);
game.spawnXY("forest", -4, 4);
game.spawnXY("forest", 4, 4);
game.spawnXY("forest", 12, 4);
game.spawnXY("forest", 20, 4);
game.spawnXY("forest", 28, 4);
game.spawnXY("forest", 36, 4);
game.spawnXY("forest", 44, 4);
game.spawnXY("forest", 52, 4);
game.spawnXY("forest", 60, 4);
game.spawnXY("forest", 68, 4);
game.spawnXY("forest", 76, 4);
game.spawnXY("forest", 4, 12);
game.spawnXY("forest", 4, 20);
game.spawnXY("forest", 4, 28);
game.spawnXY("forest", 4, 36);
game.spawnXY("forest", 4, 44); 
game.spawnXY("forest", 4, 52);
game.spawnXY("forest", 4, 60); 
game.spawnXY("forest", 76, 12);
game.spawnXY("forest", 76, 20);
game.spawnXY("forest", 76, 28);
game.spawnXY("forest", 76, 36);
game.spawnXY("forest", 76, 44); 
game.spawnXY("forest", 76, 52);
game.spawnXY("forest", 76, 60);
// Health items
game.spawnXY("potion-medium", 13, 55);
game.spawnXY("potion-medium", 13, 12);
game.spawnXY("potion-medium", 67, 12);
game.spawnXY("potion-medium", 67, 55);
// Functions
game.scoutSpawnTime = 0;
game.munchkinSpawnTime = 0;
game.scoutsDefeated = 0;
game.munchkinsDefeated = 0;
game.spawnInterval = 6;
game.munchkinsSpawned = 0;
game.scoutsSpawned = 0;
function spawnRandomGem() {
    var x = game.randomInteger(10, 70);
    var y = game.randomInteger(10, 58);
    var gem = game.spawnXY("gem", x, y);
    gem.dirX = game.randomInteger(-1, 1);
    gem.dirY = game.randomInteger(-1, 1);
    gem.scale = 1.5;
}
var gemSpeed = 0.6;
function onUpdate(event) {
    var item = event.target;
    var diffX = item.dirX * gemSpeed;
    var diffY = item.dirY * gemSpeed;
    item.pos.x += diffX;
    item.pos.y += diffY;
    if (item.pos.x > 70 || item.pos.x < 10) {
        item.dirX *= -1;
    }
    if (item.pos.y > 58 || item.pos.y < 10) {
        item.dirY *= -1;
    }
}
spawnRandomGem();
spawnRandomGem();
spawnRandomGem();
spawnRandomGem();
spawnRandomGem();
spawnRandomGem();
game.setActionFor("gem", "update", onUpdate);
function spawnScout(type) {
	var scot = game.spawnXY(type, 40, 15);
	scot.behavior = "AttacksNearest";
	scot.maxSpeed = 6;
	game.scoutsSpawned += 1;
}
function spawnMunchkin(type) {
	var munch = game.spawnXY(type, 40, 55);
	munch.behavior = "AttacksNearest";
	munch.maxSpeed = 6;
	game.munchkinsSpawned += 1;
}
function onMunchkinDefeat(event) {
	game.munchkinsDefeated += 1;
}
game.setActionFor("munchkin", "defeat", onMunchkinDefeat);
function onScoutDefeat(event) {
	game.scoutsDefeated += 1;
}
game.setActionFor("scout", "defeat", onScoutDefeat);
function checkTimers() {
	if (game.time >= game.munchkinSpawnTime && game.munchkinsSpawned < 7) {
		spawnMunchkin("munchkin");
		game.munchkinSpawnTime += game.spawnInterval;
	}
	if (game.time >= game.scoutSpawnTime && game.scoutsSpawned < 7) {
		spawnScout("scout");
		game.scoutSpawnTime += game.spawnInterval;
	}
}
//function checkGoals() {
//	if (game.munchkinsDefeated >= 7) {
//		game.setGoalState(munchGoal, true);
//	}
//	if (game.scoutsDefeated >= 7) {
//		game.setGoalState(scoutGoal, true);
//	}
//}
function onUpdateGame(event) {
	checkTimers();
}
game.on("update", onUpdateGame);

#2

Hi, please could you format your code properly by ctrl / cmd copying your code and pressing the preformatted code button and ctrl / cmd pasting your code into the space that says ‘type or paste code here’ otherwise you can’t see your code.
Thank you :grin:


#4

I fixed it. If you could help me asap that would be cool and I really appreciate it.


#5

Im afraid not, I don’t do javascript, but I think maybe @MunkeyShynes or @Chaboi_3000 might know.