I am working on a Star Wars turn based click game. The played is meant to pick 1 of 4 of the available characters that randomly is picked from an array of 6 characters.
I have a couple questions: 1 is this image link going to do anything is I am able to target it?
2 how do I turn Characters();
into a usable array
function Character(name, health, attackPoints, counterAttackPoints, img) {
this.name = name;
this.health = health;
this.attackPoints = attackPoints;
this.counterAttackPoints = counterAttackPoints;
this.img = img;
console.log(name, attackPoints, counterAttackPoints);
}
var char1 = new Character('Han Solo', 100, 13, 10, '../assets/images/Han_Solo_depicted_in_promotional_image_for_Star_Wars_(1977).jpg');
var char2 = new Character('Chewbacca', 300, 10, 20, '../assets/images/Chewbacca-2-.jpg');
var char3 = new Character('Luke Skywalker', 140, 15, 12, '../assets/images/Luke_Skywalker.png');
var char4 = new Character('Darth Maul', 120, 12, 13, '../assets/images/Darth_Maul.png');
var char5 = new Character('Darth Vader', 175, 20, 6, '../assets/images/Han_Solo_depicted_in_promotional_image_for_Star_Wars_(1977).jpg');
var char6 = new Character('General Grievous', 120, 14, 10, '../assets/images/Han_Solo_depicted_in_promotional_image_for_Star_Wars_(1977).jpg');
if i understand your question correctly, you are trying to turn an object into an array of some kind.
you can use the Object.values()
const char1 = {
name: 'Some Dude',
health: 100,
attackPoints: 13,
counterAttackPoints: 10,
img: '../some/url/to/your/asset.jpg'
};
console.log(Object.values(char1));
// Output will be => ["Some Dude", 100, 13, 10, "../some/url/to/your/asset.jpg"]
if you need to go deeper into the object like looping inside the object prototype, then you will need to opt for a for .. in
loop
Instead of
var somethingNew = new Character(...)
You could push the new character into an array:
var characters = [];
characters.push(new Character('Han Solo', 100, 13, 10, '../assets/images/Han_Solo_depicted_in_promotional_image_for_Star_Wars_(1977).jpg'));
characters.push(new Character('Chewbacca', 300, 10, 20, '../assets/images/Chewbacca-2-.jpg'));
// etc
console.log(characters.length); // 2!
console.log(characters[0].name); // Han Solo
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.