官术网_书友最值得收藏!

Retrofitting the Player class for flight

We need to perform a few quick setup tasks before we can react to player input. We will remove some of our older testing code and add a physics body to the Player class.

The Beekeeper

First, clean up the old bee physics tests from the last chapter. Open GameScene.swift, find didMove, and locate the bottom two lines; one sets a mass for bee2, and the other applies an impulse to bee2. Remove these two lines:

bee2.physicsBody?.mass = 0.2 
bee2.physicsBody?.applyImpulse(CGVector(dx: -25, dy: 0))

Updating the Player class

We need to give the Player class its own update function. We want to store player-related logic in Player and we need it to run before every frame:

  1. Open Player.swift and add the following function inside Player:
    func update() { } 
  2. In GameScene.swift, add this code at the bottom of the GameScene class:
    player.update() 
            }         override func update(_ currentTime: TimeInterval) { 
    
  3. Perfect. The GameScene class will call the Player class's update function on every update.

Moving the ground

We initially placed the ground higher than necessary to make sure it displayed for all screen sizes in the previous chapter. We can now move the ground into its lower, final position since the player will soon move up and down, bringing the ground into view.

In GameScene.swift, locate the line that sets the ground.position value and change the y value from 150 to 30:

ground.position = CGPoint(x: -self.size.width * 2, y: 30) 
主站蜘蛛池模板: 林口县| 广元市| 浦县| 平远县| 资阳市| 蛟河市| 吕梁市| 邢台县| 绥江县| 金溪县| 安远县| 寿光市| 阿拉善右旗| 额济纳旗| 彰化市| 绵阳市| 渑池县| 晋州市| 垦利县| 青铜峡市| 南丰县| 临湘市| 奇台县| 黄浦区| 天门市| 建水县| 哈尔滨市| 长岭县| 寿光市| 诸暨市| 会昌县| 长葛市| 广宁县| 修文县| 克山县| 海兴县| 阳高县| 洛浦县| 泽库县| 丰都县| 秭归县|