我使用bodyWithPolygonFromPath来定义物理体的体积,并使用
http://dazchong.com/spritekit/
获取所需的路径。但这条路似乎不正确,我希望看到物理体路径的边界线,看看形状是否正确。
有没有办法看到物理学家的音量大纲?
我尝试了以下代码,但它不起作用。
ship = [SKSpriteNode spriteNodeWithImageNamed:@“Spaceship”];
CGFloat offsetX = ship.frame.size.width * ship.anchorPoint.x;
CGFloat offsetY = ship.frame.size.height * ship.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50 - offsetX, 110 - offsetY);
CGPathAddLineToPoint(path, NULL, 18 - offsetX, 16 - offsetY);
CGPathAddLineToPoint(path, NULL, 140 - offsetX, 15 - offsetY);
CGPathCloseSubpath(path);
SKShapeNode *yourline = [SKShapeNode node];
yourline.name = @"yourline";
yourline.path = path;
[yourline setStrokeColor:[UIColor redColor]];
[self addChild:yourline];
ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
//[ship setScale:0.5];
ship.zRotation = - M_PI / 2;
答案 0 :(得分:64)
在 ViewController.m 中找到此代码
SKView *skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
在最后一行添加
之后skView.showsPhysics = YES;
构建并运行,您应该看到所有物理主体边界线
我注意到你将shapeNode添加到self而不是spriteNode,所以请尝试以下
SKSpriteNode *ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
CGFloat offsetX = ship.frame.size.width * ship.anchorPoint.x;
CGFloat offsetY = ship.frame.size.height * ship.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 50 - offsetX, 110 - offsetY);
CGPathAddLineToPoint(path, NULL, 18 - offsetX, 16 - offsetY);
CGPathAddLineToPoint(path, NULL, 140 - offsetX, 15 - offsetY);
CGPathCloseSubpath(path);
ship.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
[self addChild:ship];
SKShapeNode *shape = [SKShapeNode node];
shape.path = path;
shape.strokeColor = [SKColor colorWithRed:1.0 green:0 blue:0 alpha:0.5];
shape.lineWidth = 1.0;
[ship addChild:shape];
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
如果您想要自定义边线
let ship = SKSpriteNode(imageNamed: "Spaceship")
let offsetX = ship.frame.size.width * ship.anchorPoint.x
let offsetY = ship.frame.size.height * ship.anchorPoint.y
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, 50 - offsetX, 110 - offsetY)
CGPathAddLineToPoint(path, nil, 18 - offsetX, 16 - offsetY)
CGPathAddLineToPoint(path, nil, 140 - offsetX, 15 - offsetY)
CGPathCloseSubpath(path)
ship.physicsBody = SKPhysicsBody(polygonFromPath: path)
addChild(ship)
let shape = SKShapeNode()
shape.path = path
shape.strokeColor = SKColor(red: 1.0, green: 0, blue: 0, alpha: 0.5)
shape.lineWidth = 1.0
addChild(shape)
已经测试过:]
祝你好运!!
答案 1 :(得分:1)
出于测试目的,我将船舶设置为SKShapeNode,暂时注释掉你的SKShapeNode,并将ship.path设置为与SKPhysicsBody相同的路径。
或者你可以使用它:
答案 2 :(得分:1)
在新的7.1测试版中,有如上所述的功能。
答案 3 :(得分:1)
我知道这有点晚了,但我想你可能会对我的新工具感兴趣,它会自动在精灵图像周围创建一条路径(所以你不必自己手动点击这些点),然后你可以调整各种设置,以更好地满足您的要求。该工具还输出Objective C和Swift程序代码,用于添加精灵物理主体的路径。希望它对某些人有帮助。感谢:
答案 4 :(得分:0)
您的形状节点代码是正确的。看起来yourline
可能会在实际船只之前添加到场景中,因此船舶精灵将被绘制在它之上,使其变得模糊。确保将yourline.zPosition
设置为最高值,或确保在[self addChild:yourline]
之后执行[self addChild:ship]
。
答案 5 :(得分:0)
这将在节点顶部添加红线形状。使用与为节点的物理主体定义的路径相同的路径。
+(void)debugPath:(CGPathRef)path onNode:(SKNode*)node {
SKShapeNode *yourline = [SKShapeNode node];
yourline.name = @"yourline";
yourline.path = path;
[yourline setStrokeColor:[UIColor redColor]];
[node addChild:yourline];
}
即。用法:
[[self class] debugPath:ship.physicsBody.path onNode:ship];
答案 6 :(得分:0)
在GameViewController.swift
添加此行
override func viewDidLoad() {
...
if let view = self.view as! SKView? {
...
}
view.showsPhysics = true // <--- add this line
}