如何使用SpriteKit将SKSpriteNode的物理主体大小调整为Xcode中的图像 [英] How to Fit the Physics Body Size of an SKSpriteNode to its Image in Xcode Using SpriteKit

查看:202
本文介绍了如何使用SpriteKit将SKSpriteNode的物理主体大小调整为Xcode中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我不确定这是否可行,但我正在努力使物理机构适应我的形象。很多人都告诉我,iOS App Store上我的游戏角色的命中箱(StreakDash - 去下载它!)效果不好,因为它让游戏变得更加困难(即使角色也遇到了障碍物)似乎甚至没有触及它)。这是因为hitbox是矩形的,而我的角色有一个奇怪的形状。有什么方法可以解决这个问题吗?我想到了一些没有完全奏效的方法(例如,试图将实际的框架/画布形状从矩形改为其他形状,试图改变撞击箱的形状/大小,试图改变图像等)。如果我能得到关于我是否应该首先改变命中箱的建议(这是一种让人们想要继续玩或停止的愤怒引发游戏的类型),那将是很棒的。但是找到解决问题的方法将是最好的!

So I'm not sure if this is possible, but I am trying to adapt the physics body to go with my image. Many people have told me that the hitbox for the character in my game on the iOS App Store (StreakDash - go download it!) does not work well, since it makes the game a lot harder (the hitbox hits an obstacle even though the character doesn't appear to even be touching it). This is due to the fact that the hitboxes are rectangular, whereas my character has a strange shape. Are there any ways to go around this? I thought of some ways that didn't completely work (e.g. trying to change the actual frame/canvas shape from rectangular to something else, trying to change the hitbox shape/size, trying to alter the image, etc.). It would be great if I could have advice on whether or not I should even change the hitbox in the first place (is it the type of rage-inducing game that makes people want to keep playing or stop?). But finding a way to solve the problem would be best!

这是我的角色及其hitbox的图片:

Here is a picture of my character with its hitbox:

这是只是SKSpriteNode和物理体的一些基本代码:

Here is just some basic code of with the SKSpriteNode and physics body:

 sprite = [SKSpriteNode spriteNodeWithImageNamed:@"stick"];
 sprite.size = CGSizeMake(self.frame.size.width/6.31, self.frame.size.height/3.2);
 sprite.physicsBody =[SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake (sprite.size.width, sprite.size.height)];
 sprite.position = CGPointMake(self.frame.size.width/5.7, self.frame.size.height/2.9);
 sprite.physicsBody.categoryBitMask = personCategory;
 sprite.physicsBody.contactTestBitMask = lineCategory;
 sprite.physicsBody.dynamic = NO;
 sprite.physicsBody.collisionBitMask = 0;
 sprite.physicsBody.usesPreciseCollisionDetection = YES;


推荐答案

我建议用多边形来表示物理体。你有两种方法可以改善 bodyWithRectangleOfSize

I suggest to represent the physic body with a polygon shape. You have two ways to improve bodyWithRectangleOfSize:


  1. 懒惰的方法是使用< a href =https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/clm/SKPhysicsBody/bodyWithTexture:size:\"rel = nofollow> bodyWithTexture:size:将根据纹理内容创建物理实体。但正如Apple建议的那样,你的身体越复杂,正确模拟的工作就越多。您可能希望在精度和性能之间进行权衡。

  2. 更合适的方法是用多边形来表示精灵的边界。请参阅 bodyWithPolygonFromPath:。在线有一些工具可以在用户界面中生成路径代码。这是一个: SKPhysicsBody Path Generator (小心偏移和锚点)。如果您知道自己生成 CGMutablePathRef 代码的方法,那么更容易适应您的情况。

  1. The Lazy way is to use bodyWithTexture:size: which will create a physics body from the contents of a texture. But as Apple suggested, the more complex your physic body is , the more work to be properly simulated. You may want to make a tradeoff between precision and performance.
  2. The more proper way is to represent the bounding of your sprite with a convex polygon shape. See bodyWithPolygonFromPath:. There are some tools online to generate the path code in user interface. Here is the one: SKPhysicsBody Path Generator (be careful with the offset and anchor point). If you know the way to generate CGMutablePathRef code yourself, it will be easier to fit your situation.

这篇关于如何使用SpriteKit将SKSpriteNode的物理主体大小调整为Xcode中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆