double spawning (& overlapping) enemies on Spritekit - ios

I'm having trouble with an endless run game, mainly with the spawning on the two kind of the enemies that I created, sometimes happens that two enemies spawns when we call the function "startDifficultyTimer".
Basically when i call that function the game spawn 2 nodes in the same row so the player is forced to lose, I want to avoid that but I don't know how, I tried almost everything,I've tried to remove createsequentialenemy from didmove but the problem still persist, I think (as newbie) that the problem is in startDifficultTimer function because when the value reach the limit the problem vanish
I've posted the code below, sorry for the probably huge mistake but we're new in swift development game, an huge thank you all!
func createEnemy() {
let enemy: Enemy
let duration: CGFloat
switch Int(arc4random() % 100) {
case 0...70:
enemy = Enemy.createEnemy()
duration = CGFloat(Float(arc4random()%1)) + durationV
enemy.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 55, height: 37))
let enemyf = enemy.frame
let fixedx = frame.width + enemy.frame.width/2.0
let positions = [ CGPoint(x: fixedx, y: 383), CGPoint(x: fixedx, y: 447), CGPoint(x: fixedx, y: 511)]
let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
enemy.position = position
case 71...100:
enemy = Enemy.createEnemyMedium()
duration = CGFloat(Float(arc4random()%1)) + durationV
enemy.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 40, height: 70))
let enemyf = enemy.frame
let fixedx = frame.width + enemy.frame.width/2.0
let positions = [ CGPoint(x: fixedx, y: 415), CGPoint(x: fixedx, y: 479)]
let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
enemy.position = position
default:
enemy = Enemy.createEnemy()
//type = .small
duration = CGFloat(Float(arc4random()%1)) + durationV
let enemyf = enemy.frame
let fixedx = frame.width + enemy.frame.width/2.0
let positions = [ CGPoint(x: fixedx, y: 383), CGPoint(x: fixedx, y: 447), CGPoint(x: fixedx, y: 511)]
let position = positions[Int(arc4random_uniform(UInt32(positions.count)))]
let texture = SKTexture(imageNamed: "dronea1")
enemy.position = position
}
enemy.physicsBody!.isDynamic = false
enemy.physicsBody!.categoryBitMask = PhysicsCategory.Enemy
addChild(enemy)
let moveTo = SKAction.moveTo(x: 0.0, duration: TimeInterval(duration))
enemy.run(.repeatForever(.sequence([moveTo, .removeFromParent()])))
}
func createSequentialEnemies() {
// remove previous action if running. This way you can adjust the spawn duration property and call this method again and it will cancel previous action.
removeAction(forKey: spawnKey)
let spawnAction = SKAction.run(createEnemy)
let spawnDelay = SKAction.wait(forDuration: spawnDuration)
let spawnSequence = SKAction.sequence([spawnAction, spawnDelay])
run(SKAction.repeatForever(spawnSequence), withKey: spawnKey)later
}
func startDifficultyTimer() {
let difficultyTimerKey = "DifficultyTimerKey"
let action1 = SKAction.wait(forDuration: 1)
let action2 = SKAction.run { [unowned self] in
guard self.spawnDuration > 0.5 else { // set a min limit
self.removeAction(forKey: difficultyTimerKey) // if min duration has been reached than you might as well stop running this timer.
return
}
self.spawnDuration -= 0.5 // reduce by half a second
self.createSequentialEnemies() // spawn enemies again
}
let sequence = SKAction.sequence([action1, action2])
run(SKAction.repeatForever(sequence), withKey: difficultyTimerKey)
}

I think that the issue is that you are saying "stop this action" when the timer changes. But that statement doesn't know if an enemy has just been created or is just about to be created. Wherever it is in its loop you are stopping it and starting the loop over. So if it had just created an enemy and you stop the loop and start it over by generating a new enemy faster you will get two enemies in a row.
A way around this could be to run your sequence in reverse. Pause and then generate your enemy.
let spawnSequence = SKAction.sequence([spawnDelay, spawnAction])
you might get a slightly longer gap between enemies but you won't get two that span on top of each other.
Otherwise you could track the spawn time each time the last enemy is spawned and minus it from the next spawn time to put a custom wait action in between.

Related

How to add to a variable in SpriteKit

Relatively new to swift so apologies if I'm being stupid.
I'm using Xcode 13.3 beta 3
I have some code which spawns in a sprite named Monster and moves it from the right of the screen to the left at random speeds and at a random y location.
When it moves off screen it removes from parent and transitions to a loose screen.
what I want it to do when it leaves the screen is for it to add to a variable I've defined at the top of the class:
var pass = 0
here's the code for the monsters:
func addMonster() {
let monster = SKSpriteNode(imageNamed: "monster")
monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size)
monster.physicsBody?.isDynamic = true
monster.physicsBody?.categoryBitMask = PhysicsCatagory.monster
monster.physicsBody?.contactTestBitMask = PhysicsCatagory.projectile
monster.physicsBody?.collisionBitMask = PhysicsCatagory.none
let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)
monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)
addChild(monster)
let actualDuration = random(min: CGFloat(1.65), max: CGFloat(5.5))
let actionMove = SKAction.move(to: CGPoint(x: -monster.size.width/2, y: actualY), duration: TimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
let loseAction = SKAction.run() { [weak self] in
guard let `self` = self else {return}
let end = SKTransition.crossFade(withDuration: 0.5)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: end)
}
monster.run(SKAction.sequence([actionMove,loseAction, actionMoveDone]))
}
what I want is the loose action to just add +1 to the pass variable then have an if statement for when pass is greater than 3 to run the code that goes to the loose screen.
Hope that all makes sense
any help would be much appreciated!

GameplayKit: Enemy agent overshoots destination point during seek behavior

I'm trying to incorporate GameplayKit into a SpriteKit project.
Specifically, I'm trying to use GameplayKit's seek-and-avoid behavior wherein an "enemy" character chases a moving "player" character while avoiding obstacles.
I have managed to make the enemy seek the player, but the enemy's movement is weird/undesirable: The enemy's speed should adjust so that it comes to a halt at the desired end point, however its speed does not change, which makes it overshoot its target considerably.
Think of a fast-moving train: Once it gets up to speed, it's hard to stop; it wants to just keep on moving. It's the same phenomenon with the enemy character sprite. It moves like a heavy, lumbering object that can't stop quickly enough.
Here's what I'm doing (please assume that properties such as node are defined):
Configure the player character's GameplayKit stuff:
let entity1 = GKEntity()
heroAgent = GKAgent2D()
heroComponent = GKSKNodeComponent(node: node)
heroAgent.delegate = self
node.entity = entity1
heroEntity = entity1
if let comp = heroComponent {
entity1.addComponent(comp)
entity1.addComponent(heroAgent)
}
Configure the enemy character's GameplayKit stuff:
let entity = GKEntity()
let agent = GKAgent2D()
let avoid = GKGoal(toAvoid: obstaclesForThisLevel!, maxPredictionTime: 1000.0)
let pursue = GKGoal(toInterceptAgent: heroAgent, maxPredictionTime: 0.0)
nodeComponent = GKSKNodeComponent(node: node)
agent.maxSpeed = 100.0
agent.maxAcceleration = 50.0
agent.position = vector_float2(x: Float(levelData.enemies[i].x), y: Float(levelData.enemies[i].y))
agent.radius = Float(node.size.width*0.5)
agent.behavior = GKBehavior(goals: [avoid, pursue], andWeights: [100.0, 100.0])
agent.delegate = self
node.entity = entity
enemyAgent = agent
if let comp = nodeComponent {
entity.addComponent(comp)
entity.addComponent(agent)
}
nodeEntity = entity
In the SKScene's update(_:) method, set the player agent's position and call the enemy character's update(deltaTime:) method:
//Coordinate the hero agent's position with the hero's actual position. Without this, the values in agentDidUpdate() are nan.
heroAgent.position = vector_float2(x: Float(mainCharacter?.position.x ?? 0.0), y: Float(mainCharacter?.position.y ?? 0.0))
nodeComponent?.node.entity?.update(deltaTime: currentTime-lastFrameTime)
Set the enemy character's position in agentDidUpdate(_:):
func agentDidUpdate(_ agent: GKAgent) {
if let a = agent as? GKAgent2D {
nodeComponent?.node.position = CGPoint(x: CGFloat(a.position.x), y: CGFloat(a.position.y))
}
}
Question: Why is the enemy character overshooting its designated target point instead of managing its speed correctly, and how do I fix the issue?
Thank you!
I decided to only use GameplayKit for the purpose of generating a path around the obstacles (findPath(from:to:) is very helpful), and to simply move the enemy character along that path via SKAction.follow(_:asOffset:orientToPath:speed:) when I want to send the enemy back to its starting position.
So, to accomplish the "seeking" behavior, I'm just doing the following from inside my SKScene's update(_:) method:
let dx = mainCharacter.position.x - enemy.position.x
let dy = mainCharacter.position.y - enemy.position.y
let angle = atan2(dy, dx)
let vx = cos(angle) * enemySpeed
let vy = sin(angle) * enemySpeed
enemy.physicsBody?.velocity.dx = vx
enemy.physicsBody?.velocity.dy = vy
This seeking behavior does not consider obstacles, so the enemy can get kind of stuck behind an obstacle that's located between the enemy and the player. So, it's not ideal, but I think it's going to work for me because of other factors in my game.
To generate a path that does consider obstacles, for use with SKAction, I'm doing the following:
let graph = GKObstacleGraph(obstacles: obstaclesForThisLevel, bufferRadius: 0.0)
let startingPoint = GKGraphNode2D(point: vector_float2(x: Float(enemy.position.x), y: Float(enemy.position.y)))
let endPoint = GKGraphNode2D(point: vector_float2(x: Float(enemy.enemyOriginalPosition.x), y: Float(enemy.enemyOriginalPosition.y)))
graph.connectUsingObstacles(node: startingPoint)
graph.connectUsingObstacles(node: endPoint)
let path = (graph as GKGraph).findPath(from: startingPoint, to: endPoint)
if path.count >= 2 {
let realPath = GKPath(graphNodes: path, radius: 100.0)
let myPath: UIBezierPath = UIBezierPath()
for j in 1..<realPath.numPoints {
let previousPoint = realPath.float2(at: j-1)
let nextPoint = realPath.float2(at: j)
if j == 1 {
myPath.move(to: CGPoint(x: CGFloat(previousPoint.x), y: CGFloat(previousPoint.y)))
}
myPath.addLine(to: CGPoint(x: CGFloat(nextPoint.x), y: CGFloat(nextPoint.y)))
}
}
And finally:
let followPath = SKAction.follow(myPath.cgPath, asOffset: false, orientToPath: false, speed: enemySpeed)
enemy.run(followPath)

SpriteKit joint: follow the body

I've been asked to simplify this question, so that's what I'm doing.
I'm struggling in SpriteKit's physic joints (and possibly physic body properties). I tried every single subclass and many configurations but seams like nothing works or I'm doing something wrong.
I'm developing Snake game. User controls head of snake which should move at constant speed ahead and user can turn it clockwise or anticlockwise. All the remaining snake's pieces should follow the head - they should travel exactly the same path that head was some time ago.
I think for this game the Pin joint should be the answer, which anchor point is exactly in the centre between elements.
Unfortunately the result is not perfect. The structure should make the perfect circle, but it doesn't. I'm attaching the code, and gif showing the current effect. Is anyone experience enough to give me any suggestion what properties of physic body and or joints should are apply here for desired effect?
My code:
class GameScene: SKScene {
private var elements = [SKNode]()
override func didMove(to view: SKView) {
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
let dummyTurnNode = SKNode()
dummyTurnNode.position = CGPoint(x: size.width / 2 - 50, y: size.height / 2)
let dummyTurnBody = SKPhysicsBody(circleOfRadius: 1)
dummyTurnBody.isDynamic = false
dummyTurnNode.physicsBody = dummyTurnBody
addChild(dummyTurnNode)
for index in 0..<5 {
let element = SKShapeNode(circleOfRadius: 10)
let body = SKPhysicsBody(circleOfRadius: 10)
body.linearDamping = 0
// body.mass = 0
element.physicsBody = body
element.position = CGPoint(x: size.width / 2, y: size.height / 2 - 30 * CGFloat(index))
elements.append(element)
addChild(element)
let label = SKLabelNode(text: "A")
label.fontSize = 10
label.fontName = "Helvetica-Bold"
element.addChild(label)
if index == 0 {
element.fillColor = UIColor.blue()
body.velocity = CGVector(dx: 0, dy: 30)
let dummyTurnJoint = SKPhysicsJointPin.joint(withBodyA: dummyTurnBody, bodyB: body, anchor: dummyTurnNode.position)
physicsWorld.add(dummyTurnJoint)
} else {
body.linearDamping = 1
element.fillColor = UIColor.red()
let previousElement = elements[index - 1]
let connectingJoint = SKPhysicsJointPin.joint(withBodyA: previousElement.physicsBody!, bodyB: body, anchor: CGPoint(x: size.width / 2, y: size.height / 2 - 30 * CGFloat(index) + CGFloat(15)))
physicsWorld.add(connectingJoint)
}
}
}
override func update(_ currentTime: TimeInterval) {
let head = elements.first!.physicsBody!
var velocity = head.velocity
velocity.normalize()
velocity.multiply(30)
head.velocity = velocity
}
}
extension CGVector {
var rwLength: CGFloat {
let xSq = pow(dx, 2)
let ySq = pow(dy, 2)
return sqrt(xSq + ySq)
}
mutating func normalize() {
dx /= rwLength
dy /= rwLength
}
mutating func multiply(_ factor: CGFloat) {
dx *= factor
dy *= factor
}
}
"All the remaining snake's pieces should follow the head - they should travel exactly the same path that head was some time ago."
You should note that with Physics joints you are likely going to have variance no matter what you do. Even if you have it close to perfect you'll have rounding errors under the hood making the path not exact.
If all the tail parts are equal you can also use a different approach, this is something I've done for a comet tail. Basically the idea is that you have an array of tail objects and per-frame move move the last tail-object always to the same position as the head-object. If the head-object has a higher z-position the tail is drawn below it.
If you need to keep your tail in order you could vary the approach by storing an array of head-positions (per-frame path) and then place the tail objects along that path in your per-frame update call to the snake.
See my code below for example:
These are you head-object variables:
var tails = [SKEmitterNode]()
var tailIndex = 0
In your head init function instantiate the tail objects:
for _ in 0...MAX_TAIL_INDEX
{
if let remnant = SKEmitterNode(fileNamed: "FireTail.sks")
{
p.tails.append(remnant)
}
}
Call the below per-frame:
func drawTail()
{
if tails.count > tailIndex
{
tails[tailIndex].resetSimulation()
tails[tailIndex].particleSpeed = velocity() / 4
tails[tailIndex].emissionAngle = zRotation - CGFloat(M_PI_2) // opposite direction
tails[tailIndex].position = position
tailIndex = tailIndex < MAX_TAIL_INDEX ? tailIndex + 1 : 0
}
}
The resulting effect is actually really smooth when you call it from the scene update() function.

SKAction.moveToX repeat with value change

i want to be able move the ball with pause every 20 pixels moved i tried this one but didn't do nothing, the ball stays at the point where it started it doesn't move to the end of the screen,i know i didn't put the waitForDuration , because i wanted to check if it will move or not
func spwan()
{
let ball:SKSpriteNode = SKScene(fileNamed: "Ball")?.childNodeWithName("ball") as! SKSpriteNode
ball.removeFromParent()
self.addChild(ball)
ball.name = "spriteToTrack"
ball.zPosition = 0
ball.position = CGPointMake(1950, 1000)
var num:CGFloat = ball.position.x
let a1 = SKAction.moveToX(num - 20, duration: 10)
// i want to get to -50 let a1 = SKAction.moveToX(-50 , duration: 10)
let minus = SKAction.runBlock{
num -= 20
}
let sq1 = SKAction.sequence([a1,minus])
ball.runAction(SKAction.repeatAction(sq1, count: 10)
}
The ball should move 20 pixels at least in the above code, but 20 pixels in 10 seconds might seen like a standstill. Anyways I think you've overcomplicated things quite a bit by using moveToX rather than moveBy:, so (with a bit of rejigging) you're probably better of with something like this:
func spawn() {
let x: CGFloat = 1950
let xDelta: CGFloat = -20
let xDestination: CGFloat = -50
let repeats = Int((x - xDestination)/fabs(xDelta))
let move = SKAction.moveBy(CGVectorMake(xDelta, 0), duration: 2) // made it a lot quicker to show that stuff is happening
move.timingMode = .EaseInEaseOut
let ball:SKSpriteNode = SKScene(fileNamed: "Ball")?.childNodeWithName("ball") as! SKSpriteNode
ball.removeFromParent()
ball.name = "spriteToTrack"
ball.position = CGPointMake(x, 1000)
addChild(ball)
ball.runAction(SKAction.repeatAction(move, count: repeats))
}

How would I position my "+1" animation wherever my node comes in contact with a coin in Swift?

I have this code where when my hero node gets in contact with a coin a "+1" animation runs. Right now its in the middle of the screen every time the hero node makes contact with the coins. I want the position of that animation to be wherever the hero node made contact with the coin. How would I do this? Thanks!
let addCoinsLabel = SKSpriteNode(imageNamed: "plusone")
addCoinsLabel.setScale(0.8)
addCoinsLabel.zPosition = 200
addCoinsLabel.position = CGPoint(x: CGRectGetMidX(self.frame), y: self.frame.size.height*0.7)
self.addChild(addCoinsLabel)
let actionLabelfadeIn = SKAction.fadeInWithDuration(0.3)
let actionLabelFadeOut = SKAction.fadeOutWithDuration(0.3)
let actionLabelMove = SKAction.moveBy(CGVector(dx: 0.0, dy: 100),
duration: 0.3)
let actionRemoveFromParent = SKAction.removeFromParent()
let sequence = SKAction.sequence([actionLabelfadeIn,
actionLabelMove, actionLabelFadeOut, actionRemoveFromParent])
addCoinsLabel.runAction(sequence)
addCoinsLabel.position = CGPoint(x: coin.position.x, y: coin.position.y)

Resources