Skip to Content
Swift游戏开发经典实例
book

Swift游戏开发经典实例

by Jonathon Manning, Paris Buttfield-Addison
January 2020
Intermediate to advanced
356 pages
6h 4m
Chinese
China Electric Power Press Ltd.
Content preview from Swift游戏开发经典实例
人工智能和行为
271
9.6
目标选择问题
问题
我们想从多个目标中选择一个目标进行追逐。
解决方案
选择最优目标的一般算法步骤如下:
1.
bestScoreSoFar
设置为最恶劣的情况(要么
0
要么无限大,取决于你要搜寻
的目标是什么)。
2.
bestTargetSoFar
设为空。
3.
遍历每个可能的目标:
a.
计算每个目标的得分情况。
b.
如果该目标得分大于
bestScoreSoFar
i.
将该目标保存到
bestTargetSoFar
ii.
将该目标得分保存到
bestScoreSoFar
遍历结束,
bestTargetSoFar
保存的要么是搜索得到的最佳目标,要么为空。
上述步骤用代码实现后如下所示。
bestScoreSoFar
变量用
nearestNodeDistance
替;用它来保存搜索到的距离最近的目标,初始值为最大可能距离(即无限大)。
然后对所有可能的目标对象进行遍历,每当找到一个距离比它小的目标对象,就把
新目标的距离赋给它:
var nearestNodeDistance = CGFloat.infinity
var nearestNode : Critter? = nil
//
搜索最近的目标
scene?.enumerateChildNodes(withName: "Critter") { (node, stop) -> Void in
if let otherCritter = node as? Critter {
if otherCritter == ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

精通機器學習

精通機器學習

Aurélien Géron
FreeBSD® Unleashed

FreeBSD® Unleashed

Brian Tiemann, Michael C. Urban
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
Swift学习手册

Swift学习手册

Jon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787519840129