0
正如您在下面看到的,很多代码非常相似。所以我想知道,是否有任何方法来压缩我的按钮的创建,例如通过使用for循环?Java/libGDX - 清理对象创建
private void initButtons() {
Rectangle upButtonBounds = new Rectangle(55, 100, 75, 75);
Rectangle downButtonBounds = new Rectangle(55, 0, 75, 75);
Rectangle leftButtonBounds = new Rectangle(0, 50, 75, 75);
Rectangle rightButtonBounds = new Rectangle(105, 50, 75, 75);
Rectangle jumpButtonBounds = new Rectangle(VIEWPORT_WIDTH - 185, 115, 80, 80);
Rectangle attackButtonBounds = new Rectangle(VIEWPORT_WIDTH - 185, 25, 80, 80);
Rectangle magicButtonBounds = new Rectangle(VIEWPORT_WIDTH - 100, 75, 80, 80);
upButton = new GameButton(upButtonBounds, "up");
downButton = new GameButton(downButtonBounds, "down");
leftButton = new GameButton(leftButtonBounds, "left");
rightButton = new GameButton(rightButtonBounds, "right");
jumpButton = new GameButton(jumpButtonBounds, "jump");
attackButton = new GameButton(attackButtonBounds, "attack");
magicButton = new GameButton(magicButtonBounds, "magic");
addActor(upButton);
addActor(downButton);
addActor(leftButton);
addActor(rightButton);
addActor(jumpButton);
addActor(attackButton);
addActor(magicButton);
}
你可以做一些数组文本,一个具有界限,另一个名字,然后依次通过数据实例化的按钮和通话addActor。 – pvg
@pvg如何将矩形的边界放入浮点数组中并通过浮点数循环? – zark0
@pvg没关系我想通了,谢谢你的回应:) – zark0