这是NumberTile类的构造函数:
class NumberTile:SKShapeNode{
var tileColorArray:NSArray = NSArray(objects: "#996666","#a65959","#b34d4d","#bf4040","#cc3333","#d92626","#e61919") //red
init(xPos: NSInteger, yPos: NSInteger){
super.init()
var size:CGSize = CGSize(width: 40, height: 40);
self.path = CGPathCreateWithRect(CGRect(origin: CGPointZero, size:size), nil)
self.position = CGPointMake(0, (CGFloat)(yPos))
var fillColor:UIColor = UIColor(rgba: tileColorArray.objectAtIndex(Int(arc4random_uniform(UInt32(self.tileColorArray.count)))) as! String)
self.fillColor = fillColor
self.strokeColor = UIColor.blackColor()
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.frame.size, center: CGPointMake((CGFloat)(GlobalConstants.k_TILE_SIZE/2),(CGFloat)(GlobalConstants.k_TILE_SIZE/2)))
self.physicsBody?.mass = GlobalConstants.k_TILE_MASS
self.physicsBody?.friction = 1.0
self.physicsBody?.allowsRotation = false;
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我将大小硬编码为40 x 40,x位置为0
我通过以下代码将场景添加到场景中
var tile:NumberTile = NumberTile(xPos: xPos, yPos: yPos)
self.newlyCreatedTile = tile
println("tile position: \(NSStringFromCGRect(tile.frame)) xPos:\(xPos)");
println("physic body: \(tile.physicsBody)");
addChild(tile)
xPos可以忽略,因为我在构造函数中对其进行了硬编码以进行调试。
结果
tile position: {{-0.5, 373.5}, {41, 41}} xPos:0
physic body: Optional(<SKPhysicsBody> type:<Rectangle> representedObject:[<SKShapeNode> name:'(null)' accumulatedFrame:{{-0.5, 373.5}, {41, 41}}])
tile position: {{-0.5, 373.5}, {41, 41}} xPos:40
physic body: Optional(<SKPhysicsBody> type:<Rectangle> representedObject:[<SKShapeNode> name:'(null)' accumulatedFrame:{{-0.5, 373.5}, {41, 41}}])
tile position: {{-0.5, 373.5}, {41, 41}} xPos:80
physic body: Optional(<SKPhysicsBody> type:<Rectangle> representedObject:[<SKShapeNode> name:'(null)' accumulatedFrame:{{-0.5, 373.5}, {41, 41}}])
tile position: {{-0.5, 373.5}, {41, 41}} xPos:120
physic body: Optional(<SKPhysicsBody> type:<Rectangle> representedObject:[<SKShapeNode> name:'(null)' accumulatedFrame:{{-0.5, 373.5}, {41, 41}}])
为什么xPos移位-0.5并且宽度和高度增加1? 任何想法?
//更新
对于xPos,我终于搞清楚了,在构造函数中应该将原点设置为(0.5,0.5),如下所示
self.path = CGPathCreateWithRect(CGRect(origin:CGPointMake(0.5,0.5),size:size),nil)
但宽度和高度仍然不明白..
答案 0 :(得分:2)
我相信抚摸路径会增加笔画大小的形状(我猜测每边都是0.5
)这就是给你0.5
偏移量和大小+1
。将你的矩形插入一个以说明它或不设置笔触颜色