我正在尝试创建一个随机时间生成对象的脚本,不幸的是我的工作效果不佳。你可以帮我随机化游戏对象的产生之间的时间吗?谢谢!
#pragma strict
var SpawnObject : GameObject;
var SpawnPoint : GameObject;
var SpawnCounter : int = 0;
var SpawnCounterMinMax : int =0;
var SpawnCounterMaxMax : int =0;
function Update ()
{
var float_min_bother_I_hate_you_js : float = this.SpawnCounterMinMax;
var float_max_bother_I_hate_you_js : float = this.SpawnCounterMaxMax;
var SpawnCounterMax = Random.Range(float_min_bother_I_hate_you_js, float_max_bother_I_hate_you_js);
this.SpawnCounter++;
if (this.SpawnCounter >= SpawnCounterMax )
{
Instantiate(this.SpawnObject, this.SpawnPoint.transform.position, this.SpawnPoint.transform.rotation );
this.SpawnCounter = 0;
SpawnCounterMax = Random.Range(float_min_bother_I_hate_you_js, float_max_bother_I_hate_you_js);
}
}
答案 0 :(得分:2)
如果您希望能够指定最小/最大随机时间帧,则此类应该起作用:
class ViewController:UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating {
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tools = [TodoModel(id: "1", image: "QQ", title: "聊天", date: DateFromString("2013-12-13")!),
TodoModel(id: "2", image: "unbrella", title: "下雨", date: DateFromString("2013-12-13")!),
TodoModel(id: "3", image: "plane", title: "飞行", date: DateFromString("2013-12-13")!)]
navigationItem.leftBarButtonItem = editButtonItem()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
if tableview == searchDisplayController?.searchResultsTableView{
return filtertool.count
}
else {
return tools.count
}
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = self.tableview.dequeueReusableCellWithIdentifier("cellinit") as! UITableViewCell
var tool : TodoModel
if tableview == searchDisplayController?.searchResultsTableView{
tool = filtertool[indexPath.row] as TodoModel
}
else { tool = tools[indexPath.row] }
var image = cell.viewWithTag(1) as! UIImageView
var title: UILabel = cell.viewWithTag(2) as! UILabel
var date: UILabel = cell.viewWithTag(3) as! UILabel
image.image = UIImage(named: tool.image)
title.text = tool.title
let local = NSLocale.currentLocale()
let dateformat = NSDateFormatter.dateFormatFromTemplate("yyyy-MM-dd", options: 0, locale: local)
let datematter = NSDateFormatter()
datematter.dateFormat = dateformat
date.text = datematter.stringFromDate(tool.date)
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 65
}
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
if editingStyle == UITableViewCellEditingStyle.Delete {
tools.removeAtIndex(indexPath.row)
self.tableview.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
}
}
override func setEditing(editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
self.tableview.setEditing(editing, animated: animated)
}
@IBAction func close(segue: UIStoryboardSegue){
tableview.reloadData()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditingSegue"{
var vc = segue.destinationViewController as!
EditingView
var indexpath = tableview.indexPathForSelectedRow()
if let index = indexpath {
vc.tool = tools[index.row]
}
}
}
func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath){
let tool = tools.removeAtIndex(sourceIndexPath.row)
tools.insert(tool, atIndex: destinationIndexPath.row)
}
func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool
{
return editing
}
func searchDisplayController(controller: UISearchDisplayController, shouldReloadTableForSearchString searchString: String!) -> Bool{
filtertool = tools.filter(){$0.title.rangeOfString(searchString) != nil }
return true
}
}
使用Time.deltaTime递减一个以秒为单位存储某个值的计时器可确保您的生成在给定范围内发生,而不管帧速率如何。
进一步说明您可能在代码中看到不需要的行为的原因。您每次更新时都会重新随机化SpawnCounter的最大值,而不是每次生成一个对象时设置一次。
答案 1 :(得分:1)
你可以使用协程来实现这一点,就像这样。
int numberOfObjectsToCreate = 5; // number of objects you want to spawn.
float minTimeDiff = 1.0f; // minimum time difference between 2 objects spawned.
float maxTimeDiff = 5.0f; // Maximam time difference between 2 spawns.
public GameObject ObjectToSpawn; // object that is to be spawned;
void Start() {
StartCoroutine(CreateObjectsAtRandom());
}
IEnumerator CreateObjectsAtRandom() {
for(int i = 0; i < numberOfObjectsToCreate; i++) {
GameObject obj = Instantiate(ObjectToSpawn, Vector3.zero, Quarternion.identity) as GameObject;
yield return new WaitForSeconds(Random.Range(minTimeDiff, maxTimeDiff)); // wait for a random time before spawning the next object.
}
}