gpt4 book ai didi

java - 通过数组随机化坐标

转载 作者:行者123 更新时间:2023-12-02 12:01:20 30 4
gpt4 key购买 nike

当单击按钮时,我一直试图将按钮传送到屏幕上的随机位置(在 9 个给定坐标之一中),效果很好,但后来我发现一个常见的结果是按钮保持不变然后,我创建了一个新数组,其中不包括按钮的当前坐标,该数组在按钮不停留在同一个位置方面起作用,但是,现在我遇到了一个新问题,由于某些未知的原因 -是的,我尝试调试 - 按钮只有 2 个坐标,这里是代码:

    gameBtnRX = gameBtn.getX(); //Getting the current X of the button
gameBtnRY = gameBtn.getY(); //Getting the current Y of the button


switch ((int) gameBtnRX) { //Setting the _randArgsX array values
case 200: _randArgsX = new float[]{600, 1000}; break;
case 600: _randArgsX = new float[]{200, 1000}; break;
case 1000: _randArgsX = new float[]{200, 600}; break;
default: _randArgsX = new float[]{200, 600, 1000}; break;
}

switch ((int) gameBtnRY) { //Setting the _randArgsY array values
case 500: _randArgsY = new float[]{1000, 1500}; break;
case 1000: _randArgsY = new float[]{500, 1500}; break;
case 1500: _randArgsY = new float[]{500, 1000}; break;
default: _randArgsY = new float[]{500, 1000, 1500}; break;
}

randGameBtnX = rand.nextInt(_randArgsX.length - 1); //Randomizing one of (max 3) the values in the array
randGameBtnY = rand.nextInt(_randArgsY.length - 1); //And putting them as the X and Y of gameBtn
gameBtnX = _randArgsX[randGameBtnX];
gameBtnY = _randArgsY[randGameBtnY];
gameBtn.setX(gameBtnX);
gameBtn.setY(gameBtnY);

(200,600,100是可能的X坐标,500,1000,15000是可能的Y坐标)

最佳答案

参见Random.nextInt(int)

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive).

因此不需要将数组长度减一。

关于java - 通过数组随机化坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47237785/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com