Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

How to check if a object exists then destroy it

How do I destroy the fence when the player steps away from the x mark? Heres my code

`game.addManualGoal("Survive the night");
game.addSurviveGoal();
var player = game.spawnHeroXY(41, 5);
var forest1 = game.spawnXY("forest", 52, 5);
var forest2 = game.spawnXY("forest", 60, 5);
var forest3 = game.spawnXY("forest", 68, 5);
var forest4 = game.spawnXY("forest", 76, 5);
var forest5 = game.spawnXY("forest", 28, 5);
var forest6 = game.spawnXY("forest", 20, 5);
var forest7 = game.spawnXY("forest", 12, 5);
var forest8 = game.spawnXY("forest", 4, 5);
var forest9 = game.spawnXY("forest", 76, 13);
var forest10 = game.spawnXY("forest", 76, 21);
var forest11 = game.spawnXY("forest", 76, 29);
var forest12 = game.spawnXY("forest", 76, 37);
var forest13 = game.spawnXY("forest", 76, 45);
var forest14 = game.spawnXY("forest", 76, 53);
var forest15 = game.spawnXY("forest", 76, 61);
var forest16 = game.spawnXY("forest", 4, 13);
var forest17 = game.spawnXY("forest", 4, 21);
var forest18 = game.spawnXY("forest", 4, 29);
var forest19 = game.spawnXY("forest", 4, 37);
var forest20 = game.spawnXY("forest", 4, 45);
var forest21 = game.spawnXY("forest", 4, 53);
var forest22 = game.spawnXY("forest", 4, 61);
var forest23 = game.spawnXY("forest", 32, 20);
var forest24 = game.spawnXY("forest", 48, 20);
var forest25 = game.spawnXY("forest", 40, 20);
var xmark1 = game.spawnXY("x-mark-bones", 35, 12);
var xmark2 = game.spawnXY("x-mark-bones", 45, 12);
function checkProximity() {
    if(xmark1.distanceTo(player) <= 1 && !fence1) {
        var fence1 = game.spawnXY("fence", 30, 11);
    }
    else if(xmark2.distanceTo(player) <= 1 && !fence2) {
        var fence2 = game.spawnXY("fence", 50, 11);
    }
    else if(xmark1.distanceTo(player) > 2 && fence1) {
        fence1.destroy();
    }
    else if(xmark2.distanceTo(player) > 1 && fence2) {
        fence2.destroy();
    }

}

while (true) {
   checkProximity(); 
}`

What level is this for? So I can test your code.
Thanks,
:lion: :lion: :lion:

Game dev 3 final project

As far as I’m aware it’s not open to me, I opened it anyway, but I can’t test it because I can’t change the programming language. Can anyone remember the thing you put at the end of the URL to make it javascript? something like andlanguage=“javascript”? I’m not sure.
Sorry, I’ll help you as soon as I can.
:lion: :lion: :lion:

This is the school version of codecombat that I am using so

codecombat.com/play/level/level-name?codeLanguage=javascript?