您当前的位置: 首页 >  ar

开发游戏的老王

暂无认证

  • 4浏览

    0关注

    803博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

用Array.shuffle生成随机值

开发游戏的老王 发布时间:2019-10-12 21:01:40 ,浏览量:4

Godot Engine 3.2Alpha2

Godot内置了很多游戏开发常用的功能函数

数组对象内置了一个shuffle()函数,顾名思义就是打乱数组的顺序,常用于构造枚举类型以及其它离散有限数据的随机,非常好用

Shuffles the array such that the items will have a random order. This method uses the global random number generator common to methods such as @GDScript.randi. Call @GDScript.randomize to ensure that a new seed will be used each time if you want non-reproducible shuffling.

范例
extends Node

enum{
	IDLE,
	MOVE,
	SHOOT,
	HURT,
	DIE
}

var state = MOVE

func _process(delta):
	match state:
		IDLE:
			pass
		MOVE:
			pass
		SHOOT:
			pass
		HURT:
			pass
		DIE:
			pass
		_:
			state = choose([IDLE,MOVE,SHOOT,HURT,DIE])

func choose(array):
	randomize()
	array.shuffle()
	return array.front()

shuffle()可以和front()配合使用,注意:由于每次打乱顺序都是基于全局的随机算子,因此如果要确保每次结果不重复,需要先调用一下全局函数randomize()

关注
打赏
1656935939
查看更多评论
立即登录/注册

微信扫码登录

0.0402s