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,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2},
{3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3}};
Now the 1’s are supposed to be walkable tiles, tiles that the player will not fall through.
I have a method that iterates through the tiles, but how can I detect collision for the tiles that are 1. The player can freely move, so i dont want its movement to be tile based.
Any ideas, i tried creating a class called CollisionBlocks and set up an intersects method:
public boolean intersects(Player player)
{
boolean intersect = false;
int areaX = (width + position.x);
int areaY = (height + position.y);
if( player.position.x > position.x && player.position.x < areaX)
{
if( player.position.y > position.y && player.position.y < areaY)
{
intersect = true;
}
else
{
intersect = false;
}
}
else
{
intersect = false;
}
return intersect;
}
and i am trying to test this by using:
if( falling )
{
if( (position.y + height) > 240)
{
position.y = 240 - height;
falling = false;
}
else
{
for( int i = 0; i < 37; i++)
{
if( cBlocks[i].intersects(this) )
{
falling = false;
}
}
position.y += GRAVITY;
}
}
basically it iterates through an array of collision blocks that are set up by this:
public CollisionBlocks[] iterateTiles()
{
CollisionBlocks[] cb = new CollisionBlocks[(noCollideable + 1)];
int nextBlock = 0;
for(int y = 0; y < level1.level1.length; y++)
{
for(int x = 0; x < level1.level1[y].length; x++)
{
int tID = level1.level1[y][x];
if( tID == 1)
{
cb[nextBlock] = new CollisionBlocks(x * 16, y * 16, 16, 16);
nextBlock++;
}
}
}
return cb;
}
but it doesnt seem to work for me.
Help would be appreciated. I am using Blackberry JDE library so i dont have access to libgdx or lwjgl