游戏中精灵随机产生常用的方法

版权声明:本文为博主abcnull的原创文章,未经博主允许不得转载。 https://blog.csdn.net/abcnull/article/details/80409701

游戏中精灵随机产生常用的方法


  下面代码是我在做毕业设计游戏的时候的代码,用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             
}

猜你喜欢

转载自blog.csdn.net/abcnull/article/details/80409701