通过更新方法添加节点

时间:2015-12-31 17:36:21

标签: ios swift methods sprite-kit

我很想在重新定位之后添加SKSpriteNode,因此我想通过Swift Sprite Kit中的update方法添加节点。

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

在重新定位平台节点之后,我想在平台上再次添加一个Enemy,但是因为我通过更新方法尝试它,它有时会在它上面添加多个节点。

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

在update方法中调用SpawnEnemy方法的任何方法,并检查它是否只被调用一次?

我的代码:

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))
    }

}

2 个答案:

答案 0 :(得分:1)

你的问题是你没有创造任何一种检查,移动平台已经产生了敌人。当然,你移动平台,但直到你的玩家移动,它会不断产生敌人。我没有看到这种情况发生在哪里,但你应该在移动平台时产生敌人,而不是在你的更新上产生

编辑:
哎呀,我现在看到了,这是一些丑陋的代码大声笑,在所有那些移动平台的ifs中,你应该产生敌人。但是,还需要检查以前的平台是否低于当前平台,而不是

编辑++:
在进一步审核并添加详细信息后,请尝试使用以下内容:

首先,给所有平台命名,#34;平台&#34;

然后

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))

        }
    }
}

问题:这是如何工作的,我只是检查一个平台是否在场景中的特定点,回收它,而不是检查每个平台并移动它的前一个

答案 1 :(得分:0)

所以你在每一帧都会产生一个敌人......那将会是太多的敌人......我仍然没有完全遵循游戏的确切架构,但这可能会帮助你。

获取delta时间以查看已经过了多少时间..并且每10秒(或其他)调用您的生成的敌人代码而不是每帧调用它

$('.element-paragraph').each(function(index) {
    var characterCount = $(this).text().length
    totalCount = totalCount + characterCount

    if (totalCount > 1200)  {
        totalCount = 0;
        var feedData = "pos" + adUnit.toString();
        var advertInsert = '<div id=' + feedData +  ' class="inline-ad big-box-300x250 border-bottom-" data-mobile-display="" data-desktop-display="" data-ad-type="' + feedData + '" style=""></div>'
        var ad_script = '<script type="text/javascript">googletag.cmd.push(function() { googletag.display("' + feedData + '"); });</script>';
        $(this).append(advertInsert)
        $(this).append(ad_script);

        adUnit++;

        if ($("#" + feedData).contents().find("iframe").find("body").length > 0) {
            console.log("Advert found for unit" + feedData)
            $("#" + feedData).css("height","350");
        }

        else {
            console.log("Advert not found for unit" + feedData)
            $("#" + feedData).css("height","0");
        }
    }
});