This content is protected, please login and enroll in the course to view this content!
4 thoughts on “Take Objects from Room”
agent1red
Just notice you replied to a similar problem I reported earlier with undefined obj.name
here is my code
var app = {};
window.onload = function () {
var player = { hp: 100 };
var sword = { str: 20, name: “fire sword”, location: 2, aka: [‘sword’] };
var dagger = { str: 10, name: “dagger”, location: 0, aka:[‘knife’] };
var coin = { str: 0, name: “coin”, location: 0, aka: [‘money’, ‘loot’] };
var fish = { str: 0, name: “dead fish”, location: 0, aka: [‘fish’,’trout’] };
var objects = [sword, dagger, coin, fish];
app.objects = objects;
var entrance = {id:1, n: 2, s: 0, e: 0, w: 0, name: “Front Gate”, desc: ‘You are at the front gate. There are iron bars rusting on the windows.’ }
var greatHall = { id: 2, n: 0, s: 1, e: 0, w: 0, name: “Great Hall”, desc: ‘This is the Great Hall.’ }
var rooms = [entrance, greatHall];
app.rooms = rooms;
player.inventory = [];
player.location = 1;
app.player = player;
app.where = function () {
var room = this.rooms[this.player.location – 1];
document.getElementById(‘outputText’).append(“You are at the “+room.name + “\n”);
this.showObjects();
}
app.look = function () {
var room = this.rooms[this.player.location – 1];
document.getElementById(‘outputText’).append(room.desc + “\n”);
}
app.pickUp = function (item) {
var found = false;
for (var i = 0; 1 < app.objects.length; i++) {
var obj = app.objects[i];
if (obj.name == item) {
app.player.inventory.push(obj);
}
for (var j = 0; j < obj.aka.length; j++) {
if (obj.aka[j] == item) {
if (obj.location == this.player.location) {
found = true;
app.player.inventory.push(obj);
this.say(obj.name + " taken!")
}
}
}
}
if (found == false) {
this.say("There is no " + item + " here");
}
}
app.clicked = function () {
var text = document.getElementById('commandText').value;
var words = text.split(" ");
var verb = words[0];
var object = words[1];
switch (verb) {
case "inventory": this.showInventory();
break;
case "move":
case "walk":
case "go":
case "run":
this.move(object);
break;
case "take":
this.pickUp(object);
break;
case "drop":
this.drop(object);
break;
case "where":
this.where();
break;
}
app.move = function (dir) {
var room = this.rooms[this.player.location – 1];
switch (dir) {
case "north":
if (room.n != 0) {
this.player.location = room.n;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
case "south":
if (room.s != 0) {
this.player.location = room.s;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
case "east":
if (room.e != 0) {
this.player.location = room.e;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
case "west":
if (room.w != 0) {
this.player.location = room.w;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
}
}
app.showInventory = function () {
var len = app.player.inventory.length;
var inString = "\n You are carrying "
if (len == 0) {
inString += "nothing";
}
for (var i = 0; i < len; i++) {
inString += app.player.inventory[i].name + " ,";
}
inString += "\n";
document.getElementById('outputText').append(inString);
}
app.showObjects = function () {
for (var i = 0; 1 < app.objects.length; i++) {
var obj = app.objects[i];
if (obj.location == this.player.location) {
this.say("there is a " + obj.name + " here");
}
}
}
app.say = function (text) {
document.getElementById('outputText').append(text + "\n");
}
}
I am unable to move forward. It’s late for me but I usually troubelshoot to find the issue. It seems as though this is an issue with the loop where ‘name’ property is no getting defined?
disregard. Last-minute troubleshooting. I am using Visual Studio community 2019 and went into debug mode. The reason why I was getting an error is because I had a syntax error in my for loop.
for (var i = 0; 1 < app.objects.length; i++) {
var obj = app.objects[i];
if (obj.name == item) {
app.player.inventory.push(obj);
}
Should be for (var i = 0; i < app.objects.length; i++).
I had placed a number '1' for the variable ' i 'after the declaration thus throwing off the array causing the property 'name' to go undefined.
Often when I am coding at night, I run into a problem like that and spend an hour trying to figure it out. Then I find the answer first thing in the morning.
Just notice you replied to a similar problem I reported earlier with undefined obj.name
here is my code
var app = {};
window.onload = function () {
var player = { hp: 100 };
var sword = { str: 20, name: “fire sword”, location: 2, aka: [‘sword’] };
var dagger = { str: 10, name: “dagger”, location: 0, aka:[‘knife’] };
var coin = { str: 0, name: “coin”, location: 0, aka: [‘money’, ‘loot’] };
var fish = { str: 0, name: “dead fish”, location: 0, aka: [‘fish’,’trout’] };
var objects = [sword, dagger, coin, fish];
app.objects = objects;
var entrance = {id:1, n: 2, s: 0, e: 0, w: 0, name: “Front Gate”, desc: ‘You are at the front gate. There are iron bars rusting on the windows.’ }
var greatHall = { id: 2, n: 0, s: 1, e: 0, w: 0, name: “Great Hall”, desc: ‘This is the Great Hall.’ }
var rooms = [entrance, greatHall];
app.rooms = rooms;
player.inventory = [];
player.location = 1;
app.player = player;
app.where = function () {
var room = this.rooms[this.player.location – 1];
document.getElementById(‘outputText’).append(“You are at the “+room.name + “\n”);
this.showObjects();
}
app.look = function () {
var room = this.rooms[this.player.location – 1];
document.getElementById(‘outputText’).append(room.desc + “\n”);
}
app.pickUp = function (item) {
var found = false;
for (var i = 0; 1 < app.objects.length; i++) {
var obj = app.objects[i];
if (obj.name == item) {
app.player.inventory.push(obj);
}
for (var j = 0; j < obj.aka.length; j++) {
if (obj.aka[j] == item) {
if (obj.location == this.player.location) {
found = true;
app.player.inventory.push(obj);
this.say(obj.name + " taken!")
}
}
}
}
if (found == false) {
this.say("There is no " + item + " here");
}
}
app.clicked = function () {
var text = document.getElementById('commandText').value;
var words = text.split(" ");
var verb = words[0];
var object = words[1];
switch (verb) {
case "inventory": this.showInventory();
break;
case "move":
case "walk":
case "go":
case "run":
this.move(object);
break;
case "take":
this.pickUp(object);
break;
case "drop":
this.drop(object);
break;
case "where":
this.where();
break;
case "look":
this.look();
break;
}
//document.getElementById('outputText').append(text + "\n");
}
app.move = function (dir) {
var room = this.rooms[this.player.location – 1];
switch (dir) {
case "north":
if (room.n != 0) {
this.player.location = room.n;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
case "south":
if (room.s != 0) {
this.player.location = room.s;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
case "east":
if (room.e != 0) {
this.player.location = room.e;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
case "west":
if (room.w != 0) {
this.player.location = room.w;
this.where();
this.showObjects();
} else {
this.say("there is no door in that direction");
}
break;
}
}
app.showInventory = function () {
var len = app.player.inventory.length;
var inString = "\n You are carrying "
if (len == 0) {
inString += "nothing";
}
for (var i = 0; i < len; i++) {
inString += app.player.inventory[i].name + " ,";
}
inString += "\n";
document.getElementById('outputText').append(inString);
}
app.showObjects = function () {
for (var i = 0; 1 < app.objects.length; i++) {
var obj = app.objects[i];
if (obj.location == this.player.location) {
this.say("there is a " + obj.name + " here");
}
}
}
app.say = function (text) {
document.getElementById('outputText').append(text + "\n");
}
}
I am unable to move forward. It’s late for me but I usually troubelshoot to find the issue. It seems as though this is an issue with the loop where ‘name’ property is no getting defined?
disregard. Last-minute troubleshooting. I am using Visual Studio community 2019 and went into debug mode. The reason why I was getting an error is because I had a syntax error in my for loop.
for (var i = 0; 1 < app.objects.length; i++) {
var obj = app.objects[i];
if (obj.name == item) {
app.player.inventory.push(obj);
}
Should be for (var i = 0; i < app.objects.length; i++).
I had placed a number '1' for the variable ' i 'after the declaration thus throwing off the array causing the property 'name' to go undefined.
Go figure.
Often when I am coding at night, I run into a problem like that and spend an hour trying to figure it out. Then I find the answer first thing in the morning.