[web] Javascript dynamic objects
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.
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()".<br><br>This then has various methods for adding new elements and removing old ones (look up the documentation).<br><br>You can create any object you like using a function as a constructor. You can do this with any function.<br><br>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.<br><br>--<br><br>The second part is also pretty easy.<br><br>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:<br><br><pre><br><br>var container = document.getElementById("mycontainer");<br>var element = document.createElement("div"); // Or whatever<br>// TODO: set whatever attributes on element you require; add child nodes as necessary<br>container.appendChild(element);<br><br></pre><br><br>You can set the child element, <em>element</em> 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.<br><br>Mark
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()".<br><br>This then has various methods for adding new elements and removing old ones (look up the documentation).<br><br>You can create any object you like using a function as a constructor. You can do this with any function.<br><br>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.<br><br>--<br><br>The second part is also pretty easy.<br><br>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:<br><br><pre><br><br>var container = document.getElementById("mycontainer");<br>var element = document.createElement("div"); // Or whatever<br>// TODO: set whatever attributes on element you require; add child nodes as necessary<br>container.appendChild(element);<br><br></pre><br><br>You can set the child element, <em>element</em> 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.<br><br>Mark
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 :)
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement