游戏中精灵随机产生常用的方法
下面代码是我在做毕业设计游戏的时候的代码,用java写的,选取关键部分,用到了Math.random()。
int[] nums = {0,1,2,3};
/*Math.random()取0-1浮点数,
*floor是向下取整,
*index是数组下表,
*type是数组中的元素
*/
int index = (int)Math.floor(nums.length*Math.random());//随机数组下标
int type = nums[index];//数组中随机数
if(type == 0)
{
//执行方法1
}
else if(type == 1){
//执行方法2
}
else if(type == 2){
//执行方法3
}
else if(type == 3){
//执行方法4
}
本文介绍了一种在游戏开发中实现精灵随机生成的方法。通过使用Java的Math.random()函数配合数组,可以简单有效地生成随机精灵类型,从而为游戏增添更多变化。
78

被折叠的 条评论
为什么被折叠?



