- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我创建了一个名为 Map 的 SKSpriteNode
,它具有我定义的边缘路径(一些简单的多边形形状)。
我想弄清楚的是如何添加几条 other 边缘路径,这些路径将充当 Map 的内部边缘。好像整个“ map ”确实有漏洞。某种可以与Map整体作用的内部边界形状:一条边路径(如下图)
我知道有一种方法允许创建一个带有主体(一些 NSArray)的 SKPhysicsBody
,就像这样
Map.physicsBody = [SKPhysicsBody bodyWithBodies:bodiesArray];
这种方法实际上会生成我在图像中显示的内容吗?假设 bodiesArray
包含 3 个 SKSpriteNode
,每个都有使用这种方法定义的路径:
+ (SKPhysicsBody *)bodyWithEdgeChainFromPath:(CGPathRef)path
,创建这样的路径
SKSpriteNode *innerNode1 = [SKSpriteNode spriteNodeWithImageNamed:@"map"];
CGMutablePathRef innerNode1Path = CGPathCreateMutable();
CGPathMoveToPoint(mapPath, NULL, 1110, 1110);
CGPathAddLineToPoint(mapPath, NULL, <some x1>, <some y1>);
CGPathAddLineToPoint(mapPath, NULL, <some x2>, <some y2>);
CGPathAddLineToPoint(mapPath, NULL, <some x3>, <some y3>);
.
.
.
CGPathCloseSubpath(mapPath);
innerNode1.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:innerNode1Path];
[bodiesArray addObject:innerNode1];
// Repeat for other 2 nodes
我知道另一种方法是创建 3 个具有预期“孔”的位置和形状的独立节点,但我尽量避免创建超出我需要的节点。如果有人可以确认我正在尝试做的事情是正确的,或者可能提出我不知道的替代方案。
注意:如果我做的是正确的,但我遗漏了一些东西,如果有人能告诉我正确的方法来做我想做的事情(即使是一个简单的例子),我将不胜感激带有内部较小正方形的正方形会很棒)。谢谢!
编辑 1:下面是我用来尝试创建“内部边界”的代码片段。这里的问题是,虽然绘制和显示了外部矩形和内部矩形,但当我将内部矩形添加到 Map bodyWithBodies
时,它完全控制了碰撞检测,从中删除了所有接触控制外矩形外壳。当我删除 bodyWithBodies
时,它恢复正常并显示两个矩形,外部有碰撞检测(不允许我通过),而内部没有任何东西......如此接近
// 1 Create large outer shell Map
CGRect mapWithRect = CGRectMake(map.frame.origin.x + offsetX, map.frame.origin.y + offsetY, map.frame.size.width * shrinkage, map.frame.size.height * shrinkage);
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);
self.physicsWorld.contactDelegate = self;
// 2 Create smaller inner boundary
CGRect innerRect = CGRectMake(100, 100, 300, 300);
SKPhysicsBody *body = [SKPhysicsBody bodyWithEdgeLoopFromRect:innerRect];
body.categoryBitMask = wallCategory;
NSArray *bodyArray = [NSArray arrayWithObject:body];
// 3 Add bodies to main Map body
myWorld.physicsBody = [SKPhysicsBody bodyWithBodies:bodyArray];
myWorld.physicsBody.categoryBitMask = wallCategory;
if ( [[levelDict objectForKey:@"DebugBorder"] boolValue] == YES) {
// This will draw the boundaries for visual reference during testing
[self debugPath:mapWithRect];
[self debugPath:innerRect];
}
编辑 2这种方法有效..只需添加一个与外部矩形具有相同属性的新节点:
SKPhysicsBody *innerRectBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:innerRect];
innerRectBody.collisionBitMask = playerCategory;
innerRectBody.categoryBitMask = wallCategory;
SKNode *innerBoundary = [SKNode node];
innerBoundary.physicsBody = innerRectBody;
[myWorld addChild: innerBoundary];
...但我非常想要一个不需要额外节点的更简洁的解决方案..想法?
最佳答案
你在这里没有做错我来了一个例子,我用两个物理体创建了两个边缘矩形体
//使用gcd一段时间后添加物体
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addBodyA];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self addBodyB];
});
-(void)addBodyB
{
SKSpriteNode *node=[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
node.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:node.frame.size];
node.position=CGPointMake(550, 420);
node.physicsBody.restitution=1;
[self addChild:node];
}
-(void)addBodyA
{
SKSpriteNode *node=[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)];
node.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:node.frame.size];
node.position=CGPointMake(400, 420);
node.physicsBody.restitution=1;
[self addChild:node];
}
-(void)addEdgesBodies
{
SKAction *r=[SKAction rotateByAngle:1.0/60 duration:1.0/60];
SKSpriteNode *rect=[SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(300,300)];
rect.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:rect.frame];
rect.position=CGPointMake(500, 400);
[self addChild:rect];
//
SKSpriteNode *rect1=[SKSpriteNode spriteNodeWithColor:[SKColor clearColor] size:CGSizeMake(100,100)];
rect1.physicsBody=[SKPhysicsBody bodyWithEdgeLoopFromRect:rect1.frame];
rect1.position=CGPointMake(550, 450);
[self addChild:rect1];
[rect1 runAction:[SKAction repeatActionForever:r]];
}
[self addEdgesBodies];
请记住边体的 CPU 开销很低,所以在您的多边形没有那么多边之前不要担心性能。
关于ios - SpriteKit SKPhysicsBody 与内边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35191957/
我正在 Swift 中使用 SpriteKit 制作一款平台游戏,其中涉及一个跳跃的主角。然而,一旦关卡加载,玩家就会立即掉到地上。 You can see it in action here. 我使
我有一个简单的游戏玩法,功能如下: 我的目标是检测球何时击中白色部分(游戏结束场景将加载)或球何时击中灰色部分(游戏继续) 因为形状缩小并且有很多东西在进行,我认为在需要时创建 SKPhysicsBo
我正在构建一个 SpriteKit“链式 react ”游戏,其中的圆圈在“爆炸”时会变大。我正在使用 SKPhysicsBody(circleOfRadius:) 初始值设定项来为圆圈创建物理体。
我正在按照《Swift 3 游戏开发》一书的说明创建一个简单的点击拍打游戏。我有两个 SKSpriteNode,一个是玩家,另一个是静态对象,如果接触就会伤害玩家。 对玩家的伤害将通过category
我必须遵循为我的敌人制作自定义physicalBody的代码。请参阅下面的代码: let offsetX = CGFloat(Enemy.frame.size.width * Enemy.anchor
我有一个随机迷宫生成器,它开始 build 小迷宫,然后进入大型关卡。 “C”是收藏品,“T”是瓷砖。 “P”是玩家的起始位置。我在下面包含了一个示例瓦片 map 。 当我有像这里这样的 6x12 小
是否可以检测节点是否仅在一侧与另一侧发生碰撞? 例如,一只动物与另一只动物发生冲突。在这种情况下,碰撞检测方法被调用。但是当动物跳过动物并在其上“行走”时,碰撞检测不会被激活。 我将不胜感激每一个答案
我有一个播放器。他必须跳过一些街区。 但是,似乎 SKPhysicsBody(rectangleOfSize: ... ) 没有正确对齐。当角色撞到方 block 的上半部分时,它只是穿过而他不能从下
我在屏幕上有一个图像,它是一个 SKSpriteNode,上面附有一个 SKPhysicsBody。我试图通过倾斜设备在屏幕上四处移动它。我让它沿 x 轴移动,但我似乎无法让它沿 y 轴移动。 我使用
有什么简单的方法可以测试 SKPhysicsBody 是否完全包含另一个? 像 bodyA 这样的东西包含了 bodyB 的每个点,左右。 只是想在实现一些 2D 数学助手之前问你如何使用附加的“元”
我正在创建一个简单的 Sprite Kit 游戏,但是当我将 PhysicsBody 添加到我的一个 Sprite 时,它似乎处于错误的位置。我知道它在我设置的错误位置skView.showsPhys
我将使用哪种 SKPhysicsBody 主体类型来创建椭圆物理主体? 我知道我可以用直线做一条曲线,只是让它不是一个真正的椭圆,但似乎必须有某种方法来挤压一个圆或创建一个圆? 最佳答案 Create
我的目标是旋转一个圆形节点,就像一个由电机驱动的轮子,例如顺时针旋转时,由于地板的摩擦力,轮子会向右移动。 根据其他线程,旋转 SKPhysicsBody 的方法是旋转它所连接的主体。 此代码旋转 S
出于某种原因,当我在这个场景中为这个 SKSpriteNodes 创建一个 SKPhysicsBody 时,物理体看起来比它们应该的小得多。例如(请忽略蓝链): 如您所见,形状在其下方复制,但主体要高
我想知道如何创建一个像这样更复杂的 SKPhysicsyBody?我想对黑色部分进行hittest。 我用 PaintCode 将这个形状导出到 CG 矩形。 var barrierpath2Path
我在父节点 B 中有一个节点 A。 我在节点 B 上使用 setScale 来产生缩放的错觉,但是当我这样做时,节点 A 及其所有 sibling 都没有预期的物理体。 尽管 A 看起来越来越小,但它
所以我正在尝试使用两个 SKPhysicsBody 对象。我在看一些教程来帮助处理碰撞机制并处理它。 我从其中一个教程中提取了这段代码: func didBeginContact(contac
我目前正在开发一款游戏,其中我有一张圆柱形物体的图片,我用以下内容制作了一个 SKPhysicsBody: self.oval.node.physicsBody = SKPhysicsBody(tex
下面是我的“Floor.swift”类,下面基本上是一堆墙。我有来自屏幕顶部的对象,一旦 Floor 和 SKSpriteNodes 发生碰撞,我希望 SKSpriteNode 被移除。下面是我的 F
我想知道是否有一个工具可以用来在 SpriteKit 中轻松生成复杂的物理体。我想要一个具有多边形形状的基于体积的物理体。 SpriteKit 允许使用该方法创建这样的主体: + (SKPhysics
我是一名优秀的程序员,十分优秀!