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

Anatomy of game projects

In this section we will see the basics of a game project. This includes understanding the basic architecture and work flow of game projects. Here we will learn about the scene and layers and their importance in games.

Getting ready

Complete game development is dependent on three core components: scenes, nodes, and sprites mentioned earlier. We need to have a command over these components to effectively start on game development.

How to do it...

Internally, the life cycle is executed as per the scenes—nodes are added and actions applied on these nodes. It also includes attaching some physics bodies to the nodes, support for cropping, applying animation and effects to all or a part of the content, detecting forces and collision, drawing in OpenGL, and many more things.

Apart from all this, there is an overridden update method in SKScene, which is called for each frame of the game with the current time interval as a parameter. There you can add your actual game logic specifying what to do at what time and many more things as it is called by every frame that is rendered.

For an example, we can track the difference in time between the current time and the last updated time.

  1. As the current time interval is received in the update method, define the properties for difference in time and last updated time.
    @property (nonatomic, assign) NSTimeInterval lastUpdatedTime;
    @property (nonatomic, assign) NSTimeInterval    diffTime;
  2. Now calculate difference in time by subtracting the last updated time from the current time and updating the lastUpdatedTime to the current time.
    self.diffTime = currentTime - self.lastUpdatedTime;    
    self.lastUpdatedTime = currentTime;
  3. At last the update method looks like this:
    - (void)update:(CFTimeInterval)currentTime
    {
        /* Called before each frame is rendered */
        self.diffTime = currentTime - self.lastUpdatedTime;    
        self.lastUpdatedTime = currentTime;
    }

Now this is the place where we are going add our maximum game logic—all the adding, removing, animating, updating all nodes, sprites, and actions will take place inside this method. We can also take the help of currentTime to maintain some timers simply using float variables (updating them by the diffTime and firing time events whenever required according to our game design or logic).

How it works...

All that we see running on the screen are just frames added by a time interval, which is driven by a SKScene added as a child on the SKView that serves as the main scene of the game.

As shown in the following diagram, a frame circle is present, which depicts the execution cycle of the game project for each frame:

How it works...

Some of the methods from the preceding diagram are explained as follows:

  • An update method of SKScene is called where we can add, remove, animate, and update different kinds of nodes and actions.
  • SKScene evaluates its actions that are running for the current frame following some life cycle calls such as didEvaluateActions.
  • SKScene has its own physics simulation, so if some bodies are added to it, the physics simulation is also evaluated such as collision detection, applying forces, and so on.
  • All the methods mentioned earlier contribute to the final rendering of SKView, which is displayed as a frame to the user. Hence, regular running of these frames makes the game appear as an environment.
主站蜘蛛池模板: 霍林郭勒市| 九寨沟县| 巴马| 铁力市| 西吉县| 牡丹江市| 朝阳县| 自治县| 柳河县| 温泉县| 沿河| 商城县| 张家口市| 大丰市| 平度市| 柳江县| 永定县| 八宿县| 靖安县| 阿克陶县| 射洪县| 阿拉善左旗| 贺兰县| 江口县| 无棣县| 湄潭县| 洱源县| 玉龙| 隆安县| 新巴尔虎右旗| 寿光市| 昌乐县| 长寿区| 台北县| 临夏县| 合水县| 永顺县| 寿阳县| 塔城市| 邵阳市| 永城市|