[web] Javascript dynamic objects

Started by
3 comments, last by kylecrass 18 years, 9 months ago
Hello, I am writing a game in &#106avascript, 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 &#106avascript. I am aiming for compatibility with netscape and ie browsers.
Joel Bruce, Founder of JAB IT and Yoale's Site
Vivid War Game Manual
Advertisement
I think there are two issues here:

1. How to keep track of objects in memory in &#106avascript

and

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

The first part is easily done using &#106avascript objects and arrays. In &#106avascript, an array can be created using "new Array()".<br><br>This then has various methods for adding new elements and removing old &#111;nes (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 &#111;ne 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 &#111;n 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 &#106avascript 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 :)
Joel Bruce, Founder of JAB IT and Yoale's Site
Vivid War Game Manual
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
Hmm, I must have been misinformed, thanks again :)
Joel Bruce, Founder of JAB IT and Yoale's Site
Vivid War Game Manual

This topic is closed to new replies.

Advertisement