如何在 4 个数字之间生成随机数?
例如,我在舞台中心有一个舞台 { 550x400 } 我有对象 {50x50} 并且我正在创建新对象,但我不需要在舞台中心的静态对象上创建新对象。所以我需要在 4 个数字之间设置随机位置 - [0,250 ; 300,550] 对于 x 和 [0,175 ; 225,400] x
tempNewEnemy._x = getRandom(0,250 ; 300,550)
tempNewEnemy._y = getRandom(0,175 ; 225,400)
是否可以做到这一点,或者我可以通过其他方式做到这一点?
最佳答案
既然范围相同,那么生成两个随机数怎么样?一个用于范围(0-250 沿 x)和(0-175)沿 y),然后另一个随机数将其设置在静态对象的左侧或右侧?
也许是这样的:
tempNewEnemy._x = Math.round(Math.random() * 250) + ((Math.random() < 0.5) ? 0 : 300);
tempNewEnemy._y = Math.round(Math.random() * 175) + ((Math.random() < 0.5) ? 0 : 225);
关于flash - ActionScript 2 在 4 个数字之间随机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14167036/