我开始使用SpriteKit和我的项目。我想创建一些从天空坠落并形成网格地图的网格,如下所示:
我用重力创造了物理世界-9.8
并且对于那些Grid,它们中的每一个都由以下构造函数创建并添加到场景中:
class NumberTile:SKSpriteNode{
convenience init(xPos:CGFloat, yPos:CGFloat){
var tileColorArray:NSArray = ["#996666","#a65959","#b34d4d","#bf4040","#cc3333","#d92626","#e61919"]
//get random color from array
var fillColor:UIColor = UIColor(rgba: tileColorArray.objectAtIndex(Int(arc4random_uniform(UInt32(tileColorArray.count)))) as! String)
var size:CGSize = CGSize(width: 38, height: 38);
self.init(color:fillColor, size:size)
self.position = CGPointMake(xPos, yPos)
self.anchorPoint = CGPointMake(0,0)
self.physicsBody = SKPhysicsBody(
rectangleOfSize: CGSize(width: 38,height: 40),
center: CGPointMake((CGFloat)(38/2),(CGFloat)(40/2)))
self.physicsBody?.mass = 1000
self.physicsBody?.friction = 1.0
self.physicsBody?.allowsRotation = false;
}
}
运行函数后从天空中删除这些网格。在这种情况下,第6列的高度与其他列不同。这是随机发生的,不是必须发生在第6列,任何想法?
//更新添加磁贴创建代码
func addTileInRow(row: NSInteger){
var xPos:CGFloat = (CGFloat)(self.counter - 1) * (38 + (2)) + (2)/2;
var yPos:CGFloat = (self.headerSprite.frame.origin.y - self.headerSprite.frame.size.height)
var tile:NumberTile = NumberTile(xPos: xPos, yPos: yPos)
addChild(tile)
}
计数器从1开始,当计数器大于7时,它将重置为1,直到创建8行。