Jump to content

Array for tilemap.


madmenyo

Recommended Posts

Hey,

 

I am totally new to javascript and i want to create a interactive map for my PHP game. Obviously i will not be able to just code it instantly so i wanna do this step by step and first let java draw a map from an Array for me. Unfortunately but expected :D i can't even pull this off :D.

 

Firebug gives me (error: parentDiv is null) stated in the function addImage this function gets called by another function renderTileMap div parent stated in this function should feed the attribute. Mind, i rewritten the code from another another file that has a simple tile map and lets a player move a character. I have looked thorugh the whole code for things i forgot to include but can't find anything. Offcourse i did include the open.jp and wall.jpg.

 

Am i close? Or am i totally off with this and should i start javascript with something more easy?

 

-EDIT-Error: parentDiv is null

Source File: file:///C:/xampp/htdocs/projects/java/myfirstmap/tilemap.html

Line: 31

 

<html>
<title>My first tile map</title>
<head>

<script language="JavaScript">

function addImage(parentDiv, elementName, filename, x, y, height, width)
{
   elementName = document.createElement("img");
   elementName.src = filename;
   elementName.style.position = 'absolute';
   elementName.style.left = x + 'px';
   elementName.style.top = y + 'px';
   elementName.style.width = width + 'px';
   elementName.style.height = height + 'px';
   elementName.style.right = x + width + 'px';
   elementName.style.bottom = y + height + 'px';
   parentDiv.appendChild(elementName);
}

tileMap = [
[1 ,1 ,1 ,1 ,1],
[1 ,0 ,0 ,0 ,1],
[1, 0, 0, 0, 1],
[1, 0, 0, 0, 1],
[1, 1 ,1 ,1 ,1],
];

images = new Array();
images[0] = "open.jpg";
images[1] = "wall.jpg";


function renderTileMap(tileMap, tiles, width, height, tileWidth, tileHeight)
{
   var divParent = document.getElementById('ParentImage');
   for(y = 0; y < height; y++) //Loop Height
      for(x = 0; x < width; x++) //Loop Width
      {            
         addImage(divParent, tiles[y * width + x], images[tileMap[y][x]], 
            x * tileWidth, y * tileHeight, tileWidth, tileHeight);
      }
}
   
window.onload = renderTileMap(tileMap, images, 5, 5, 32, 32);

</SCRIPT>
</HEAD>
<BODY>
<DIV ID="ParentImage"></DIV>
</BODY>
</HTML>

Link to comment
Share on other sites

It seems i have solved it.

window.onload = renderTileMap(tileMap, images, 5, 5, 32, 32); //last line of the script

If i delete this and put a onload in the body element it DOES show the map i have drawn.

<BODY onLoad="renderTileMap(tileMap,images,5,5,32,32)">

 

Whats the difference between the 2? why won't my images get loaded with the javascript and they do if i put the function in the body?

 

Does this have consequences in the future for when i want to make a character move or have the window only show 3*3 images? Or for anything else related to further build with this script?

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.