gpt4 book ai didi

Javascript - 如何使用循环在对象内定义数组?

转载 作者:行者123 更新时间:2023-11-28 14:48:20 25 4
gpt4 key购买 nike

我正在尝试制作一款像素 Angular 色扮演游戏,但有点困惑。

我正在尝试做的事情:

var character = {
gender: male,
age: 18,
action: walking,
direction: left,
skin: 0,
eyes: aqua,
hairStyle: 0,
hairColor: blue,
hat: 'cap'
};

ctx.drawImage(characterIMG.body[character.gender][character.skinColor], 0, 0);
ctx.drawImage(characterIMG.eyes[character.gender][character, 0, 0);
ctx.drawImage(characterIMG.hair[style0], 0, 0);
if(character.hat != "none"){
ctx.drawImage(characterIMG.hat['cap'], 0, 0);
}

我将如何定义图像?

到目前为止,但我很困惑..

var characterIMG = [male, female]; //I want some separate variables to just write in, in case I need to add more images of the same type, then I can just write the "type" in the variable instead of changing a bunch of stuff :)

for(var i in characterIMG){
characterIMG[i] = {
skin: [],
eyes: [],
hair: [],
accessories: []
}
}

除非有更有效的方法来做到这一点?

谢谢!

--简而言之,我想这样做:

ctx.drawImage(characterIMG.body[character.gender][character.skinColor], 0, 0);
ctx.drawImage(characterIMG.eyes[character.gender][character], 0, 0);
ctx.drawImage(characterIMG.hair[style0], 0, 0);

..with characterIMG 包含所有相关图像的巨大数据库(此)

最佳答案

使用一个对象,然后每当您想要创建 Angular 色的新实例时就新建该对象。这样,每个 Angular 色都可以从主对象(通过原型(prototype))继承其属性,并成为游戏中的独立实体。

var Person = function(payload) {
this.name = payload.name;
this.age = payload.age;
this.gender = payload.gender;
};

Person.prototype.talk = function() {
return console.log('hello, I am ' + this.name);
}

var Person1 = new Person({
name : 'Amanda',
age : 18,
gender : 'Female',
});

var Person2 = new Person({
name : 'Sam',
age : 21,
gender : 'Make',
});

Person1.talk();
Person2.talk();

关于Javascript - 如何使用循环在对象内定义数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45521009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com