山姆开始发现这座森林里还有一些奇妙的情况。
偶而时间会变得缓慢,宁静的像是只有自己一个人在行走。
彷佛和森林处在两种平行时空中...
PS. 这里是开发 iOS 手机游戏的系列文,如果还没看过之前
剧情文章的朋友,欢迎先点这边回顾唷!
在先前的章节已经设定过我们地图上要画的图片代号,想复习的朋友可以点这边
+
代号代表这个位置要画上香菇let mapDraw = [
"ccccccccpccccccci",
" .....e*......b",
"aam.1ji.s.11.zy.b",
" d.3gh....1.wx.b",
"jcl.....ji.1....b",
"d*...11.gh...rt.b",
"d.11.nm....2....b",
"d..1.kl.22.1.naah",
"ot... .b ",
"d..1.jcu vci.b ",
"d.12.d b.kccc",
"d+...gaaaaah. ",
"gaam. .11.n",
" d.rt.1#.q....b",
" d....21.e.ji.b",
"cccl.ji....e.gh.b",
" .gh.13.s....b",
"aaam....1....21.b",
" d...23.rt.1..b",
" d.1.........3b",
"jccl.1.rft.3.1.1b",
"d*.............*b",
"gaaaaaaaaaaaaaaah",
]
请准备好图片,并且拖拉进专案中
mushrooms
,虽然目前游戏只设定有一个香菇,但用阵列的好处是未来还可以再扩充drawMap
方法里,再加上 "+" 的 casemushroom
,将需要的参数带入mapNode
里加入香菇的 node
mushrooms
阵列中class GameScene: SKScene {
...
var mushrooms: [Collection] = []
...
func drawMap() {
for i in 0..<gridYCount {
let mapRowArr = Array(mapDraw[i]);
for j in 0..<gridXCount {
let mapKeys = wallMapping.keys
switch mapRowArr[j] {
...
case "+":
let mushroom = Collection(gridWH: self.gridWH, gridX: j, gridY: i, imageName: "mushroom")
self.mapNode!.addChild(mushroom.node);
self.mushrooms.append(mushroom)
default:
break
}
}
}
}
}
画面上出现一个香菇了!
update
方法里,再加上主角跟香菇之间的位置判断,这边的方式与先前的其他收集物判断方式一样,当香菇还没有被收集、并且位置跟主角一样时,做以下动作:
setGotten(isGotten: true)
setCanMove(isCanMove: false)
stopWeatherTimer
,设定 10
秒过後执行 stopWeatherAction
stopWeatherAction
setCanMove(isCanMove: true)
startMove(direction: .NONE)
stopTimer
方法中,将 stopWeatherTimer
停止class GameScene: SKScene {
...
var stopWeatherTimer: Timer? = nil
...
override func update(_ currentTime: TimeInterval) {
...
for mushroom in self.mushrooms where !mushroom.isGotten && mushroom.gridX == sam.gridX && mushroom.gridY == sam.gridY {
mushroom.setGotten(isGotten: true)
for weather in self.weathers {
weather.setCanMove(isCanMove: false)
}
self.stopWeatherTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(stopWeatherAction), userInfo: nil, repeats: false)
}
}
@objc func stopWeatherAction() {
self.stopWeatherTimer = nil
for weather in self.weathers {
weather.setCanMove(isCanMove: true)
weather.startMove(direction: .NONE)
}
}
func stopTimer() {
if let stopWeatherTimer = self.stopWeatherTimer {
stopWeatherTimer.invalidate()
self.stopWeatherTimer = nil
}
}
}
吃到香菇後,怪物们暂停了 10 秒钟,之後又恢服原样
我们在先前制作迷宫的章节已经加上能隐身的树 (紫色的树),现在我们来制作它的效果吧!
enum ZPosition {
static let HIDE = -1
static let COLLECTION = 1
static let SAM = 2
static let WEATHER = 3
static let PURPLE_TREE = 4
}
#
,则将 zPosition
调整为 ZPosition.PURPLE_TREE
class GameScene: SKScene {
func drawMap() {
for i in 0..<gridYCount {
let mapRowArr = Array(mapDraw[i]);
for j in 0..<gridXCount {
let mapKeys = wallMapping.keys
switch mapRowArr[j] {
case _ where mapKeys.contains(mapRowArr[j]):
let spriteItem = SKSpriteNode(imageNamed: wallMapping[mapRowArr[j]]!);
spriteItem.anchorPoint = CGPoint(x: 0.5, y: 0.5);
spriteItem.size.width = CGFloat(gridWH);
spriteItem.size.height = CGFloat(gridWH);
spriteItem.position = CGPoint(x: gridWH * j + (gridWH/2), y: -gridWH * i - (gridWH/2));
if mapRowArr[j] == "#" {
spriteItem.zPosition = CGFloat(ZPosition.PURPLE_TREE)
}
self.mapNode!.addChild(spriteItem)
...
}
}
}
}
}
struct gridMapping {
...
struct purpleTree {
static let x = 9
static let y = 13
}
}
gridMapping.purpleTree.x
不等於主角的格子 x 且 gridMapping.purpleTree.y
不等於主角的格子 yclass GameScene: SKScene {
override func update(_ currentTime: TimeInterval) {
...
for weather in self.weathers where (weather.gridX == sam.gridX && abs(weather.node.position.y - sam.node.position.y) <= CGFloat(self.gridWH + 6) || weather.gridY == sam.gridY && abs(weather.node.position.x - sam.node.position.x) <= CGFloat(self.gridWH + 6)) && (gridMapping.purpleTree.x != sam.gridX && gridMapping.purpleTree.y != sam.gridY)
{
...
}
}
}
大家可以发挥个人创意,新增更多种类的道具在游戏中喔!
Most of the aspiring professionals are getting cer...
很谢谢各位的支持, 让这趟连续30天的教学旅程能告一段落。 在参考官方文件整理内容的过程中, 让我...
昨天讲了条件渲染,今天来讲列表渲染(List Rendering)吧!! v-for 当我们在页面上...
昨天提到了一个奇怪的现象: byte num = 128; 如上撰写,你的IDE将会在128底下亮出...
文章选自 作者:xiaohesong 连接:https://juejin.im/post/68449...