iOS平台手游框架开发及应用外文翻译资料

 2022-03-24 10:03

Chapter 5

Adding Actions and Animations

In this chapter, you will refactor the orb nodersquo;s layout one last time with the goal of enhancing playability. After that, wersquo;ll show you how you can use SKActions to move an SKSpriteNode back and forth across the scene and then make that same node rotate forever. We close out the chapter with a look at how you can add colorizing effects to an SKSpriteNode using a colorize action.

Refactoring the Orb Node Layout One Last Time

Before we start talking about SKActions, a few changes need to be made to the way the orb nodes are laid out. Chapter 4 mentioned refactoring the layout of the orb nodes. In this section, we want to not only refactor the layout of the nodes into a method but also modify the layout of the orb nodes one last time. The goal of this modification is to lay out the orbs in a little more gamelike manner. This new method is shown here:

func addOrbsToForeground() {

\37var orbNodePosition = CGPoint(x: playerNode.position.x, y: playerNode.position.y 100)

\37var orbXShift : CGFloat = -1.0

\37for _ in 1...50 {

\376\37let orbNode = SKSpriteNode(imageNamed: 'PowerUp')

\376\37if orbNodePosition.x - (orbNode.size.width * 2) lt;= 0 {

\376\377\00orbXShift = 1.0

\376\37}

copy; James Goodwill and Wesley Matlock 2017

55

J. Goodwill and W. Matlock, Beginning Swift Games Development for iOS, DOI 10.1007/978-1-4842-2310-9_5

56 CHAPTER 5: Adding Actions and Animations

\376\37if orbNodePosition.x orbNode.size.width gt;= size.width {

\376\377\00orbXShift = -1.0

\376\37}

\376\37orbNodePosition.x = 40.0 * orbXShift

\376\37orbNodePosition.y = 120

\376\37orbNode.position = orbNodePosition

\376\37orbNode.physicsBody = SKPhysicsBody(circleOfRadius: orbNode.size.width / 2) \376\37orbNode.physicsBody?.isDynamic = false

\376\37orbNode.physicsBody?.categoryBitMask = CollisionCategoryPowerUpOrbs \376\37orbNode.physicsBody?.collisionBitMask = 0 \376\37orbNode.name = 'POWER_UP_ORB'

\376\37foregroundNode.addChild(orbNode)

\37}

}

As you look at this method, yoursquo;ll see therersquo;s really nothing too special about it. It begins by setting the initial position of the first orb to be 100 points directly above the player node. It then adds 50 orbs, 40 points apart on the x-axis and 120 points apart on the y-axis, moving from right to left on the scene until all the nodes have been added. Go ahead and add this method to the GameScene directly after the init() method.

Once you have the addOrbsToForeground() method added to the game scene, then you need to add a call to invoke this method. Add the following method invocation to the end of the GameScenersquo;s init() method:

addOrbsToForeground()

Before invoking this new method, you need to remove the old orb-adding code from the init() method. To do so, find the following code and remove it from the init():

var orbNodePosition = CGPoint(x: playerNode.position.x, y: playerNode.position.y 100)

for _ in 0...19 {

\376let orbNode = SKSpriteNode(imageNamed: 'PowerUp')

\376orbNodePosition.y = 140

\376orbNode.position = orbNodePosition

\376orbNode.physicsBody = SKPhysicsBody(circleOfRadius: orbNode.size.width / 2) \376orbNode.physicsBody?.isDynamic = false

\376orbNode.physicsBody?.categoryBitMask = CollisionCategoryPowerUpOrbs \376orbNode.physicsBody?.collisionBitMask = 0 \376orbNode.name = 'POWER_UP_ORB'

\376foregroundNode.addChild(orbNode)

}

CHAPTER 5: Adding Actions and Animations

57

orbNodePosition = CGPoint(x: playerNode.position.x 50, y: orbNodePosition.y)

for _ in 0...19 {

\376let orbNode = SKSpriteNode(imageNamed: 'PowerUp')

\376orbNodePosition.y = 140

\376orbNode.position = orbNodePosition

\376orbNode.physicsBody = SKPhysicsBody(circleOfRadius: orbNode.size.width / 2) \376orbNode.physicsBody?.isDynamic = false

\376orbNode.physicsBody?.categoryBitMask = CollisionCategoryPowerUpOrbs \376orbNode.physicsBody?.collisionBitMask = 0 \376orbNode.name = 'POWER_UP_ORB'

\376foregroundNode.addChild(orbNode)

}

Once yoursquo;ve made and saved all these changes, run the app again and take a look at the new layout. It should look like Figure\5-1.

Figure 5-1.\ The new orb layout

58 CHAPTER 5: Adding Actions and Animations

After restarting the app, play around with the new layout. Itrsquo;s a lot more fun than just going straight up. You now have to fly side to side to collect orbs.

Sprite Kit Actions

In Sprite Kit, whenever you want to move, modify, or perform some action on an SKNode, yoursquo;re going to be, more often than not, applying that change using an SKAction. Apple defines an SKAction as follows: “An action is an object that defines a change you want to make to the scene.” There are many different uses for SKActions, but some of the more common uses of SKActions are as follows:

Animating a node through a series of textures

Modifying the position of a node using its position property

Changing the visibility of a node using its hidden property

Adjusting the translucency of a node using its alpha property

Modifying the size of a node using its size property

Playing simple sounds

Colorizing a node

To use an SKAction, you have to perform only two steps: you create the action you want to perform, and you tell the node you want to perform it on to run the action. Here

全文共28928字,剩余内容已隐藏,支付完成后下载完整资料


章节5

添加动作和动画

在本章中,您将最后一次重构orb节点的布局,以增强可播放性。 之后,我们将向您展示如何使用SKActions在场景中来回移动SKSpriteNode,然后使同一节点永远旋转。 我们结束这一章,看看如何使用colorize操作为SKSpriteNode添加着色效果。

最后一次重构Orb节点布局

在开始讨论SKActions之前,需要对orb节点的布局方式进行一些更改。 第4章提到重构orb节点的布局。 在本节中,我们不仅要将节点的布局重构为方法,还要最后一次修改orb节点的布局。 这种修改的目标是以更具游戏性的方式布局球体。 这个新方法如下所示:

func addOrbsToForeground() {

\37var orbNodePosition = CGPoint(x: playerNode.position.x, y: playerNode.position.y 100)

\37var orbXShift : CGFloat = -1.0

\37for _ in 1...50 {

\376\37let orbNode = SKSpriteNode(imageNamed: 'PowerUp')

\376\37if orbNodePosition.x - (orbNode.size.width * 2) lt;= 0 {

\376\377\00orbXShift = 1.0

\376\37}

\376\37if orbNodePosition.x orbNode.size.width gt;= size.width {

\376\377\00orbXShift = -1.0

\376\37}

\376\37orbNodePosition.x = 40.0 * orbXShift

\376\37orbNodePosition.y = 120

\376\37orbNode.position = orbNodePosition

\376\37orbNode.physicsBody = SKPhysicsBody(circleOfRadius: orbNode.size.width / 2) \376\37orbNode.physicsBody?.isDynamic = false

\376\37orbNode.physicsBody?.categoryBitMask = CollisionCategoryPowerUpOrbs \376\37orbNode.physicsBody?.collisionBitMask = 0 \376\37orbNode.name = 'POWER_UP_ORB'

\376\37foregroundNode.addChild(orbNode)

\37}

}

当你看着这个方法时,你会发现真的没什么特别的。 它首先将第一个球体的初始位置设置为球员节点正上方的100个点。 然后它添加50个球体,在x轴上相距40个点,在y轴上相距120个点,在场景中从右向左移动,直到所有节点都添加完毕。 继续并在init()方法之后直接将此方法添加到GameScene。

一旦将addOrbsToForeground()方法添加到游戏场景中,则需要添加一个调用来调用此方法。 将以下方法调用添加到GameScene的init()方法的末尾:

addOrbsToForeground()

在调用这个新方法之前,您需要从init()方法中删除旧的添加orb的代码。 为此,请找到以下代码并将其从init()中移除:

var orbNodePosition = CGPoint(x: playerNode.position.x, y: playerNode.position.y 100)

for _ in 0...19 {

\376let orbNode = SKSpriteNode(imageNamed: 'PowerUp')

\376orbNodePosition.y = 140

\376orbNode.position = orbNodePosition

\376orbNode.physicsBody = SKPhysicsBody(circleOfRadius: orbNode.size.width / 2) \376orbNode.physicsBody?.isDynamic = false

\376orbNode.physicsBody?.categoryBitMask = CollisionCategoryPowerUpOrbs \376orbNode.physicsBody?.collisionBitMask = 0 \376orbNode.name = 'POWER_UP_ORB'

\376foregroundNode.addChild(orbNode)

}

orbNodePosition = CGPoint(x: playerNode.position.x 50, y: orbNodePosition.y)

for _ in 0...19 {

\376let orbNode = SKSpriteNode(imageNamed: 'PowerUp')

\376orbNodePosition.y = 140

\376orbNode.position = orbNodePosition

\376orbNode.physicsBody = SKPhysicsBody(circleOfRadius: orbNode.size.width / 2) \376orbNode.physicsBody?.isDynamic = false

\376orbNode.physicsBody?.categoryBitMask = CollisionCategoryPowerUpOrbs \376orbNode.physicsBody?.collisionBitMask = 0 \376orbNode.name = 'POWER_UP_ORB'

\376foregroundNode.addChild(orbNode)

}

一旦您完成并保存了所有这些更改,再次运行该应用程序并查看新布局。 它应该如图5-1所示。

图5-1. 新的orb布局

重新启动应用程序后,玩弄新的布局。 这比直接往上更有趣。 你现在必须一边飞一边收集球。

Sprite Kit操作

在Sprite Kit中,无论何时想要移动,修改或对SKNode执行一些操作,通常都会使用SKAction应用该更改。 Apple定义了一个SKAction,如下所示:“动作是一个对象,用于定义您想要对场景进行的更改。”SKActions有许多不同用途,但SKActions的一些更常见用途如下所示:

Animating a node through a series of textures

Modifying the position of a node using its position property

Changing the visibility of a node using its hidden property

Adjusting the translucency of a node using its alpha property

Modifying the size of a node using its size property

Playing simple sounds

Colorizing a node

要使用SKAction,您只需执行两个步骤:创建要执行的操作,并告诉要执行该操作的节点来运行操作。 下面是一个例子,它在两秒的时间内将节点移动到场景的右侧:

var moveRightAction = SKAction.moveToX(size.width, duration: 2.0)

sampleNode.runAction(moveRightAction)

SKActions的另一个非常酷的功能是将动作链接在一起的能力。 看看这个例子:

var moveRightAction = SKAction.moveToX(size.width, duration: 2.0)

var moveLeftAction = SKAction.moveToX(0.0, duration: 2.0)

var actionSequence = SKAction.sequence([moveRightAction, moveLeftAction])

sampleNode.runAction(actionSequence)

这部分代码将首先将节点移动到场景的右侧,然后当该动作完成时,它将把同一节点移动到场景的左侧。 SKAction的另一个优点是可以重复一个动作。 看看这个片段:

let moveLeftAction = SKAction.moveTo(x: 0.0, duration: 2.0)

let moveRightAction = SKAction.moveTo(x: size.width, duration: 2.0)

let actionSequence = SKAction.sequence([moveLeftAction, moveRightAction])

let moveAction = SKAction.repeatForever(actionSequence)

sampleNode.runAction(moveActionSequence)

在这里你看到前面的例子,但是现在有另一个action,repeatForever,它将永远运行moveAction。 这就是所有这一切 - 一行使所有以前的行动永远运行。

虽然这是一组简单的SKAction示例,但您可以如何利用游戏中的操作几乎没有限制。 在下面的章节中,我们向您展示如何使用相同的代码来在场景中来回移动节点集合,同时还通过一组纹理旋转节点,从而给出节点旋转的错觉。注意:在继续之前,请查看所有用于创建SKA的方法。 请注意,所有这些方法都是类方法。 这是用来创建所有SKActions的模式。 目前没有SKAction类的扩展。

使用操作在场景中移动节点

在上一节中,我们告诉过你,我们将向你展示如何在游戏场景中来回移动节点集合。 你已经看到了如何使用moveToX,sequence和repeatForever动作来做到这一点。

将在场景中移动的节点是新节点。 如果你打开精灵。 atlas文件夹中,您将看到多个BlackHoleX.png图像。 选择列表中的第一个图像BlackHole0.png。 它应该如图5-2所示。

图5-2 BlackHole节点

这个节点的目的是代表在整个场景中来回移动的黑洞。 如果玩家节点接触到这个节点,玩家将停止对物理世界的反应,然后慢慢地死亡。

要将这个新节点添加到场景中,您需要创建一个新的SKSpriteNode实例并将其传递给BlackHole0图像。 您可以通过以下方法找到代码,名为addBlackHolesToForeground():

func addBlackHolesToForeground() {

\376let blackHoleNode = SKSpriteNode(imageNamed: 'BlackHole0')

\376blackHoleNode.position = CGPoint(x: size.width - 80.0, y: 600.0)

\376blackHoleNode.physicsBody =

\376\377SKPhysicsBody(circleOfRadius: blackHoleNode.size.width / 2)

\376blackHoleNode.physicsBody?.isDynamic = false

\376blackHoleNode.name = 'BLACK_HOLE'

\376foregroundNode.addChild(blackHoleNode)

}

你以前看过所有这些代码。 它首先使用名为BlackHole0的图像创建blackHoleNode的新实例。 然后它将节点添加到场景中,设置节点的physicsBody的属性,并命名节点BLACK_HOLE。 继续并在addOrbsToForeground()方法之后立即添加此代码。 然后在调用addOrbsToForeground()方法之前添加一个调用来调用addBlackHolesToForeground()方法。

完成这些更改后,再次运行该应用程序。 这一次你会看到blackHoleNode挂起并在播放器节点的右侧

全文共16926字,剩余内容已隐藏,支付完成后下载完整资料


资料编号:[15514],资料为PDF文档或Word文档,PDF文档可免费转换为Word

原文和译文剩余内容已隐藏,您需要先支付 30元 才能查看原文和译文全部内容!立即支付

以上是毕业论文外文翻译,课题毕业论文、任务书、文献综述、开题报告、程序设计、图纸设计等资料可联系客服协助查找。