Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过更新方法添加节点_Ios_Swift_Methods_Sprite Kit - Fatal编程技术网

Ios 通过更新方法添加节点

Ios 通过更新方法添加节点,ios,swift,methods,sprite-kit,Ios,Swift,Methods,Sprite Kit,我想在重新定位后添加SKSpriteNode,因此我想通过Swift Sprite工具包中的更新方法添加节点 有些对象(平台)沿y轴向下移动,一旦离开屏幕,它们就会再次出现在顶部(就像一个无限循环)。我在update方法中对此进行了可视化。请注意,平台并没有真正移动,而是我将相机放在向上移动的播放器节点的中心 在重新定位平台节点后,我想在平台顶部再次添加一个敌人,但是因为我通过更新方法尝试,它有时会在其顶部添加多个节点 我不能像对平台那样重新定位敌人,因为它应该是一个随机的敌人节点 有没有办法在

我想在重新定位后添加SKSpriteNode,因此我想通过Swift Sprite工具包中的更新方法添加节点

有些对象(平台)沿y轴向下移动,一旦离开屏幕,它们就会再次出现在顶部(就像一个无限循环)。我在update方法中对此进行了可视化。请注意,平台并没有真正移动,而是我将相机放在向上移动的播放器节点的中心

在重新定位平台节点后,我想在平台顶部再次添加一个敌人,但是因为我通过更新方法尝试,它有时会在其顶部添加多个节点

我不能像对平台那样重新定位敌人,因为它应该是一个随机的敌人节点

有没有办法在update方法中调用这个方法,并检查它是否只被调用过一次

我的代码:

override func didMoveToView(view: SKView) {
/* Setup your scene here */

    self.World = SKNode()
    self.World.name = "World"
    addChild(World)

    self.WorldCamera = SKNode()
    self.WorldCamera.name = "Camera"
    self.World.addChild(WorldCamera)

    ....

    SpawnPlatforms()

    SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform2.position.y + 30))
    SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform3.position.y + 30))
    SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform4.position.y + 30))
    SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform5.position.y + 30))
    SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform6.position.y + 30))
    SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform7.position.y + 30))

}

func SpawnPlatforms() {


    Platform0 = SKSpriteNode (color: SKColor.greenColor(), size: CGSize(width: self.frame.size.width * 2 , height: 25))
    Platform0.position = CGPoint(x: self.frame.size.width / 2, y: -36)
    Platform0.zPosition = 1

    Platform0.physicsBody = SKPhysicsBody(rectangleOfSize:Platform0.size)
    Platform0.physicsBody?.dynamic = false
    Platform0.physicsBody?.allowsRotation = false
    Platform0.physicsBody?.restitution = 0.0
    Platform0.physicsBody?.usesPreciseCollisionDetection = true
    Platform0.physicsBody?.velocity = CGVectorMake(0, 0)

    Platform0.physicsBody?.categoryBitMask = Platform0Category
    Platform0.physicsBody?.collisionBitMask = PlayerCategory | EnemyCategory
    Platform0.physicsBody?.contactTestBitMask = PlayerCategory | EnemyCategory

    World.addChild(Platform0)

    Platform1 = SKSpriteNode (color: SKColor.redColor(), size: CGSize(width: self.frame.size.width * 2 , height: 25))
    Platform1.position = CGPoint(x: self.frame.size.width / 2, y: Platform0.position.y + self.frame.size.height / 4.5)
    Platform1.zPosition = 1

    Platform1.physicsBody = SKPhysicsBody(rectangleOfSize:Platform1.size)
    Platform1.physicsBody?.dynamic = false
    Platform1.physicsBody?.allowsRotation = false
    Platform1.physicsBody?.restitution = 0
    Platform1.physicsBody?.usesPreciseCollisionDetection = true

    Platform1.physicsBody?.categoryBitMask = Platform1Category
    Platform1.physicsBody?.collisionBitMask = PlayerCategory | EnemyCategory
    Platform1.physicsBody?.contactTestBitMask = PlayerCategory | EnemyCategory

    World.addChild(Platform1)

    ....a.s.o. for the other 6 platform nodes
}

func SpawnEnemy(position: CGPoint!){

    let random = arc4random_uniform(4)

    switch (random) {

    case 0:

        let node1 = SpawnNode1(position)

        self.addChild(node1)

        break

    case 1:

        let node2 = SpawnNode2(position)

        self.addChild(node2)

        break

    case 2:

        let node3 = SpawnNode3(position)

        self.addChild(node3)

        break

    case 3:

        break

    default:

        break
    }
}

override func didSimulatePhysics() {

    WorldCamera.position = CGPoint(x: Player.position.x, y: Player.position.y)

    self.centerOnNode(WorldCamera!)

}

func centerOnNode(node: SKNode) {

    let cameraPositionInScene: CGPoint = WorldCamera.scene!.convertPoint(WorldCamera.position, fromNode: World)

    World.runAction(SKAction.moveTo(CGPoint(x:World.position.x , y:World.position.y - cameraPositionInScene.y), duration: 1.0))

}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

     if(Platform1.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform1.position = CGPointMake(self.frame.size.width / 2, Platform7.position.y + self.frame.size.height / 4.5)
        Platform1.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform1.position.y + 30))
    }

    if(Platform2.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform2.position = CGPointMake(self.frame.size.width / 2, Platform1.position.y + self.frame.size.height / 4.5)
        Platform2.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform2.position.y + 30))
    }

    if(Platform3.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform3.position = CGPointMake(self.frame.size.width / 2, Platform2.position.y + self.frame.size.height / 4.5)
        Platform3.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform3.position.y + 30))
    }

    if(Platform4.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform4.position = CGPointMake(self.frame.size.width / 2, Platform3.position.y + self.frame.size.height / 4.5)
        Platform4.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform4.position.y + 30))
    }

    if(Platform5.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform5.position = CGPointMake(self.frame.size.width / 2, Platform4.position.y + self.frame.size.height / 4.5)
        Platform5.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform5.position.y + 30))
    }

    if(Platform6.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform6.position = CGPointMake(self.frame.size.width / 2, Platform5.position.y + self.frame.size.height / 4.5)
        Platform6.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform6.position.y + 30))
    }

    if(Platform7.position.y < (Player.position.y - self.frame.size.height / 2)){
        Platform7.position = CGPointMake(self.frame.size.width / 2, Platform6.position.y + self.frame.size.height / 4.5)
        Platform7.color = SKColor.redColor()
        SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: Platform7.position.y + 30))
    }

}
覆盖func didMoveToView(视图:SKView){
/*在这里设置场景*/
self.World=SKNode()
self.World.name=“世界”
addChild(世界)
self.WorldCamera=SKNode()
self.WorldCamera.name=“摄像头”
self.World.addChild(WorldCamera)
....
()
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform2.position.y+30))
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform3.position.y+30))
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform4.position.y+30))
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform5.position.y+30))
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform6.position.y+30))
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform7.position.y+30))
}
func平台(){
Platform0=SKSpriteNode(颜色:SKColor.greenColor(),大小:CGSize(宽度:self.frame.size.width*2,高度:25))
平台0.position=CGPoint(x:self.frame.size.width/2,y:-36)
平台0.zPosition=1
Platform0.physicsBody=SKPhysicsBody(矩形尺寸:Platform0.size)
平台0.physicsBody?.dynamic=false
平台0.physicsBody?.allowsRotation=false
平台0.physicsBody?恢复=0.0
平台0.physicsBody?.usesPreciseCollisionDetection=true
平台0.physicsBody?.velocity=CGVectorMake(0,0)
Platform0.physicsBody?.categoryBitMask=Platform0类别
平台0.physicsBody?.collisionBitMask=PlayerCategory | EnemyCategory
平台0.physicsBody?.contactTestBitMask=PlayerCategory | EnemyCategory
World.addChild(平台0)
Platform1=SKSpriteNode(颜色:SKColor.redColor(),大小:CGSize(宽度:self.frame.size.width*2,高度:25))
Platform1.position=CGPoint(x:self.frame.size.width/2,y:Platform0.position.y+self.frame.size.height/4.5)
平台1.zPosition=1
Platform1.physicsBody=SKPhysicsBody(矩形尺寸:Platform1.size)
平台1.physicsBody?.dynamic=false
平台1.physicsBody?.allowsRotation=false
平台1.physicsBody?恢复=0
平台1.physicsBody?.usesPreciseCollisionDetection=true
平台1.physicsBody?.categoryBitMask=平台1类别
平台1.physicsBody?.collisionBitMask=PlayerCategory | EnemyCategory
平台1.physicsBody?.contactTestBitMask=PlayerCategory | EnemyCategory
World.addChild(平台1)
..其他6个平台节点的a.s.o
}
func生成敌人(位置:CGPoint!){
设random=arc4random_均匀(4)
开关(随机){
案例0:
让节点1=繁殖节点1(位置)
self.addChild(节点1)
打破
案例1:
让node2=生成node2(位置)
self.addChild(node2)
打破
案例2:
让node3=繁殖node3(位置)
self.addChild(node3)
打破
案例3:
打破
违约:
打破
}
}
重写func didSimulatePhysics(){
WorldCamera.position=CGPoint(x:Player.position.x,y:Player.position.y)
self.centerOnNode(WorldCamera!)
}
func centerOnNode(节点:SKNode){
让CameraPositionScene:CGPoint=WorldCamera.scene!.convertPoint(WorldCamera.position,fromNode:World)
运行动作(SKAction.moveTo(CGPoint(x:World.position.x,y:World.position.y-CameraPositionScene.y),持续时间:1.0))
}
覆盖函数更新(currentTime:CFTimeInterval){
/*在渲染每个帧之前调用*/
if(平台1.position.y<(播放器位置.y-自身框架尺寸高度/2)){
Platform1.position=CGPointMake(self.frame.size.width/2,Platform7.position.y+self.frame.size.height/4.5)
Platform1.color=SKColor.redColor()
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform1.position.y+30))
}
if(平台2.position.y<(播放器位置.y-自身框架尺寸高度/2)){
Platform2.position=CGPointMake(self.frame.size.width/2,Platform1.position.y+self.frame.size.height/4.5)
Platform2.color=SKColor.redColor()
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform2.position.y+30))
}
if(平台3.position.y<(播放器位置.y-自身框架尺寸高度/2)){
Platform3.position=CGPointMake(self.frame.size.width/2,Platform2.position.y+self.frame.size.height/4.5)
Platform3.color=SKColor.redColor()
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform3.position.y+30))
}
if(平台4.position.y<(播放器位置.y-自身框架尺寸高度/2)){
Platform4.position=CGPointMake(self.frame.size.width/2,Platform3.position.y+self.frame.size.height/4.5)
Platform4.color=SKColor.redColor()
繁殖敌人(CGPoint(x:self.frame.size.width/2,y:Platform4.position.y+30))
}
if(平台5.position.y<(播放器位置.y-自身框架尺寸高度/2)){
Platform5.position=CGPointMake(self.frame.size.width/2,Platform4.position.y+self.frame.size.height/4.5)
Platform5.color=SKColor.redColor()
产卵敌人(CG点(x:sel)
// time values
var delta = NSTimeInterval(0)
var last_update_time = NSTimeInterval(0)

let spawnTimerMax = NSTimeInterval(10)
var spawnTimer = NSTimeInterval(0)

override func update(currentTime: NSTimeInterval) {
    if last_update_time == 0.0 {
        delta = 0
    } else {
        delta = currentTime - last_update_time
    }

    last_update_time = currentTime

    // ten seconds elapsed, spawn enemy and restart.
    spawnTimer += delta
    if spawnTimer >= spawnTimerMax {
        // your spawn code goes here
        spawnTimer = 0
    }
}
override func update(currentTime: CFTimeInterval) {
    for node in scene.children
    {
        if (node.name == "Platform" && !node.intersectsNode(scene) && node.position.y  < player.y - (self.frame.size.height / 4.5))
        {
            let platform = node as! SKSpriteNode
            platform.position.y += (7  * (self.frame.size.height / 4.5))
            platform.color = SKColor.redColor()
            SpawnEnemy(CGPoint(x: self.frame.size.width / 2, y: platform.position.y + 30))

        }
    }
}