[ad_1]
I describe as StackOverflow requirements the next problem.
Summarize the issue
I’ve problem about colliding two nodes. One consists by a crowd and every individuals is a single merchandise of my crowd outlined in the identical means (included shortly simply to be clear and utilizing index “i” and “j” to create the row of the group). I needed to make disappear as soon as arrive a node (as civilian) to the underside and going to the highest and the group take away the civilian spawned alongside the trail. Really I’ve this factor and the func tells me that the colliding occurs but it surely did not ‘trigger nothing occurs and the civilian node captured by crowd it did not disappear or eliminated with the “removefromparent()”. I’ve received no error messages with my compiler, it really works for him. My scope is : detecting node civilian in the course of the path by the group and take away this one from the trail.
What I’ve tried
I attempted many issues to repair this. The very first thing is following lots of tutorials about how collidingmasks and many others.. works. I do know what they do. However what I’ve tried it was to make a invisible line for the final line of crowd of individuals simply to see if the issue is the group itself and making that if the civilian node arrives to collide the invisible line is like he was in touch with the group but it surely didin’t this impact. I adopted lots of tutorial resembling Hackingwithswift, youtube tutorials however the process for me it is clear however nothing occurs (sorry for being repetitive).
Present code
My downside is about this GameScene.sks as a result of it it only one file with all of the features.
import SpriteKit
import GameplayKit
enum CategoryMask: UInt32 {
case civilian_value = 1
case crowd_value = 2
case background_value = 0
}
enum GameState {
case prepared
case taking part in
case lifeless
}
var gameState = GameState.prepared {
didSet {
print(gameState)
}
}
class GameScene: SKScene, SKPhysicsContactDelegate {
let participant = SKSpriteNode(imageNamed: "player1")
let textureA = SKTexture(imageNamed: "player1")
let textureB = SKTexture(imageNamed: "player2")
let pause = SKSpriteNode(imageNamed: "pause-button")
let resume = SKSpriteNode(imageNamed: "pause-button")
var civilian = SKSpriteNode()
let pauseLayer = SKNode()
let gameLayer = SKNode()
weak var sceneDelegate: GameSceneDelegate?
//primary
override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x: 0.5, y: 0.5)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFrom: body)
//func for dynamic background
moveBackground(picture: ["background1", "background2", "background3", "background1"], x: 0, z: -3, period: 5, dimension: CGSize(width: 0.5, top: 1.0))
character(participant: participant)
run(SKAction.repeatForever(
SKAction.sequence([
SKAction.run(civilians),
SKAction.wait(forDuration: 3.0)])))
run(SKAction.run(crowdSpawn))
pause.title="pause"
pause.place = CGPoint(x: body.minX/1.3, y: body.minY/1.15)
pause.dimension=CGSize(width: 0.1, top: 0.1)
pause.zPosition = 4
addChild(pause)
if self.scene?.isPaused == true {
resume.title="resume"
resume.place = CGPoint(x: body.minX/1.5, y: body.minY/1.15)
resume.dimension=CGSize(width: 0.1, top: 0.1)
resume.zPosition = 12
addChild(resume)
}
}
func pauseGame() {
sceneDelegate?.gameWasPaused()
let barr = SKSpriteNode()
let barrbehind = SKSpriteNode()
let buttonresume = SKSpriteNode(imageNamed: "again")
barrbehind.title = "barrbehind"
barrbehind.zPosition = 9
barrbehind.coloration = SKColor.black
barrbehind.dimension = CGSize(width: body.width, top: body.top)
barrbehind.alpha = 0.5
self.addChild(barradietro)
barr.title = "bar"
barr.dimension = CGSize(width: 0.4, top: 0.5)
barr.coloration = SKColor.white
barr.zPosition = 10
self.addChild(barr)
buttonresume.title = "resume"
buttonresume.zPosition = 11
buttonresume.coloration = SKColor.black
buttonresume.dimension = CGSize(width: 0.1, top: 0.1)
buttonresume.alpha = 0.5
self.addChild(buttonresume)
self.scene?.isPaused = true
}
//random func (it helps for generate randomly civilians alongside the trail
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
//func to outline civilians
func civilians() {
let civilian = SKSpriteNode(imageNamed: "PV")
civilian.title = "civilian"
//posiziono il civile
civilian.place = CGPoint(x: body.dimension.width/8.0 * random(min: -1.5, max: 1.5), y: -frame.dimension.top * 0.45)
civilian.physicsBody = SKPhysicsBody(rectangleOf: civilian.dimension)
civilian.zPosition = 3
civilian.physicsBody?.categoryBitMask = CategoryMask.civilian_value.rawValue
civilian.physicsBody?.collisionBitMask = CategoryMask.crowd_value.rawValue
civilian.physicsBody?.contactTestBitMask = CategoryMask.crowd_value.rawValue
civilian.physicsBody?.isDynamic = true
//civilian dimension
civilian.dimension=CGSize(width: 0.2, top: 0.2)
//civilian motion
civilian.run(
SKAction.moveBy(x: 0.0, y: body.dimension.top + civilian.dimension.top,period: TimeInterval(1.77)))
addChild(civilian)
}
//func for the principle character
func character(participant: SKSpriteNode){
participant.place = CGPoint(x: 0, y: 0)
participant.dimension = CGSize(width: 0.2, top: 0.2)
let animation = SKAction.animate(with: [textureB,textureA], timePerFrame:0.2)
participant.place = CGPoint(x: body.midX, y: body.midY)
addChild(participant)
participant.run(SKAction.repeatForever(animation))
}
//func for generate the group
func crowdSpawn(){
var i = 0.0
var j = 0.25
var crowdRaw : Bool = true
whereas crowdRaw {
if i <= 1 {
let crowd = SKSpriteNode(imageNamed: "player1")
crowd.title = "crowd"
//posiziono il civile
crowd.dimension=CGSize(width: 0.15, top: 0.15)
crowd.place = CGPoint(x: -frame.dimension.width / 3.6 + CGFloat(i)/2 * crowd.dimension.width , y: body.dimension.top / 2 + (CGFloat(j)*2) * -crowd.dimension.top)
crowd.zPosition = 3
let animation = SKAction.animate(with: [textureB,textureA], timePerFrame:0.25)
crowd.run(SKAction.repeatForever(animation))
crowd.run(SKAction.moveBy(x: body.dimension.width / 16.0 + CGFloat(i) * crowd.dimension.width, y: 0, period: 0))
let infectedCollision = SKSpriteNode(coloration: UIColor.purple,
dimension: CGSize(width: 1, top: 0.1))
infectedCollision.physicsBody = SKPhysicsBody(rectangleOf: infectedCollision.dimension)
infectedCollision.physicsBody?.categoryBitMask = CategoryMask.crowd_value.rawValue
//collisionBitMask : qui la linea della folla non può collidere con il civilian
infectedCollision.physicsBody?.collisionBitMask = CategoryMask.civilian_value.rawValue
infectedCollision.physicsBody?.contactTestBitMask = CategoryMask.civilian_value.rawValue
infectedCollision.physicsBody?.isDynamic = true
infectedCollision.title = "infectedCollision"
infectedCollision.place = crowd.place
addChild(crowd)
addChild(infectedCollision)
i += 0.25
} else {
j += 0.25
i = 0.0
}
if j == 1 {
crowdRaw = false
}
}
}
func didBegin(_ contact: SKPhysicsContact) {
if contact.bodyA.node?.place == contact.bodyB.node?.place {
let actionMoveDone = SKAction.removeFromParent()
civilian.run(SKAction.sequence([actionMoveDone]))
}
}
//func concerning the touches
func touchDown(atPoint pos : CGPoint) {
let motion = SKAction.transfer(to: pos, period: 1.0)
// playerSprite is a SpriteKit sprite node.
participant.run(motion)
}
//func concerning the touches
override func touchesBegan(_ touches: Set<UITouch>, with occasion: UIEvent?) {
change gameState {
case .prepared:
gameState = .taking part in
case .taking part in:
for t in touches {
let location = t.location(in: self)
participant.place.x = location.x/2
for node in self.nodes(at: location){
if node.title == "civilian" {
let explode = SKAction.colorize(with: UIColor.systemBlue,colorBlendFactor: 5.0, period: 2)
let vanish = SKAction.fadeOut(withDuration: 2.0)
node.run(explode , completion: {
node.run(vanish) {
node.removeFromParent()
}
})
}else if node.title == "pause" {
pauseGame()
}else if node.title == "resume" {
self.scene?.isPaused = false
}
}
}
case .lifeless:
print("lifeless")
}
}
//perform to have totally different backgrounds in scrolling (3 backgrounds in a loop)
func moveBackground(picture: [String], x: CGFloat, z:CGFloat, period: Double, dimension: CGSize) {
for i in 0...3 {
let background = SKSpriteNode(imageNamed: picture[i])
background.place = CGPoint(x: x, y: dimension.top * CGFloat(i))
background.dimension = dimension
background.zPosition = z
let transfer = SKAction.moveBy(x: 0, y: -background.dimension.top*3, period: 0)
let again = SKAction.moveBy(x: 0, y: background.dimension.top*3, period: period)
let sequence = SKAction.sequence([move,back])
let repeatAction = SKAction.repeatForever(sequence)
addChild(background)
background.run(repeatAction)
}
}
}
[ad_2]
