1.地图大于屏幕大小,对camera进行控制,并不是简单地让player一直限制在屏幕中心,而是将player限制在屏幕中间的一个矩形区域内.
1 float rectangleWidth=GameVariables.WIDTH/3; 2 float rectangleHeight=GameVariables.HEIGHT/3; 3 float cameraX=stage.getCamera().position.x; 4 float cameraY=stage.getCamera().position.y; 5 if (cameraX > player.getCenterX()+ rectangleWidth/2) { 6 cameraX = player.getCenterX()+ rectangleWidth/2; 7 }else if (cameraX < player.getCenterX()- rectangleWidth/2) { 8 cameraX= player.getCenterX()- rectangleWidth/2; 9 } 10 if (cameraX-GameVariables.WIDTH/2 <0 ) { 11 cameraX=GameVariables.WIDTH/2; 12 }else if (cameraX+GameVariables.WIDTH/2 > getMapWidth()) { 13 cameraX= getMapWidth()- GameVariables.WIDTH/2; 14 } 15 16 if (cameraY > player.getCenterY() +rectangleHeight/2) { 17 cameraY = player.getCenterY() +rectangleHeight/2; 18 }else if (cameraY < player.getCenterY() - rectangleHeight/2) { 19 cameraY = player.getCenterY() - rectangleHeight/2; 20 } 21 if (cameraY-GameVariables.HEIGHT/2<0) { 22 cameraY=GameVariables.HEIGHT/2; 23 } 24 if (cameraY+GameVariables.HEIGHT/2>getMapHeight()) { 25 cameraY=getMapHeight()-GameVariables.HEIGHT/2; 26 } 27 stage.getCamera().position.set(cameraX, cameraY, 0);
2.
时间: 2024-10-10 13:01:51