Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by SnakePlissken

So was working on a loop during the init of a game map. Basically I plan on using this to be able to work with tiles adjacent to my current tile. Would like to know if I am going to run into any problems or if there is some ways to clean up the code.

Thanks,

Snake

MapTiles: function() {
		
		
		var intNumber = 0;
		var intXFloor = 0;
		var intYFloor = 0;
		
		// Init variables Ceiling variable will be the pixel size of 1 tile
		var intXCeiling = 25;
		var intYCeiling = 25;
		
		// Loop to create tile floor and ceiling and number it for unique Identifier
		for (intTileIndex = 0; intTileIndex < 16; intTileIndex += 1) {
			
			for (intIndex = 0; intIndex < 16; intIndex += 1) {
				MyArray.push({ Number: intNumber, xFloor: intXFloor, yFloor: intYFloor, XCeiling: intXCeiling, YCeiling: intYCeiling });
			
				intNumber += 1;
				intXFloor += 25;
				intXCeiling += 25;
				
			}
			
			// Reset x to far left after last column in previous row.
			intXFloor = 0;
			intXCeiling = 25;
			
			// Change to new row
			intYFloor += 25;
			intYCeiling += 25;
			
		}
		
	}

1 decade ago by Arantor

I don't understand why you're making it so complex.

As far as I remember, each layer of map tiles is stored in the master backgroundMaps array, but it's stored row by row, so you can address it directly by x and y co-ordinates rather than converting it to a single index and back.

1 decade ago by SnakePlissken

Hmm... where are these tile x y coords stored? All I can see is the ability to change the tileset of the tile, and the x and y of entities. However there is no way to tell what each individual tile coords are. What am I missing?

Basically I going to create mouseover functions with the x floor and ceiling and y floor and ceiling. Is there another way to go about this?

Thanks,

Snake

1 decade ago by Arantor

The pos.x/y of an entity, divided by the tilesize will tell you the tile x/y.

The engine itself does this very thing, when it figures out what tile an entity is colliding with, since it has to identify the tiles the entity is on, and look it up in the collision map.

As for the map itself, every background map is an entry in the backgroundMaps property of the main game object (which can be accessed globally via ig.game.backgroundMaps and within the game object via this.backgroundMaps), where it's an array of map objects, and the data property of each layer is an array itself.

If you know the x/y co-ord you want to access, and you know it's on the first layer that you added, this.backgroundMaps[0].data[y][x] will give you the tile itself.

As far as mouseover goes, ig.input.mouse.x and ig.input.mouse.y will give you the co-ordinates the mouse is over, and IIRC that's relative to the canvas.

Can't really say any more than that without knowing what it is you're trying to achieve, since what you're doing may or may not be the best way to approach it, but I can only tell you what may be wrong with the process you've outlined.

1 decade ago by SnakePlissken

Arantor you are the man thanks for the in depth response. I have been very impressed with this community so far.

I will test what you just explained and see if it will work in my situation. But by the way you describe it that should be perfect for what I was trying to achieve. I had no idea that the backgroundMaps[0].data[y][x] would work when I was looking at what was in data it looked like it was just 1 2 or 3 which was I imagine correlated to the tile used in the tilesheet.

Thanks,

Snake

1 decade ago by Arantor

Thing is, that's all a map actually is... a number representing the tile to be drawn. A backgroundMap does nothing in itself, it's just a map of image indexes.

A collision map on the other hand is stored in this.collisionMap and is also just a series of numbers. The difference is that the number relates to an array in the collisionmap definition which identifies what the tile should do as far as collision goes.

The thing is, I have absolutely no idea what you're actually trying to do, so I have no idea whether it is the right solution for you or not, or whether you need to do something else on top... (this is something I find frustrating at every single community I have been a member of from a technical perspective, people always focus on how they're trying to achieve something, with the assumption that their world view must inherently be correct, and that there couldn't possibly be a better way to achieve what it is they're trying to do. But no-one ever explains up front what they're trying to achieve, it's always 'here's some code, will this work?' or 'here's some code, why doesn't it work?' Too much on the how, never enough on the why.)

1 decade ago by SnakePlissken

Sorry didn't mean to act like I am trying to solve one particular problem rather grasp a better understanding of the tools at my disposal.

Your description alone has helped me better understand this engine and how I may tackle future problems up ahead.

Sorry I should of explained myself better in that I was looking for more general help rather than help with a solution to a specific task.

Thanks again Arantor!
Page 1 of 1
« first « previous next › last »