javascript - Game code breaking when setting variable -
this game code seems fine, it's not working:
//get canvas , context var canvas = document.getelementbyid('canvas'); var ctx = canvas.getcontext('2d'); //capture keypresses var keys = []; document.onkeydown = function (e) {keys[e.keycode] = true;}; document.onkeyup = function (e) {keys[e.keycode] = false;}; //set game variables var characters; characters.charlist = []; var player = {}; //character constructor function character (name, strength, speed, intelligence, reflexes, age, height, weight, weapon, usesmagic, magictype, armor, hair, eyes, skin, clothes, species, type, accessories) { this.name = name; this.strength = strength; this.speed = speed; this.intelligence = intelligence; this.reflexes = reflexes; this.age = age; this.height = height; this.weight = weight; this.weapon = weapon; this.usesmagic = usesmagic; this.magictype = magictype; this.armor = armor; this.hair = hair; this.eyes = eyes; this.skin = skin; this.clothes = clothes; this.species = species; this.type = type; if (accessories) { this.accessories = accessories; } characters.charlist[characters.charlist.length] = name; } characters.xantar = new character('xantar', 1, 1, 1, 1, 1, 1, 1, 'sword', true, 'derp', [], 'hair brown\?', 'duh', 'foo', [], 'animale', 'wolf', []); alert(characters.xantar.weapon + ' ' + characters[0].type); //activate mainloop , value pausing purposes var mainloopinterval = setinterval(mainloop, 5); //main game loop function mainloop(){ }
the program seems stop at:
characters.charlist = [];
i don't see problem here. here relevant html:
<canvas id='canvas' style='border:1px solid black;'>your browser not support canvas. please update browser.</canvas>
note: running on jsfiddle. if me here, grateful. here link: https://jsfiddle.net/q6vdph1p/14/.
as noted before, need assign object characters
variable before you'll able access properties in it.
just declaring var characters;
assign value of undefined
, trying access undefined.charlist
, assign empty array fail.
a more proper, fail resistant, , readable way of declaring building empty data structure, so:
var characters = { charlist: [] };
also, adding end of array more safe using array.push()
indexing array.length
, i.e.
characters.charlist.push(name);
rather than
characters.charlist[characters.charlist.length] = name;
Comments
Post a Comment