[英]SKShapeNode Positioning
I'm poking around SpriteKit and encountered some weirdness ie i'm simply adding rectangular SKShapeNode
that should be fullscreen on iPhone 我在SpriteKit周围闲逛,遇到了一些怪异现象,即我只是添加了应该在iPhone上全屏显示的矩形
SKShapeNode
override func didMoveToView(view: SKView) {
let _box = SKShapeNode(rectOfSize: self.frame.size)
_box.strokeColor = SKColor.blueColor()
_box.fillColor = SKColor.blueColor()
_box.position = CGPointMake(200, 200)
_box.name = "box"
self.addChild(_box)
let _player = SKShapeNode(circleOfRadius: 50)
_player.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))
_player.fillColor = SKColor.redColor()
_player.strokeColor = SKColor.redColor()
_player.name = "player"
_player.physicsBody = SKPhysicsBody(circleOfRadius: 50)
_player.physicsBody?.dynamic = true
_player.physicsBody?.allowsRotation = false
self.addChild(_player)
let _ground = SKShapeNode(rectOfSize: CGSizeMake(self.frame.width, 20))
_ground.name = "ground"
_ground.position = CGPointMake(0, 0)
_ground.fillColor = SKColor.greenColor()
_ground.strokeColor = SKColor.greenColor()
_ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.width, 20))
_ground.physicsBody?.dynamic = false
self.addChild(_ground)
}
My views are initialised this way override func viewDidLoad() { super.viewDidLoad() } 我的视图以这种方式初始化,覆盖了func viewDidLoad(){super.viewDidLoad()}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
The resulting scene looks like so: screenshot 结果场景如下: 屏幕截图
All Nodes are shifted to the left-bottom. 所有节点都移到了左下角。 Basically left-bottom corner of rectangular shapes is out of bounds (i've checked that by positioning blue shape to (200, 200) instead of (0, 0) - and left-bottome edges were still out of screen frame.
基本上,矩形的左下角超出范围(我已经通过将蓝色形状定位为(200,200)而不是(0,0)进行了检查-并且左下边缘仍然不在屏幕框架之内。
Anchor point for scene is set to (0, 0) so the basically it looks like for me, that it sets position for central point of shape. 场景的锚点设置为(0,0),所以基本上对我来说,它设置形状中心点的位置。
What is a best way to define position of left-bottom corner of the Node, instead of mid-point? 定义节点左下角而不是中点的最佳方法是什么?
_box.position = CGPointMake(self.size.width*.5, self.size.height*.5)
这会将您的精灵的中心定位在屏幕的中心。
If you create a shape using a bezier path or CGPath, you can specify the position of its "anchor point," where the path's origin specifies the bottom/left corner of the path. 如果使用贝塞尔曲线路径或CGPath创建形状,则可以指定其“锚点”的位置,其中路径的原点指定路径的左下角。 For example,
例如,
UIBezierPath* bezierPath = [UIBezierPath bezierPathWithRect: CGRectMake(0, 0, 50, 50)];
SKShapeNode *shape = [SKShapeNode node];
shape.path = bezierPath.CGPath;
shape.fillColor = [SKColor blueColor];
Also, if your deployment target is iOS 8, you can create your shape node with 另外,如果您的部署目标是iOS 8,则可以使用
SKShapeNode *shape = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 50, 50)];
shape.fillColor = [SKColor blueColor];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.