Xamarin 中的 SpriteKitSpriteKit in Xamarin.iOS

SpriteKit 是 Apple 的2D 图形框架,在 iOS 8 和 OS X Yosemite 中有一些有趣的新功能。SpriteKit, the 2D graphics framework from Apple, has some interesting new features in iOS 8 and OS X Yosemite. 其中包括与 SceneKit、着色器支持、照明、阴影、约束、正常地图生成和物理增强功能的集成。These include integration with SceneKit, shader support, lighting, shadows, constraints, normal map generation, and physics enhancements. 特别是,新的物理学功能使向游戏添加逼真效果变得非常简单。In particular, the new physics features make it very easy to add realistic effects to a game.

物理学主体Physics Bodies

SpriteKit 包括一个二维的硬正文物理 API。SpriteKit includes a 2D, rigid body physics API. 每个 sprite 都有一个关联的物理主体(SKPhysicsBody),用于定义物理属性(如质量和摩擦)以及物理世界中主体的几何。Every sprite has an associated physics body (SKPhysicsBody) that defines the physics properties such as mass and friction, as well as the geometry of the body in the physics world.

基于纹理创建物理主体Creating a Physics Body from a Texture

SpriteKit 现在支持从其纹理中派生子画面的物理主体。SpriteKit now supports deriving the physics body of a sprite from its texture. 这样就可以轻松地实现外观更自然的冲突。This makes it easy to implement collisions that look more natural.

例如,在以下冲突中,请注意香蕉和猴子如何在每个图像的图面上发生冲突:For example, notice in the following collision how the banana and monkey collide nearly at the surface of each image:

SpriteKit 使使用一行代码就能创建这样的物理体。SpriteKit makes creating such a physics body possible with a single line of code. 只需调用带有纹理和大小的 SKPhysicsBody.Create:子画面。PhysicsBody = SKPhysicsBody (sprite。纹理、sprite。大小);Simply call SKPhysicsBody.Create with the texture and size: sprite.PhysicsBody = SKPhysicsBody.Create (sprite.Texture, sprite.Size);

Alpha 阈值Alpha Threshold

除了直接将 PhysicsBody 属性直接设置到从纹理派生的几何以外,应用程序还可以设置和 alpha 阈值来控制如何派生几何。In addition to simply setting the PhysicsBody property directly to the geometry derived from the texture, applications can set and alpha threshold to control how the geometry is derived.

Alpha 阈值定义要在生成的物理主体中包含的像素必须包含的最小 alpha 值。The alpha threshold defines the minimum alpha value a pixel must have to be included in the resulting physics body. 例如,以下代码会产生略微不同的物理主体:For example, the following code results in a slightly different physics body:

sprite.PhysicsBody = SKPhysicsBody.Create (sprite.Texture, 0.7f, sprite.Size);

调整 alpha 阈值(如这种方式)的效果会微调上一个冲突,以便在与香蕉发生冲突时,猴子会下降:The effect of tweaking the alpha threshold like this fine-tunes the previous collision, such that the monkey falls over when colliding with the banana:

物理字段Physics Fields

SpriteKit 的另一个极佳补充是新的物理领域支持。Another great addition to SpriteKit is the new physics field support. 利用这些功能,您可以将 vortex 字段、径向重力字段和弹簧字段等一些东西命名为几个。These allow you to add things such as vortex fields, radial gravity fields and spring fields to name just a few.

物理学字段是使用 SKFieldNode 类创建的,它与任何其他 SKNode一样添加到场景中。Physics fields are created using the SKFieldNode class, which is added to a scene just like any other SKNode. SKFieldNode 上有多种用于创建不同物理字段的工厂方法。There are a variety of factory methods on SKFieldNode to create different physics fields. 可以通过调用 SKFieldNode.CreateRadialGravityField()SKFieldNode.CreateSpringField()、径向重力字段来创建弹簧字段。You can create a spring field by calling SKFieldNode.CreateSpringField(), a radial gravity field by calling SKFieldNode.CreateRadialGravityField(), and so on.

SKFieldNode 还具有属性来控制字段属性,例如字段强度、字段区域和字段强制的衰减。SKFieldNode also has properties to control field attributes such as the field strength, the field region, and the attenuation of field forces.

弹簧字段Spring Field

例如,下面的代码创建一个弹簧字段并将其添加到场景中:For example, the following code creates a spring field and adds it to the scene:

SKFieldNode fieldNode = SKFieldNode.CreateSpringField ();
fieldNode.Enabled = true;
fieldNode.Position = new PointF (Size.Width / 2, Size.Height / 2);
fieldNode.Strength = 0.5f;
fieldNode.Region = new SKRegion(Frame.Size);
AddChild (fieldNode);

然后,可以添加子画面并设置其 PhysicsBody 属性,使物理学字段影响 sprite,如以下代码在用户触摸屏幕时执行操作:You can then add sprites and set their PhysicsBody properties so that the physics field will affect the sprites, as the following code does when the user touches the screen:

public override void TouchesBegan (NSSet touches, UIEvent evt)
{
    var touch = touches.AnyObject as UITouch;
    var pt = touch.LocationInNode (this);
    var node = SKSpriteNode.FromImageNamed ("TinyBanana");
    node.PhysicsBody = SKPhysicsBody.Create (node.Texture, node.Size);
    node.PhysicsBody.AffectedByGravity = false;
    node.PhysicsBody.AllowsRotation = true;
    node.PhysicsBody.Mass = 0.03f;
    node.Position = pt;
    AddChild (node);
}

这会导致 bananas 有,如字段节点周围的弹簧:This causes the bananas to oscillate like a spring around the field node:

径向重力字段Radial Gravity Field

添加不同的字段类似。Adding a different field is similar. 例如,以下代码将创建一个径向重力字段:For instance, the following code creates a radial gravity field:

SKFieldNode fieldNode = SKFieldNode.CreateRadialGravityField ();
fieldNode.Enabled = true;
fieldNode.Position = new PointF (Size.Width / 2, Size.Height / 2);
fieldNode.Strength = 10.0f;
fieldNode.Falloff = 1.0f;

这会产生一个不同的 "强制" 字段,其中 bananas 将 radially 有关字段的请求:This results in a different force field, where the bananas are pulled radially about the field: