-2
\$\begingroup\$

Okay im having a problem with drawing an isometric map exported from tiled.
Here is my problem. My problem
where it should be like this

actual

Here is the code that i used to reproduce the 1st image, what's wrong with it?

for (var y = 0; y < this.height; y++){
            for (var x = 0; x < this.width; x++){
                counter++;  
                var sprite = this.tilemap.getSprite(this.data[ counter ], this);    
                var drawX = 0, drawY = 0;


                if ( sprite ) {     
                    var cell = sprite.cell[ this.data[counter] ];   
                    if (!cell) continue;

                    if (this.orientation == 'isometric') {
                        // Isometric maps 
                        drawX = x * cell.w;
                        drawY = y * cell.h; 
                    } 


                    if (sprite && cell){
                        Engine.currentGame.ctx.drawImage(
                         sprite.sprite.img,
                         cell.x,
                         cell.y,
                         cell.w,
                         cell.h,
                         drawX, 
                         drawY,
                         cell.w,
                         cell.h
                        );
                    }
                }       
            }
        }
        game.currentGame.ctx.restore();
        return this;
    };

UPDATE:

var counter = 0;
        game.currentGame.ctx.save();
        for (var x = 0; x < this.width; x++){
            for (var y = 0; y < this.height; y++){
                counter++;  
                var sprite = this.tilemap.getSprite(this.data[ counter ], this);    
                var drawX = 0, drawY = 0;


                if ( sprite ) {     

                    //console.log( counter );
                    var cell = sprite.cell[ this.data[counter] ];   
                    if (!cell) continue;

                    if (this.orientation == 'isometric') {
                        // Isometric maps 
                        drawX = x * cell.w;
                        drawY = y * cell.h; 
                    } 


                    if (sprite && cell){
                        Engine.currentGame.ctx.drawImage(
                         sprite.sprite.img,
                         cell.x,
                         cell.y,
                         cell.w,
                         cell.h,
                         drawX, 
                         drawY,
                         cell.w,
                         cell.h
                        );
                    }
                }       
            }
        }
        game.currentGame.ctx.restore();
        return this;

update

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This seems like a trivial positioning problem. What have you tried? \$\endgroup\$ Commented Aug 29, 2014 at 11:27
  • \$\begingroup\$ You need to divide by 2 for a 2:1 perspective ratio. \$\endgroup\$ Commented Aug 29, 2014 at 11:29
  • \$\begingroup\$ jsiso.com/tutorials/isometric-engine-basics.html gamedev.stackexchange.com/questions/47388/… gamedev.stackexchange.com/questions/24602/… Ive read all of them but cant get it to work @GerbenJacobs divide what by 2? the width and height \$\endgroup\$ Commented Aug 29, 2014 at 11:31
  • \$\begingroup\$ The position of your tiles, based on the widht and height yeah. So if you have 20x20 blocks, you need to position them on -10 on the x axis, for example. \$\endgroup\$ Commented Aug 29, 2014 at 11:43

2 Answers 2

2
\$\begingroup\$

From the screens it's kinda obvious, that your tiles are positioned wrong.

So it seems your X axis needs to go from top-left to bottom-right and Y axis from top-right to bottom-left. Right now X goes from left to right and Y from top to bottom. Can you update your code to accommodate for that?

Also your spacing between the tiles is too big, but first - solve the first issue ;)

\$\endgroup\$
5
  • \$\begingroup\$ Okay I did, but now it look's very weird, ill add the new code \$\endgroup\$ Commented Aug 29, 2014 at 11:36
  • \$\begingroup\$ Your new code is the same as the old? \$\endgroup\$ Commented Aug 29, 2014 at 11:41
  • \$\begingroup\$ Na, it's not I just switched some values around like you said \$\endgroup\$ Commented Aug 29, 2014 at 11:54
  • \$\begingroup\$ So you want me to read through all of your code once again and cross-check this with the old code? \$\endgroup\$ Commented Aug 29, 2014 at 11:56
  • \$\begingroup\$ Okay i just did a bit more research and got it working!! Thank you \$\endgroup\$ Commented Aug 29, 2014 at 12:21
1
\$\begingroup\$

Iso-tiled maps needs "Z-axis" work, where you must draw from the upper left, moving to the bottom right, as you already know, you must overwrite already existing tiles.

Your code right now is a basic work for common 2D tilesets.

if (this.orientation == 'isometric') { // Isometric maps drawX = x * (cell.w / 2); drawY = y * (cell.h / 2); }

This should do the trick.

\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.