落在地上

我打出我的球员,他跌倒在地面,并开始慢慢地穿过它。 码:

package com.pumkinmelon.Systems; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.systems.IteratingSystem; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.maps.MapLayer; import com.badlogic.gdx.maps.MapObjects; import com.badlogic.gdx.maps.objects.RectangleMapObject; import com.badlogic.gdx.maps.tiled.TiledMap; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.pumkinmelon.Components.HitBox; import com.pumkinmelon.Components.Player; import com.pumkinmelon.Components.Position; import com.pumkinmelon.Components.Velocity; public class CollisionSystem extends IteratingSystem { ComponentMapper<HitBox> Box; ComponentMapper<Position> Pos; ComponentMapper<Velocity> Vel; ComponentMapper<Player> Player; TiledMap map; private OrthographicCamera cam; public CollisionSystem(TiledMap map, OrthographicCamera cam) { super(Family.all(HitBox.class, Position.class, Velocity.class).get()); Box = ComponentMapper.getFor(HitBox.class); Pos = ComponentMapper.getFor(Position.class); Vel = ComponentMapper.getFor(Velocity.class); Player = ComponentMapper.getFor(Player.class); this.map = map; this.cam = cam; } @Override protected void processEntity(Entity e, float dt) { HitBox box = Box.get(e); Position pos = Pos.get(e); Velocity vel = Vel.get(e); Player player = Player.get(e); Vector2 Topright = new Vector2(box.rect.x + box.rect.width, box.rect.y + box.rect.height); Vector2 bottomleft = new Vector2(box.rect.x, box.rect.y); MapLayer collisionObjectLayer = map.getLayers().get(1); MapObjects objects = collisionObjectLayer.getObjects(); for (RectangleMapObject rectangleObject : objects.getByType(RectangleMapObject.class)) { Rectangle rectangle = rectangleObject.getRectangle(); if(player.falling) { if(rectangle.contains(bottomleft)) { vel.velY = 0; player.falling = false; player.canjump = true; player.falling = false; } } if(!player.falling) { if(!rectangle.contains(bottomleft)) { player.falling = true; } } if(rectangle.contains(Topright)) { vel.velX = 0; } } cam.position.x = pos.pos.x + box.rect.width / 2; cam.position.y = pos.pos.y; box.rect.x = pos.pos.x; System.out.println(box.rect.x); box.rect.y = pos.pos.y; System.out.println(box.rect.y); System.out.println("falling " + player.falling); System.out.println("Can Jump " + player.canjump); } } 

 package com.pumkinmelon.Systems; import com.badlogic.ashley.core.ComponentMapper; import com.badlogic.ashley.core.Entity; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.systems.IteratingSystem; import com.badlogic.gdx.graphics.OrthographicCamera; import com.pumkinmelon.Components.Player; import com.pumkinmelon.Components.Position; import com.pumkinmelon.Components.Velocity; public class VelocitySystem extends IteratingSystem { ComponentMapper<Velocity> Vel; ComponentMapper<Position> Pos; ComponentMapper<Player> Player; float G = -15f; @SuppressWarnings("unchecked") public VelocitySystem() { super(Family.all(Velocity.class, Position.class, Player.class).get()); Vel = ComponentMapper.getFor(Velocity.class); Pos = ComponentMapper.getFor(Position.class); Player = ComponentMapper.getFor(Player.class); } @Override protected void processEntity(Entity e, float dt) { Velocity vel = Vel.get(e); Position pos = Pos.get(e); Player player = Player.get(e); if(player.falling) { vel.velY += G * dt; } else vel.velY = 0; System.out.println("Falling velSystem " + player.falling); pos.pos.y += vel.velY; pos.pos.x += vel.velX; System.out.println("posY" + pos.pos.y); System.out.println("velY" + vel.velY); } } 

我认为问题是,当我检查碰撞我检查太多的矩形。 我是对的

如果我是不是我不能解决这个问题,我已经尝试了这么久。

我相信就像你说的,你检查太多的矩形,特别是你检查矩形,大概find一个设置player.fallingfalse ,然后你不断检查矩形,并find一个不直接在玩家设置player.falling true

所以你想要做的是停止检查矩形,如果你发现一个球员下。 你可以使用break关键字来做到这一点:

 if(player.falling) { if(rectangle.contains(bottomleft)) { vel.velY = 0; player.falling = false; player.canjump = true; player.falling = false; break; } } 

当中break运行时, for循环会立即停止,执行后的下一行。 在这种情况下, cam.position.x = pos.pos.x + box.rect.width / 2;

编辑

你基本上希望玩家掉下来,除非他们下面有一个块呢? 再看一下代码,我想你可以通过删除这些行来简化:

 if(!player.falling) { if(!rectangle.contains(bottomleft)) { player.falling = true; } } 

并添加player.falling = true; 就在for循环之前。 在这种情况下, if(player.falling)检查是不必要的,(当然要保留其中的部分)。