0
votes

I am currently working on a game where enemies get spawned from the left of the screen and move right. I want to give these enemies their own attributes (health, strength, etc). So I am working on creating a Basic_fighter class. I also have a user sniper scope that the user uses to hit the enemies. The problem I have is how to access the enemies attributes in the DidBeginContact Function since the function only returns two nodes, and not the class information. I will put my code below

Basic_Fighter_Class

import Foundation
import SpriteKit

class Basic_Fighter  {

var health = Int()
var type = SKSpriteNode()

init(sk:SKSpriteNode){

    self.type = sk
    self.health = 3

}  
} 

func spawn_enemies(){

    let enemynode = SKSpriteNode(imageNamed: "Shooter")
    enemynode.size = CGSize(width: 100, height: 40)
    enemynode.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
    enemynode.physicsBody = SKPhysicsBody(rectangleOfSize: enemynode.size)
    enemynode.physicsBody?.affectedByGravity = false
    enemynode.physicsBody?.categoryBitMask = BodyType.enemy
    enemynode.physicsBody?.contactTestBitMask = BodyType.bullet

    let enemy = Basic_Fighter(sk: enemynode)
    addChild(enemynode)
}

I am able to detect the contact made between the user scope and the enemy in the DidBeginContact function, but I do not know how to access the information of the enemy, such as its health.

1
just access the node feature of the body in the didbegincontact, this is available on multiple tutorials, then you just cast it to the class that it belongs toKnight0fDragon
did you ever figure this out?Mugs

1 Answers

0
votes

I think you can access it with enemynode.health, I'm not sure though.