I am just starting to build the game "engine" and I am using a sprite drawn in the same position as a rectangle and the two always move together. I have another rectangle which is the floor when the two collide the sprite/rectangle will stay on top of the "floor" rectangle for a small while then it will disappear. I assume it is falling through it as when I hold the key to move the sprite up again it re-appears.
The class I am using to represent my walls and floor is here:
public class Solids {
Array<Rectangle> floors;
Array<Rectangle> walls;
public Solids(){
floors = new Array<Rectangle>();
walls = new Array<Rectangle>();
floors.add(new Rectangle(0, 0, 1280, 10));
}
public Array<Rectangle> getFloors(){
return floors;
}
public Array<Rectangle> getWalls(){
return walls;
}
}
I am representing my character in a class called entities which looks like this:
public class Entity {
Rectangle bottom, left, right, top;
Sprite sprite;
Texture texture;
Solids solids;
int action;
float velocity = 100;
float width, height;
float velocityY = 0;
public Entity(Rectangle b, Texture t){
solids = new Solids();
bottom = b;
texture = t;
width = b.width;
height = b.height;
sprite = new Sprite(t, 0, 0, (int)width, (int)height);
this.setPosition(0, 0);
}
public void setPosition(float x, float y){
bottom.x = x;
bottom.y = y;
sprite.setPosition(x, y);
}
public void draw(SpriteBatch batch){
sprite.draw(batch);
}
public void moveLeft(float delta){
bottom.x -= velocity * delta;
sprite.setPosition(bottom.x, bottom.y);;
}
public void moveRight(float delta){
bottom.x += velocity * delta;
sprite.setPosition(bottom.x, bottom.y);;
}
public void jump(){
velocityY = 15;
}
public void update(float delta){
velocityY -= 50 * delta;
bottom.y += velocityY;
sprite.setPosition(bottom.x, bottom.y);
}
/*public int hits(Rectangle r){
if(bottom.overlaps(r)){
return 1;
}
return -1;
}
public void action(int type, int y){
if(type == 1){
setPosition((int)bottom.x, y);
}
}*/
public void isCollidedFloor(){
/* need to know if collided with floor or wall for jumping need two arrays also need array of entities */
for(int i = 0; i < solids.getFloors().size; i++){
if(bottom.overlaps(solids.getFloors().get(i))){
action(solids.getFloors().get(i));
}
}
}
public void action(Rectangle r){
setPosition(bottom.x, r.y+1);
}
}
The calls worth noting to this class from the main class are as follows:
player = new Entity(new Rectangle(0.0f, 0.0f, 128f,128f), new Texture(Gdx.files.internal("woman.png")));
player.setPosition(1280/2, 100);
player.update(Gdx.graphics.getDeltaTime());
player.isCollidedFloor();
So my question is, why is this happening, tried just creating one Rectangle outside of the solids class and it still doesn't work. I have been playing around for hours and just cant seem to figure it out. Thanks in advance.