我正在创建一个桌面游戏,具有六边形的基于图块的地图。我用konva创建了六边形/多边形。我开始的方式是为每个多边形创建一个变量,但这最终成为1000多行代码(因为地图有大约50个六边形)所以我想知道是否有更有效的方法,因为它很难跟踪这么多行代码。
这是1个多边形的外观。 (我打算放置一个精灵,玩家可以在国际象棋中移动精灵,通过点击鼠标等从一个磁贴移动到另一个磁贴等。)
var polyA = new Konva.RegularPolygon({
x: 600,
y: 400,
sides: 6,
radius: 400,
rotation: 90,
stroke: "black",
strokeWidth: 5
});
这是加载图块的代码。你可以看到它很长。
board.add(polyA, polyB, polyC, polyD, polyE, polyF, polyG, polyCr, polyDr, polyEr, polyFr, polyGr, polyA1, polyB1, polyC1, polyD1, polyE1, polyF1, polyCr1, polyDr1, polyEr1, polyFr1, polyGr1, polyB2, polyC2, polyD2, polyE2, polyF2, polyG2, polyCr2, polyDr2, polyEr2, polyB3, polyC3, polyD3, polyE3, polyCr3, polyDr3, polyEr3, polyFr3, polyB4, polyC4, polyD4, polyE4, polyCr4, polyDr4, polyEr4, polyB5, polyC5, polyD5, polyCr5, polyDr5, polyEr5, polyB1u, polyC1u, polyD1u, polyE1u, polyF1u, polyCr1u, polyDr1u, polyEr1u, polyFr1u, polyGr1u, polyB2u, polyC2u, polyD2u, polyE2u, polyF2u, polyG2u, polyCr2u, polyDr2u, polyEr2u, polyB3u, polyC3u, polyD3u, polyE3u, polyCr3u, polyDr3u, polyEr3u, polyFr3u, polyB4u, polyC4u, polyD4u, polyE4u, polyCr4u, polyDr4u, polyEr4u, polyB5u, polyC5u, polyD5u, polyCr5u, polyDr5u, polyEr5u);
stage.add(board);