I have a function for finding on which tile the mouse is on:
function testTile(mouseX, mouseY){
if(mouseX<mapArray[0].length*20 && mouseY<mapArray.length*20){
var tile = getTilePosition(mouseX-10, mouseY-10);
selectedtile.x = tile.xtile,
selectedtile.y = tile.ytile;
}
}
and a function for getting the tile position given x and y
function getTilePosition(x, y){
var xtile = Math.floor(x/20);
var ytile = Math.floor(y/20);
return{
xtile : xtile,
ytile : ytile
}
}
but when I call selectedtile.x or selectedtile.y I get a value of 'undefined'. What's wrong?
It appears your values are declared inside the function and are out of scope.
Do this:
var selectedtile = new Object() //de OUTSIDE All functions.
//de this works too: var selectedtile = {}
function testTile(mouseX, mouseY){
if(mouseX<mapArray[0].length*20 && mouseY<mapArray.length*20){
var tile = getTilePosition(mouseX-10, mouseY-10);
selectedtile.x = tile.xtile,
selectedtile.y = tile.ytile;
}
}
function getTilePosition(x, y){
var xtile = Math.floor(x/20);
var ytile = Math.floor(y/20);
return {
xtile : xtile,
ytile : ytile
}
}