我刚刚开始使用Swift并尝试制作类似俄罗斯方块的游戏。
我有一个计时器,每10秒钟,生成一个新的框'或形状,然后向下移动。
var TimeMethod = NSTimer.scheduledTimerWithTimeInterval(10, target: self, selector: Selector("doMethod"), userInfo: nil, repeats: true)
这会调用以下函数doMethod
来调用MoveBlocks
func doMethod(){
if(true){
if(number_of_blocks != 1){
current_min = current_max + 1
}
var box_d = createBox(self.size.width/2, yinit: self.size.height/2)
makeBox(box_d)
var Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("MoveBlocks"), userInfo: nil, repeats: true)
number_of_blocks++
}
}
func MoveBlocks(){
var Sprite = childNodeWithName("BOX_"+String(current_min))
var y_min = Sprite?.position.y
for i in current_min...current_max{
var Sprite_self = childNodeWithName("BOX_"+String(i))
if(Sprite_self?.position.y < y_min){
y_min = (Sprite?.position.y)!
}
}
if(y_min > CGFloat(BOX_WIDTH/2)){
for i in current_min...current_max {
var sprite_self = childNodeWithName("BOX_"+String(i))
//var current_pos = sprite_self?.position.y
sprite_self?.position.y = (sprite_self?.position.y)! - CGFloat(BOX_WIDTH)
}
}
}
但是,当我调用它时,它似乎适用于第一个形状,它似乎将每个块向下移动BOX_WIDTH/2
。下一个形状向下移动BOX_WIDTH
,第三个形状为3/2*BOX_WIDTH
,我不太清楚为什么要这样做,并希望有人可以帮助我。