I come from a C#/Java background and I'm trying to go about learning how to make games in HTML5. I ran across a problem that has been boggling my mind, and although normally I would ask a friend, he's been MIA. I feel bad having created an account to ask the community, but I've been searching for the answer to no avail.
So the problem is this... I want to create a gameobject that can hold an instance of a tower. Also, I want to create an array list of towers as a sort of back-end repository. Essentially, I was hoping these would be external js files that I would be able to reference in my main game. I'm not sure if this is possible in js or how this would work.
What I attempted to do was... Create an external JS file and declare a tower object. This is where I got lost. I wanted to make another object that was simply a list of the first type, a towerList. In this external JS file, I was going to populate the list with a bunch of tower properties. Then, in the main file with the canvas element, I was going to iterate through the list and draw out the graphics. I have no idea how to go about this.
function Tower (type, texture, cost, damage, speed, range, health, Xpos, Ypos)
{
this.type = type;
this.texture = new Image();
this.texture.src = texture;
this.cost = 0;
this.damage = 0;
this.speed = 0;
this.range = 0;
this.health = 0;
this.Xposition = 0;
this.Yposition = 0;
this.target = false;
}
var TowerList = [Tower("Basic", "tower.png", 0, 0, 0, 0, 0, 0, 0), Tower(), Tower()];
I doubt I have this working correctly, but I want to be able to create instances of the towerlist in the main project, access it randomly like an array, and print its contents as I see fit. What's the best OO way to accomplish this in JS? I want to be able to later in the main code be able to assign a tower from the list as such
var tL = new TowerList();
var tower = new Tower();
tower = tL[0];
I just started this today, so I realize there's a lot of learning to be had. I think I need to redefine the function as a var else I can instantiate it (I'm sure I read that earlier). If anyone can help me out or point me in the direction to a resource that has examples that I might be able to learn from, I would very much appreciate it.