Connecting Tech Pros Worldwide Forums | Help | Site Map

Image does not move to set location

Newbie
 
Join Date: Apr 2007
Posts: 17
#1: Jun 13 '08
I'm just trying to move a few images around a page... The images already exist, and the following function simply assigns them an ID and sets their offsetTop/offsetLeft attributes. This works for other images, but these refuse to cooperate. The alert halfway in is an attempt to find the problem, but it always returns the right values. Once the function finishes, however, they're always totally off, and just line up horizontally at the top-left corner of the page. Note that all variables that should be defined elsewhere, are. No loose ends, as far as I can tell.
Expand|Select|Wrap|Line Numbers
  1. // Part of larger function...
  2. var numofscats = (Math.floor(5*Math.random())) + 3; // 3-8 weapons in-game
  3.     for (var i = 0; i < numofscats; i++) {
  4.         var weaponindex = (Math.floor(numofweapons*Math.random()));
  5.         var weaponx = (Math.floor(map_l*Math.random()))+map_x;
  6.         var weapony = (Math.floor(map_h*Math.random()))+map_y;
  7.         weapons_ingame[i] = new inGameWeapon(weaponindex, weaponx, weapony);
  8.         if (weapons_ingame[i]) {
  9.             alert("Weapon "+i+" exists at ("+weaponx+","+weapony+")");
  10.         }
  11.         var weapon = weapons[weaponindex];
  12.         var gamearea = document.getElementById("gamearea");
  13.         if (weapon.clss == 0 || weapon.clss == 1) {
  14.             gamearea.innerHTML += "<img src=\"cheddar/w_light.png\" id=\"weapon"+weaponindex+"\">";
  15.         } else if (weapon.clss == 5) {
  16.             gamearea.innerHTML += "<img src=\"cheddar/w_heavy.png\" id=\"weapon"+weaponindex+"\">";
  17.         } else {
  18.             gamearea.innerHTML += "<img src=\"cheddar/w_medium.png\" id=\"weapon"+weaponindex+"\">";
  19.         }
  20.         document.getElementById("weapon"+weaponindex).setAttribute("offsetTop", weapony+"px");
  21.         document.getElementById("weapon"+weaponindex).setAttribute("offsetLeft", weaponx+"px");
  22.  

acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2: Jun 14 '08

re: Image does not move to set location


Set style.top/left instead.
Newbie
 
Join Date: Apr 2007
Posts: 17
#3: Jun 16 '08

re: Image does not move to set location


Revised it to this (complete with style.top/style.left), but without progress... it still doesn't seem to work.
Expand|Select|Wrap|Line Numbers
  1.     var numofscats = (Math.floor(5*Math.random())) + 3; // 3-8 weapons in-game
  2.     for (var i = 0; i < numofscats; i++) {
  3.         var weaponindex = (Math.floor(numofweapons*Math.random()));
  4.         var weaponx = (Math.floor(map_l*Math.random()))+map_x;
  5.         var weapony = (Math.floor(map_h*Math.random()))+map_y;
  6.         weapons_ingame[i] = new inGameWeapon(weaponindex, weaponx, weapony);
  7.         //if (weapons_ingame[i]) {
  8.         //    alert("Weapon "+i+" exists at ("+weaponx+","+weapony+")");
  9.         //}
  10.         var weapon = weapons[weaponindex];
  11.         var weaponimg = document.getElementById("weapon"+(i+1));
  12.         if (weapon.clss == 2 || weapon.clss == 3 || weapon.clss == 4) {
  13.             weaponimg.src = "cheddar/w_medium.png";
  14.         } else if (weapon.clss == 5) {
  15.             weaponimg.src = "cheddar/w_heavy.png";
  16.         }
  17.  
  18.         weaponimg.style.top = weapony+"px";
  19.         weaponimg.style.left = weaponx+"px";
  20.         weaponimg.style.display = "";
  21.     }
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#4: Jun 16 '08

re: Image does not move to set location


Can you show the rest of your code or a link to a test page?
Newbie
 
Join Date: Apr 2007
Posts: 17
#5: Jun 18 '08

re: Image does not move to set location


Sure.

http://kidkonia.com/projects/cheddar.html
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#6: Jun 19 '08

re: Image does not move to set location


You may need to position the images absolutely.
Reply