0

I have run into this strange issue. I am adding an enemy(SKSpriteNode) from inside the GameScene didMove(to view: SKView) using addChild.

The enemy has been positioned to x:100, y: 100 and it appears correctly. I also have another animation , the completion of which I am adding another enemy at the same location . But the enemy appears at a different location.The completion block is as shown below.

    holeExplosion.runHoleExplosionAction {[unowned self] in
        //self.addEnemy(enemyCount: 1, hole: holeExplosion)
        var modEnemy: ParentEnemy? = nil
        modEnemy = Enemy1(imageNamed: "Zombie1Jump1.png", healthPower:30)

        print(" \(self.scene?.position.x)  \(self.scene?.parent) ")
        self.addChild(modEnemy!)
        modEnemy!.enemySpeed = self.enemy1Speed
        modEnemy!.name = "enemy1"

        modEnemy!.position = CGPoint (x: 100 , y: 100)
        modEnemy!.zPosition = 2
    }

Any help would be appreciated. Thanks.

vaishakh
  • 176
  • 2
  • 2
  • 14
  • "self" is not the scene, self is the explosion node, you need to add to the scene with self.scene.addChild(modEnemy!) – Knight0fDragon Aug 20 '17 at 23:37
  • I checked with self.scene.add it still shows at the same location. – vaishakh Aug 20 '17 at 23:54
  • what do you mean it "shows", position is relative to the parent, not absolute. if a parent is at 10, and a child is at 20, then in reality the child is at 30, but it will only show 20 – Knight0fDragon Aug 20 '17 at 23:57
  • Yes I had already checked that perspective. I did change the position of holeExpansion to check if the parent it is referring too is the holeExpansion and not scene. But change in holeExpansion position doesn't affect the position of enemy . It is not relative to the holeExpansion. – vaishakh Aug 21 '17 at 07:01

1 Answers1

0

Your enemy class have physicsBody delegate?? maybe you can see that first because if it has it, you have to search your isDynamic property. You can't have 2 bodies in the same space when the property is equal to true.

  • You can have 2 bodies in the same spot as long as the collision mask doesn't collide – Knight0fDragon Aug 20 '17 at 23:56
  • The physics body is not added at that moment. I have just given the 2 enemies a same location for representation purpose. The one outside the closure positions fine. The one inside the closure is at totally different location – vaishakh Aug 20 '17 at 23:56