设置变量时游戏代码中断 [英] Game code breaking when setting variable

查看:24
本文介绍了设置变量时游戏代码中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个游戏代码看起来不错,但它不起作用:

This game code seems fine, but it's not working:

//Get canvas and 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 is brown\?', 'duh', 'foo', [], 'animale', 'wolf', []);
alert(characters.Xantar.weapon + '   ' + characters[0].type);

//Activate mainloop and get value for pausing purposes
var mainloopInterval = setInterval(mainloop, 5);
//Main game loop
function mainloop(){

}

程序似乎停在:

characters.charList = [];

我只是没有看到这里的问题.这是相关的 HTML:

I simply don't see the problem here. Here is the relevant HTML:

<canvas id='canvas' style='border:1px solid black;'>Your browser does not support canvas. Please update your browser.</canvas>

注意:我在 JSFiddle 上运行它.如果有人能在这里帮助我,我将不胜感激.这是链接:https://jsfiddle.net/q6vdph1p/14/.

Note: I am running this on JSFiddle. If anyone could help me here, I would be really grateful. Here is the link: https://jsfiddle.net/q6vdph1p/14/.

推荐答案

如前所述,您需要先将一个对象分配给 characters 变量,然后才能访问其中的属性.

As noted before, you need to assign an object to your characters variable before you'll be able to access properties in it.

只需用 var 字符声明; 将分配给它 undefined 的值并尝试访问 undefined.charList 并分配一个空数组它会失败.

Just declaring it with var characters; will assign it the value of undefined and trying to access undefined.charList and assign an empty array to it will fail.

一种更合适、抗故障且可读的声明方式是构建空数据结构,如下所示:

A more proper, fail resistant, and readable way of declaring it would be building the empty data structure, like so:

var characters = {
    charList: []
};

此外,使用array.push() 添加到数组的末尾比使用array.length 索引更安全,即

Also, adding to the end of an array is more safe using array.push() than indexing by array.length, i.e.

characters.charList.push(Name);

而不是

characters.charList[characters.charList.length] = Name;

这篇关于设置变量时游戏代码中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆