Frame processing in a scene
//官方解读
1、The scene’supdate:method is called with the time elapsed so far in the simulation. This is the primary place to implement your own in-game simulation, including input handling, artificial intelligence, game scripting, and other similar game logic. Often, you use this method to make changes to nodes or to run actions on nodes.
以上内容就是[2d游戏引擎]iOS 2D游戏引擎框架SpriteKit入门的相关内容介绍,喜欢侠外游戏论坛的朋友可以关注我们。
上一页23456下一页作者: marital 时间: 2020-12-18 06:13
2、The scene processes actions on all the nodes in the tree. It finds any running actions and applies those changes to the tree. In practice, because of custom actions, you can also hook into the action mechanism to call your own code. You cannot directly control the order in which actions are processed or cause the scene to skip actions on certain nodes, except by removing the actions from those nodes or removing the nodes from the tree.
3、The scene’sdidEvaluateActionsmethod is called after all actions for the frame have been processed.
4、The scene simulates physics on nodes in the tree that have physics bodies. Adding physics to nodes in a scene is described inSKPhysicsBody, but the end result of simulating physics is that the position and rotation of nodes in the tree may be adjusted by the physics simulation. Your game can also receive callbacks when physics bodies come into contact with each other, seeSKPhysicsContactDelegate.
5、The scene’sdidSimulatePhysicsmethod is called after all physics for the frame has been simulated.
6、The scene applies any constraints associated with nodes in the scene. Constraints are used to establish relationships in the scene. For example, you can apply a constraint that makes sure a node is always pointed at another node, regardless of how it is moved. By using constraints, you avoid needing to write a lot of custom code in your scene handling.
7、The scene calls itsdidApplyConstraintsmethod.
8、The scene calls itsdidFinishUpdatemethod. This is your last chance to make changes to the scene.
9、The scene is rendered.
这里用到了第一步update:函数,通过CGRectIntersectsRect函数判断两个精灵是否相交,来加入相应的Action。以下是实现update:函数,并写在函数体中的。(这里也可以用碰撞引擎方案来替换)
1、判定飞镖打中小怪物
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
for (SKSpriteNode *projectile in self.projectiles) {
NSMutableArray *monstersToDelete = [[NSMutableArray alloc] init];
// 判定是否有飞镖打中小怪物
for (SKSpriteNode *monster in self.monsters) {
if (CGRectIntersectsRect(projectile.frame, monster.frame)) {
[monstersToDelete addObject:monster];
}
}
// 移除小怪物
for (SKSpriteNode *monster in monstersToDelete) {
[self.monsters removeObject:monster];
[monster removeFromParent];
int random = arc4random();
NSString *str = (random % 2 == 0) ? @"Fly162.mp3" : @"Fat242.mp3";
SKAction *soundAction = [SKAction playSoundFileNamed:str waitForCompletion:NO];
[self runAction:soundAction];
// 该函数为添加小怪物消失时的动画,一个气泡破裂的GIF,就不贴代码了
[self addcloud:CGPointMake(monster.position.x, monster.position.y)];