Sign in to follow this  
kylecrass

[web] Javascript dynamic objects

Recommended Posts

Hello, I am writing a game in javascript, and I was wondering how to go about creating an array of dynamic objects. Basically, I want to be able to fire a missile every time a person presses a space bar. Now I would love to make this missile a seperste entity and not tied to the original person, so that way a person can fire multiple missiles w/o having to keep track of each missile, but have each missile be like a spawned character. I can do this in vb 6.0, and was wondering if there was a way to do this in javascript. I am aiming for compatibility with netscape and ie browsers.

Share this post


Link to post
Share on other sites
I think there are two issues here:

1. How to keep track of objects in memory in javascript

and

2. How to represent this with a visual object in the browser.

The first part is easily done using javascript objects and arrays. In javascript, an array can be created using "new Array()".

This then has various methods for adding new elements and removing old ones (look up the documentation).

You can create any object you like using a function as a constructor. You can do this with any function.

Although conventional inheritance is impossible, you can call one constructor from another, which has a similar effect. Again, read documentation as to how to do this.

--

The second part is also pretty easy.

Your best bet is to create some "container" element in the page (div is usually what's used). You can then dynamically add elements to this using appendChild:



var container = document.getElementById("mycontainer");
var element = document.createElement("div"); // Or whatever
// TODO: set whatever attributes on element you require; add child nodes as necessary
container.appendChild(element);



You can set the child element, element as a property of your game object to allow it to easily be referrer to later (when you want to move the missile, or it is destroyed and needs to be removed). This is an alternative to assigning it an ID, which is unnecessarily if you have its identity in a property of a javascript object.

Mark

Share this post


Link to post
Share on other sites
Thanks for the help mark, that is a nice approach. If only it wasnt for the fact it can only be done in ie...as far as createelement i mean. :) As always your help is very welcome and, well, helpful :)

Share this post


Link to post
Share on other sites
Total nonsense about IE.

CreateElement and the other DOM methods are supported in all browsers which support DOM1. As far as I am aware this is

Mozilla (all versions since, never)
Opera 7 (previous versions had partial support)
Konqueror
Safari
IE (5.5 +)

Please tell me if I've missed out any other popular browsers... that is basically, everyone.

Mark

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this