position dynamically created layer??

admin

Administrator
Staff member
Hi,

could anybody help me out on the following:

I'm trying to make a small game in javascript. Within this game I have a function that creates a layer when a key is pressed, and after that I want the layer to move upwards on the page. However I'm having problems addressing the newly created layer.

What I have is:

var layerCount = 0;
.....
if (key == 32) { // spacebar pressed
createLayer();
}

function createLayer() {
myID = "bullet" + layerCount;
bulletLayer = "<DIV ID='myID' style='position:absolute; LEFT:" + shipPos + "; TOP:200; WIDTH:10; HEIGHT:10; VISIBILITY: VISIBLE; Z-INDEX: 10'>";
bulletLayer += "<Img Src='http://www.webdeveloper.com/forum/archive/index.php/ship.gif' width='10' height='10'></DIV>";

document.body.insertAdjacentHTML("BeforeEnd", bulletLayer);
shoot(myID, 50);
layerCount++;
}

function shoot(shootID, steps) {
activeLayer = shootID;
bulletPos-=4;
document.all["bullet0"].style.top = bulletPos;
steps--;
newsteps = steps;
document.myForm.bulletY.value = bulletPos;
if (steps > 0) {
setTimeout ("shoot(activeLayer, newsteps);",1);
}
}


When I execute this, I get an error saying:
"document.all["bullet0"].style" is null or not an object.

Ofcourse, ['bullet0"] should be replaced by the variable "shootID" somehow, but I don't know the exact syntax for that. That's why I just put bullet0 in there to test if the positioning works in the first place.

However, it does not work.....

Can anybody please tell me what I'm doing wrong here?

(My code probably looks a bit messy, 'cause this is the first tie I'm trying to do a bit of javascripting.)

Thanks,

ArjenG
 
Back
Top