有没有办法直观地看到精灵套件的SKPhysicsbody边界线?

use*_*367 27 physics sprite sprite-kit

我使用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;
Run Code Online (Sandbox Code Playgroud)

Jul*_*oya 65

对于Objective-C和iOS <7.1

ViewController.m中找到此代码

SKView *skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
Run Code Online (Sandbox Code Playgroud)

在最后一行添加之后

skView.showsPhysics = YES;
Run Code Online (Sandbox Code Playgroud)

构建并运行,您应该看到所有物理主体边界线

我注意到你将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];
Run Code Online (Sandbox Code Playgroud)

对于Swift&iOS> = 8.0

let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true
Run Code Online (Sandbox Code Playgroud)

如果你想要一个自定义边界线

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)
Run Code Online (Sandbox Code Playgroud)

已经测试过:]

祝好运!!


jos*_*ell 5

迅速

在 中GameViewController.swift,添加这一行:

override func viewDidLoad() {
    ...
    if let view = self.view as? SKView {
        ...
        view.showsPhysics = true // <--- add this line
    }
}
Run Code Online (Sandbox Code Playgroud)