有没有办法直观地看到子图形工具包的SKPhysicsbody边界线? [英] Is there a way to visually see sprite kit's SKPhysicsbody borderline?

查看:118
本文介绍了有没有办法直观地看到子图形工具包的SKPhysicsbody边界线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bodyWithPolygonFromPath定义物理物体的体积,并使用

I am using bodyWithPolygonFromPath to define the volume of the physicsbody, and used

http://dazchong.com/spritekit/

以获取所需的路径.但是该路径似乎不正确,我希望查看物理体路径的边界以查看形状是否正确.

to get the paths required. But the path does not seem correct and I wish to see the borderline of the physicsbody path to see if the shape is correct.

有什么方法可以查看物理物体的体积轮廓吗?

Is there any way to see the physicsbody's volume outline?

我尝试了以下代码,但是它不起作用.

I tried the following code, but it doesn't work.

ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

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;

推荐答案

对于Objective-C& iOS< 7.1

在您的 ViewController.m 中找到此代码

SKView *skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;

在最后一行之后添加

skView.showsPhysics = YES;

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

Build and Run and you should see all the physics Body borderline

我注意到您将shapeNode添加到self而不是spriteNode中,因此请尝试以下操作

i noticed that you add the shapeNode to self instead of the spriteNode so try the following

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];

对于Swift& iOS> = 8.0

let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
skView.showsPhysics = true

如果您想要自定义边界线

And if you want a custom borderline

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)

已经测试过:]

祝你好运!

这篇关于有没有办法直观地看到子图形工具包的SKPhysicsbody边界线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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