# as2: map array page 2

53 posts

 It does the same thing, but using the _x and _y attributes is the “right” way to do it. ok, ive come to the point where i want to do collision on each induvidual tile in the tileArray, i started thinking and did something like this for (var b:Number = 0; b < tileArray.length; b++){ for (var c:Number = 0; c < levelData2D©.length; c++){ and then here i would do something like this = if this hittest tile©[b] } } but im not really sure where im going with what i did! is this the correct way of doing it? did i misunderstand anything? Originally posted by darkjonas8:ok, ive come to the point where i want to do collision on each induvidual tile in the tileArray, i started thinking and did something like this for (var b:Number = 0; b < tileArray.length; b++){ for (var c:Number = 0; c < levelData2D©.length; c++){ and then here i would do something like this = if this hittest tile©[b] } } but im not really sure where im going with what i did! is this the correct way of doing it? did i misunderstand anything? Nononononono never. You would be running a ridiculous amount of hit-tests a second doing this. What you want to do is use math to find the bottom of your player: ```var botX:Number; var botY:Number; botX = player.x - (player.x % Tile.WIDTH); botY = player.y - (player.y + player.height % Tile.HEIGHT); ``` divide by Tile size: ```botX /= Tile.WIDTH; botY /= Tile.HEIGHT; ``` and check if the cell its bottom in is solid. If it is, move it on top of it. ```if (cellAt(botX, botY).solid) { player.y = cellAt(botX, botY).y - player.height; } ``` Tons of ways to improve this, but this should get you going. Originally posted by darkjonas8:ok, ive come to the point where i want to do collision on each induvidual tile in the tileArray, i started thinking and did something like this for (var b:Number = 0; b < tileArray.length; b++){ for (var c:Number = 0; c < levelDa+ta2D©.length; c++){ and then here i would do something like this = if this hittest tile©[b] } } but im not really sure where im going with what i did! is this the correct way of doing it? did i misunderstand anything? Not cache friendly way (read truefire’s post) + AS2 + using hitTest default function + level with more than 500 tiles (or even just 100) = Ultra-lag. RTL_Shadow’s way is what you’re looking for. If you only have one type of solid platform you may want to check the int array (Instead of `tileArray[botX][botY].solid` using `levelData[botX][botY] == 1`), but it’s better to add .solid property for in the case you decide to use more. (You don’t want to do something like `levelData[botX][botY] == 1 || levelData[botX][botY] == 4 || levelData[botX][ botY] == 7 || levelData[botX][botY] == 10` RTL_Shadow, what do you mean by cell? im sry, English is not my first language :D Originally posted by darkjonas8:RTL_Shadow, what do you mean by cell? im sry, English is not my first language :D It was just an example name for a function that returns the tile at that position. cell = tile = square = block = position thx rtl, made it much easier to understand :D Originally posted by RTL_Shadow:Originally posted by darkjonas8:ok, ive come to the point where i want to do collision on each induvidual tile in the tileArray, i started thinking and did something like this for (var b:Number = 0; b < tileArray.length; b++){ for (var c:Number = 0; c < levelData2D©.length; c++){ and then here i would do something like this = if this hittest tile©[b] } } but im not really sure where im going with what i did! is this the correct way of doing it? did i misunderstand anything? Nononononono never. You would be running a ridiculous amount of hit-tests a second doing this. What you want to do is use math to find the bottom of your player: ```var botX:Number; var botY:Number; botX = player.x - (player.x % Tile.WIDTH); botY = player.y - (player.y + player.height % Tile.HEIGHT); ``` divide by Tile size: ```botX /= Tile.WIDTH; botY /= Tile.HEIGHT; ``` and check if the cell its bottom in is solid. If it is, move it on top of it. ```if (cellAt(botX, botY).solid) { player.y = cellAt(botX, botY).y - player.height; } ``` Tons of ways to improve this, but this should get you going. theres something i dont understand, my tile width is 36 so if im at coordinate 24 inside that tile then i divide by tile size then i would get 0,6 but wouldent i need to round to the next 36 and then check whether its solid or not? and also, i got several square shapes like a teleporter, enemies etc… so it wouldent be possible to divide by 36 on a 10 pixel wide teleporter platform! oh and how do you think i could to that .soldid thing to check if the block is solid? .solid should just be a Boolean in your tile class. This is for tiles, not enemies/abstract shapes. You’d have to use an AABB hit-testing algorithm for enemies if they’re square. Not sure what you mean by your coordinates. make sure your registration point is in the top-left. i dont code in classes i code on main timeline as someone has told me is the best way! and by the coordinates thing i mean that the player is not just gonna move 36 pixels at a time (width of tiles), ur not gonna hit up one time and move one tile uppwards, lets say theres only an x coord, u move from x 0 to 23, so when you are gonna see if the bottom of the player is inside the solid tile you divide the botx with tile width, but you will not get a whole number(0,66) and can therefor not check if array tile number 0,66 is solid as there is no tile number 0,66 but 1,2,3,4,5. i hope u understand! Originally posted by darkjonas8:i dont code in classes i code on main timeline as someone has told me is the best way! and by the coordinates thing i mean that the player is not just gonna move 36 pixels at a time (width of tiles), ur not gonna hit up one time and move one tile uppwards, lets say theres only an x coord, u move from x 0 to 23, so when you are gonna see if the bottom of the player is inside the solid tile you divide the botx with tile width, but you will not get a whole number(0,66) and can therefor not check if array tile number 0,66 is solid as there is no tile number 0,66 but 1,2,3,4,5. i hope u understand! Whoever told you coding on timeline is the best way is stupid and is completely uninformed. You need to code in classes, else doing things like this won’t work out wel.. Originally posted by darkjonas8:i dont code in classes i code on main timeline as someone has told me is the best way! and by the coordinates thing i mean that the player is not just gonna move 36 pixels at a time (width of tiles), ur not gonna hit up one time and move one tile uppwards, lets say theres only an x coord, u move from x 0 to 23, so when you are gonna see if the bottom of the player is inside the solid tile you divide the botx with tile width, but you will not get a whole number(0,66) and can therefor not check if array tile number 0,66 is solid as there is no tile number 0,66 but 1,2,3,4,5. i hope u understand! If you use the formula RTL_Shadow gave you it should give you a whole number since you use player.x – (player.x % Tile.WIDTH) to find a multiple of the tile width. (I’m not so sure about the y-coordinate formula, I think it should be ` (player.y + player.height) - ((player.y + player.height) % Tile.HEIGHT); ` but I’m not sure) Anyways, you were talking about width and x, so there should be something wrong in your implementation. Originally posted by SumGato:Originally posted by darkjonas8:i dont code in classes i code on main timeline as someone has told me is the best way! and by the coordinates thing i mean that the player is not just gonna move 36 pixels at a time (width of tiles), ur not gonna hit up one time and move one tile uppwards, lets say theres only an x coord, u move from x 0 to 23, so when you are gonna see if the bottom of the player is inside the solid tile you divide the botx with tile width, but you will not get a whole number(0,66) and can therefor not check if array tile number 0,66 is solid as there is no tile number 0,66 but 1,2,3,4,5. i hope u understand! If you use the formula RTL_Shadow gave you it should give you a whole number since you use player.x – (player.x % Tile.WIDTH) to find a multiple of the tile width. (I’m not so sure about the y-coordinate formula, I think it should be ` (player.y + player.height) - ((player.y + player.height) % Tile.HEIGHT); ` but I’m not sure) Anyways, you were talking about width and x, so there should be something wrong in your implementation. what does player.x % tile.width do? i dont understand the %, i may be overlooking the obvious though :D TBH lots of these could be answered with a quick search. % is the modulus operator, it returns the remainder when you divide something: 12 % 5 = 2 so when do you do x – x % Tile.Width where x is 200 and the width is 24 you get 196 which is the closest tile’s origin. Originally posted by RTL_Shadow:TBH lots of these could be answered with a quick search. % is the modulus operator, it returns the remainder when you divide something: 12 % 5 = 2 so when do you do x – x % Tile.Width where x is 200 and the width is 24 you get 196 which is the closest tile’s origin. Thank you very much! Originally posted by RTL_Shadow: x – x % Tile.Width where x is 200 and the width is 24 you get 196 which is the closest tile’s origin. Actually its equivalent to Math.floor(x / Tile.Width). So it’s not technically the closest. x = 23 won’t get you `Tiles[1]` it’ll get you `Tiles[0]` despite being closer to `Tiles[1]`. so 23 = 0, 35 = 0, 36 = 1 ? Originally posted by darkjonas8:so 23 = 0, 35 = 0, 36 = 1 ? Depends on your tile width. If it’s 36, then yes. Originally posted by Draco18s:Originally posted by RTL_Shadow: x – x % Tile.Width where x is 200 and the width is 24 you get 196 which is the closest tile’s origin. Actually its equivalent to Math.floor(x / Tile.Width). So it’s not technically the closest. x = 23 won’t get you `Tiles[1]` it’ll get you `Tiles[0]` despite being closer to `Tiles[1]`. Closest is a relative term. If you mean by origin, yes. But in this case x = 23 getting Tiles0 would be correct because the point (23, 0) is inside Tile0. But you’re right, I should’ve used a different wording. hmm, so then if i check for a tile in x 23 i would get tile number 0 that doesent exist? or does flash count number 1 in the array as 0 ? Originally posted by darkjonas8:hmm, so then if i check for a tile in x 23 i would get tile number 0 that doesent exist? or does flash count number 1 in the array as 0 ? Yes, arrays are 0-indexed. Originally posted by RTL_Shadow:Originally posted by MossyStump:Originally posted by RTL_Shadow:Don’t use a 2D array, it’s 2x more lookups. Just use a 1-Dimensional one and use a formula such as `y*columns+width` to get the ID of the cell you want. This way you can loop with one loop instead of 2 nested ones. I’ve always been curious of this, how much better is it than a 2D array, and would you recommend it for a 1D vector as well opposed to a 2? Using a 2D array you lookup it with level[y][x], so you’re technically doing level[i] twice, so if you are accessing it a lot it can stack up. Unless you cache it to a variable, but even then sometimes you’ll have to access it directly sometimes. And yes, it would be fastest to do a 1-Dimensional fixed length vector. It shouldn’t matter. Lookups are pretty damn fast. Unless you’re using ludicrously large arrays. It’s better to use 2arrays and not have hideous unmaintainable code, like treating an array as a 2d array. Originally posted by Idiral: It shouldn’t matter. Lookups are pretty damn fast. Unless you’re using ludicrously large arrays. It’s better to use 2arrays and not have hideous unmaintainable code, like treating an array as a 2d array. Or doing a ludicrously large number of them. Originally posted by Draco18s:Originally posted by Idiral: It shouldn’t matter. Lookups are pretty damn fast. Unless you’re using ludicrously large arrays. It’s better to use 2arrays and not have hideous unmaintainable code, like treating an array as a 2d array. Or doing a ludicrously large number of them. Basically, it’s still pretty much irrelevant. If you’re looking up all these objects and doing something with the object, chances are whatever you’re doing with the object is using up 99.99% of the processing time compared to the actual lookup