Xcode 6 Swift:在屏幕的两侧放置 2 个方块?
Xcode 6 Swift: place 2 blocks on either side of the screen?
我需要在屏幕两侧的左上角和右上角放置 2 个方块。这看起来很简单,我已经做了几次尝试,但是这些块是无处可寻的。我试过同时使用 CGPoint
和 CGPointMake
并且只使用数字坐标,但是方块没有出现在 GameScene 中。
这些块稍后会向下移动到位。我在这里初始化一切:
class GameScene: SKScene {
var savior = SKSpriteNode()
var block1Texture = SKTexture()
var block2Texture = SKTexture()
var blocksMoveRemove = SKAction()
let blockGap = 150
var block1 = SKSpriteNode()
var block2 = SKSpriteNode()
这里是我需要设置方块位置的地方:
//Create blocks
block1Texture = SKTexture(imageNamed: "block1")
block2Texture = SKTexture(imageNamed: "block2")
block1 = SKSpriteNode(texture: block1Texture)
block2 = SKSpriteNode(texture: block2Texture)
block1.setScale(1)
block2.setScale(1)
block1.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.1)
block2.position = CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.1)
self.addChild(block1)
self.addChild(block2)
您需要使用 addChild:
将块添加到您的节点。
更多备注:
如果您打算覆盖变量,则不必在声明变量时对其进行初始化。相反,使用强制解包变量。
var block1 : SKSpriteNode!
如果你想要上角的方块,你的y
应该是高度乘以0.1
,而不是0.9
。
此外,您可能看不到任何内容,因为找不到图像(检查名称)或不包含可见元素。
另外,请注意 frame
属性 可能具有意外值。来自文档:
The frame is the smallest rectangle that contains the node’s content, taking into account the node’s xScale, yScale, and zRotation properties. Not all nodes contain content of their own.
我需要在屏幕两侧的左上角和右上角放置 2 个方块。这看起来很简单,我已经做了几次尝试,但是这些块是无处可寻的。我试过同时使用 CGPoint
和 CGPointMake
并且只使用数字坐标,但是方块没有出现在 GameScene 中。
这些块稍后会向下移动到位。我在这里初始化一切:
class GameScene: SKScene {
var savior = SKSpriteNode()
var block1Texture = SKTexture()
var block2Texture = SKTexture()
var blocksMoveRemove = SKAction()
let blockGap = 150
var block1 = SKSpriteNode()
var block2 = SKSpriteNode()
这里是我需要设置方块位置的地方:
//Create blocks
block1Texture = SKTexture(imageNamed: "block1")
block2Texture = SKTexture(imageNamed: "block2")
block1 = SKSpriteNode(texture: block1Texture)
block2 = SKSpriteNode(texture: block2Texture)
block1.setScale(1)
block2.setScale(1)
block1.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.1)
block2.position = CGPoint(x: self.size.width * 0.8, y: self.size.height * 0.1)
self.addChild(block1)
self.addChild(block2)
您需要使用 addChild:
将块添加到您的节点。
更多备注:
如果您打算覆盖变量,则不必在声明变量时对其进行初始化。相反,使用强制解包变量。
var block1 : SKSpriteNode!
如果你想要上角的方块,你的y
应该是高度乘以0.1
,而不是0.9
。
此外,您可能看不到任何内容,因为找不到图像(检查名称)或不包含可见元素。
另外,请注意 frame
属性 可能具有意外值。来自文档:
The frame is the smallest rectangle that contains the node’s content, taking into account the node’s xScale, yScale, and zRotation properties. Not all nodes contain content of their own.