Spritekit : how to make newly spawned enemy ships shoot bullets at player? - ios

I need help with coming up with ways to make to make the newly spawned enemy ships all shoot bullets. The problem is that I don't actually know how I am going to do that. I tried using a variable timer but, it only shoots one straight line and I can't really control the location it spawns at.
Game Picture: There are more enemies spawning
What I Tried:
var EnemyTimer = Foundation.Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(GameScene.spawnBullet3), userInfo: nil, repeats: true)
Also Tried:
}
func spawnBullet3(){
let Bullet = SKSpriteNode(imageNamed: "eBullet.png")
Bullet.setScale(1)
Bullet.zPosition = -1
Bullet.position = CGPoint(x: enemy.position.x, y: enemy.position.y)
let action = SKAction.moveTo(y: self.size.height + 100, duration: 0.5)
let actionDone = SKAction.removeFromParent()
Bullet.run(SKAction.sequence([action, actionDone]))
Bullet.physicsBody = SKPhysicsBody(rectangleOf: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategories.Bullet
Bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
Bullet.physicsBody?.contactTestBitMask = PhysicsCategories.Enemy
Bullet.physicsBody?.affectedByGravity = true
Bullet.physicsBody?.isDynamic = false
self.addChild(Bullet)
}

Well, for starters, (as LearnCocos2D explained wonderfully here) never user a Timer, performSelector:afterDelay:, or Grand Central Dispatch (GCD, ie any dispatch_... method.
Before anything, you must set up a couple of things:
First, replace add an argument to your spawnBullet3 function (If you use a custom class replace enemy:SKSpriteNode with enemy:yourCustomClass).
func spawnBullet3(enemy:SKSpriteNode){
let Bullet = SKSpriteNode(imageNamed: "eBullet.png")
Bullet.setScale(1)
Bullet.zPosition = -1
Bullet.position = CGPoint(x: enemy.position.x, y: enemy.position.y)
let action = SKAction.moveTo(y: self.size.height + 100, duration: 0.5)
let actionDone = SKAction.removeFromParent()
Bullet.run(SKAction.sequence([action, actionDone]))
Bullet.physicsBody = SKPhysicsBody(rectangleOf: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategories.Bullet
Bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
Bullet.physicsBody?.contactTestBitMask = PhysicsCategories.Enemy
Bullet.physicsBody?.affectedByGravity = true
Bullet.physicsBody?.isDynamic = false
self.addChild(Bullet)
}
Second, create a public enemies array (Again, if you use a custom class replace SKSpriteNode with your custom class name).
var enemies:[SKSpriteNode] = []
Next, every time you create a new enemy, append the enemy to the array
enemies.append(newEnemy)
Now you have two options:
Option 1: Use a SKAction
Use a SKAction timer to automate every enemy shooting every couple of seconds (Replace SKAction.waitForDuration(2.5) with SKAction.waitForDuration(yourAmountOfSeconds)).
for e in enemies
{
var wait = SKAction.waitForDuration(2.5)
var run = SKAction.runBlock {
spawnBullet3(e)
}
e.runAction(SKAction.repeatActionForever(SKAction.sequence([wait, run])))
}
Option 2: Use the update function
Use the currentTime from the update function (Replace currentTime.truncatingRemainder(dividingBy: 3) with currentTime.truncatingRemainder(dividingBy: yourAmountOfSeconds)).
override func update(_ currentTime: TimeInterval) {
[...]
if currentTime.truncatingRemainder(dividingBy: 3) == 0
{
for e in enemies
{
spawnBullet3(e)
}
}
}

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!

How to spawn images forever? Sprite-Kit

Im trying to create a dodgeball feature in my game and I need to repeatedly spawn a dodgeball and the current code I used (shown below) results in Thread 1: Exception: "Attemped to add a SKNode which already has a parent: name:'(null)' texture:[ 'dodgeball5' (500 x 500)] position:{-140.00019836425781, -55.124687194824219} scale:{1.00, 1.00} size:{30, 30} anchor:{0.5, 0.5} rotation:0.00". What am I doing incorrect?
class ClassicLevelScene: SKScene {
// Right Pointing Cannons
var rightPointingCannon: [SKReferenceNode] = []
// Dodgeball 5 Constants
var dodgeball5 = SKSpriteNode(imageNamed: "dodgeball5")
// Dodgeball SKActions
var dodgeballRepeat = SKAction()
let dodgeballMoveLeft = SKAction.moveBy(x: -400, y: 0, duration: 0.5)
let dodgeballMoveRight = SKAction.moveBy(x: 400, y: 0, duration: 0.5)
let dodgeballMoveDone = SKAction.removeFromParent()
let dodgeballWait = SKAction.wait(forDuration: 1)
var dodgeballLeftSequence = SKAction()
var dodgeballRightSequence = SKAction()
override func didMove(to view: SKView) {
// Cannon Setup
for child in self.children {
if child.name == "rightPointingCannon" {
if let child = child as? SKReferenceNode {
rightPointingCannon.append(child)
dodgeball5.position = child.position
run(SKAction.repeatForever(
SKAction.sequence([
SKAction.run(spawnDodgeball),
SKAction.wait(forDuration: 1.0)
])
))
}
}
}
// Dodgeball Right Sequence
dodgeballRightSequence = SKAction.sequence([dodgeballMoveRight, dodgeballMoveDone, dodgeballWait])
}
func spawnDodgeball() {
dodgeball5.zPosition = -1
dodgeball5.size = CGSize(width: 30, height: 30)
addChild(dodgeball5)
dodgeball5.run(dodgeballRightSequence)
}
}
Assuming single ball and ball can go off screen:
I would actually recommend against cloning the sprite.
Simply reuse it.
Once it's off the screen, you can reset it's position and speed and display it again. No need to create lots of objects, if you only need one.
One SKNode only can have 1 parent at time.
You need to create a new SKSpriteNode or clone the dodgeball5 before to add again on spawnDodgeball()
You can use
addChild(dodgeball5.copy())
instead of
addChild(dodgeball5)
But be make sure dodgeball5 have no parent before.
Your code seems to be unnecessarily complex.
all you need to do is copy() you ball and add it to your scene
Also all of your SKActions don't need to be declared at a class level
And it's not clear why you are creating an array of cannons when only one is instantiated
class ClassicLevelScene: SKScene {
// Dodgeball 5 Constants
var dodgeball5 = SKSpriteNode(imageNamed: "dodgeball5")
override func didMove(to view: SKView) {
setupCannonAndBall()
let dodgeballMoveRight = SKAction.moveBy(x: 400, y: 0, duration: 0.5)
let dodgeballMoveDone = SKAction.removeFromParent()
let dodgeballWait = SKAction.wait(forDuration: 1)
dodgeballRightSequence = SKAction.sequence([dodgeballMoveRight, dodgeballMoveDone, dodgeballWait])
//setup sequences ^ before spawning ball
let spawn = SKAction.run(spawnDodgeball)
let wait = SKAction.wait(forDuration: 1.0)
run(SKAction.repeatForever(SKAction.sequence([spawn, wait])))
}
func setupCannonAndBall() {
if let cannonRef = SKReferenceNode(fileNamed: "rightPointingCannon") {
dodgeball5.position = cannonRef.position
dodgeball5.isHidden = true
dodgeball5.size = CGSize(width: 30, height: 30)
dodgeball5.zPosition = -1
}
}
func spawnDodgeball() {
let dodgeball = dodgeball.copy()
dodgeball.isHidden = false
addChild(dodgeball)
dodgeball.run(dodgeballRightSequence)
}
}

Change SKSpriteNode.isHidden to false when hit by raycast, otherwise set it to true

I am attempting to create a line of sight/field of view on a 2d game and am having some issues. I currently am trying a raycast as a solution. The idea is that by default all my enemy nodes have isHidden = true. When the raycast hits them it should change that value to be false and when the raycast is no longer hitting them it should change it back to true.
I've messed around with a lot of different options here. I have tried keeping track of the raycast hit variable like suggested here.
I currently have it set up in an if-else statement to handle it, but this ends up having the sprites flash in and out if the raycast is hitting and then will leave isHidden = false if it was true originally.
scene?.physicsWorld.enumerateBodies(alongRayStart: rayStart, end: rayEnd) { (body, point, normal, stop) in
let sprite = body.node as? SKSpriteNode
if body.categoryBitMask == 2 {
sprite?.isHidden = true
}else if body.categoryBitMask == 1 {
} else { sprite?.isHidden = true }
}
I have the above code as part of a function that I am then calling in the update() function.
I expect the sprite to have the value of isHidden = false only when currently being hit by the raycast. Unfortunately it isn't working that way.
I would first have all my enemies in a special node container in the scene, then before casting a ray, reset the enemies isHidden property by using a forloop.
enemyContainer.children.forEach{$0.isHidden = false}
scene?.physicsWorld.enumerateBodies(alongRayStart: rayStart, end: rayEnd) { (body, point, normal, stop) in
if let sprite = body.node as? SKSpriteNode,sprite.categoryBitMask & ~0b1 \\Replace 0b1 with your category bit mask constant if you created one
sprite.isHidden = true
}
}
This will allow you to immediately figure out if a light is touching an enemy, as opposed to E.Coms answer, which will only tell you when an enemy is hit during the physics update phase
if you do not want your enemies in a special container, then you can create an array and store only the ones that were touched the last time the ray was cast
//Global to class
var nodesInLight = [SKNode]()
.......
//in func
nodesInLight.forEach{$0.isHidden = false}
nodesInLight = [SKNode]() //Allow ARC to remove the previous container, instead of calling clear on it
scene?.physicsWorld.enumerateBodies(alongRayStart: rayStart, end: rayEnd) { (body, point, normal, stop) in
if let sprite = body.node as? SKSpriteNode,sprite.categoryBitMask & ~0b1 \\Replace 0b1 with your category bit mask constant if you created one
sprite.isHidden = true
nodesInLight.append(sprite)
}
}
An advance technique I would do is create an array of weak references, this way you do not accidentally create a retain cycle
In this case, you may give the ray a real physicsbody, and implement the contact delegate. In the DidBegin func, to turn the enemy on. and DidEnd func, turn the enemy off.
func didBegin(_ contact: SKPhysicsContact) {
print("begin")
}
func didEnd(_ contact: SKPhysicsContact) {
print("end")
}
override func didMove(to view: SKView) {
view.showsPhysics = true
let ray = SKShapeNode()
ray.path = UIBezierPath(rect: CGRect(x: 0, y: 0, width: 1000, height: 20)).cgPath
ray.fillColor = UIColor.red
addChild(ray)
ray.physicsBody = SKPhysicsBody(rectangleOf:(ray.path?.boundingBoxOfPath)!.size)
ray.physicsBody?.affectedByGravity = false
ray.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(Float.pi * 2), duration: 3)))
ray.physicsBody?.contactTestBitMask = 1
ray.physicsBody?.collisionBitMask = 0
let enemy = SKSpriteNode(color: UIColor.green, size: CGSize.init(width: 100, height: 100))
enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.frame.size)
enemy.physicsBody?.affectedByGravity = false
enemy.position = CGPoint(x: 200, y: 200)
enemy.physicsBody?.contactTestBitMask = 1
enemy.physicsBody?.collisionBitMask = 0
addChild(enemy)
enemy.isHidden = true
physicsWorld.contactDelegate = self

Collision Detection In Sprite Kit Swift

This is my first time making a game in iOS so naturally I came across a couple of problems. I've managed to solve them but now that it comes to the collision part between mario and the pipes I'm having some trouble:
What I would like to do:
When mario collides with the pipes, he will have to jump over them to get to the other side. If he does not then the pipes will continue to 'push' him to the edge of the screen where the game will end.
What actually happens:
No collision is detected whatsoever.
Here is the code below:
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var mario = SKSpriteNode()
enum ColliderType: UInt32 {
case Mario = 1
case Object = 2
}
func addPipes() {
let pipetexture = SKTexture(imageNamed: "pipes")
let pipes = SKSpriteNode(texture: pipetexture)
let setScale = CGFloat(arc4random_uniform(19) + 15)
pipes.setScale(setScale) //min 15, max 19
pipes.position = CGPoint(x: self.frame.maxX, y: self.frame.minY)
let movePipes = SKAction.move(by: CGVector(dx: -5 * self.frame.width, dy: 0), duration: TimeInterval(self.frame.width/100))
let removePipes = SKAction.removeFromParent()
let moveAndRemovePipes = SKAction.sequence([movePipes, removePipes])
pipes.zPosition = +1
pipes.physicsBody = SKPhysicsBody(rectangleOf: pipes.size)
pipes.physicsBody!.isDynamic = false
pipes.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
pipes.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
pipes.physicsBody!.collisionBitMask = ColliderType.Mario.rawValue
self.addChild(pipes)
pipes.run(moveAndRemovePipes)
}
func didBegin(_ contact: SKPhysicsContact) {
print("contact")
}
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
_ = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.addPipes), userInfo: nil, repeats: true)
let marioTexture = SKTexture(imageNamed: "mariorun0.png")
let marioTexture2 = SKTexture(imageNamed: "mariorun1.png")
let marioTexture3 = SKTexture(imageNamed: "mariorun2.png")
let animation = SKAction.animate(with: [marioTexture, marioTexture2, marioTexture3], timePerFrame: 0.1)
let makeMarioRun = SKAction.repeatForever(animation)
mario = SKSpriteNode(texture: marioTexture)
mario.setScale(10)
mario.position = CGPoint(x: -(self.frame.size.width/3.5), y: -(self.frame.size.height/2.8))
mario.run(makeMarioRun)
mario.physicsBody = SKPhysicsBody(rectangleOf: mario.size)
mario.physicsBody!.isDynamic = false
mario.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
mario.physicsBody!.categoryBitMask = ColliderType.Mario.rawValue
mario.physicsBody!.collisionBitMask = ColliderType.Object.rawValue
self.addChild(mario)
addPipes()
mario.zPosition = +2
}
}
I have also implemented a jump function that works perfectly. My problem is that I cannot get a collision detection between the pipes and mario.
I have tried setting the z position for both sprites to the same value without any luck.
My initial worry was that the images were to small and hence a collision would be near impossible to detect. But since I am scaling up both nodes and using the physics body of the scaled up images then there wouldn't be a problem, but that doesn't help either.
Thanks for your help in advance.
physicsBody!.isDynamic = false means that no physics activity will happen to this body. Other bodies can interact with it, but it will not interact with anything.
This makes sense for a pipe to have, because a pipe never moves, so there is no reason to be dynamic. (Your world should be moving, not the pipe, if you are doing a flappy bird-esc game)
Now for Mario, this does not make sense. Mario is a moving entity, so he will be interacting with the physics world around in. To fix your issue, you need to make him dynamic with mario.physicsBody!.isDynamic = true

How to attach particles to moving object

I've created an PowerUpTrail.sks (particle thingy)
And I'm trying to attach it to a moving object
func SpawnPowerUp(){
let PowerUp = SKSpriteNode(imageNamed: "PowerUp.png")
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
PowerUp.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height - 100)
//Physics
PowerUp.physicsBody = SKPhysicsBody(rectangleOfSize: PowerUp.size)
PowerUp.physicsBody?.categoryBitMask = PhysicsCategory.PowerUp //Sätter unikt ID för Enemy, hämtat från PhysicsCategory som vi gjorde där uppe
PowerUp.physicsBody?.contactTestBitMask = PhysicsCategory.PowerUp //Kolla om Enemy nuddar Bullet
PowerUp.physicsBody?.affectedByGravity = false
PowerUp.physicsBody?.dynamic = true
let action = SKAction.moveToY(-128, duration: 5)
let actionDone = SKAction.removeFromParent()
PowerUp.runAction(SKAction.sequence([action, actionDone]))
PowerUp.runAction(SKAction.rotateByAngle(5, duration: 5))
/* this does not work!
let PowerUpTrail = SKEmitterNode(fileNamed: "PowerUpTrail.sks")
PowerUpTrail!.targetNode = self
PowerUpTrail!.position = PowerUp.position
self.addChild(PowerUpTrail!)
*/
self.addChild(PowerUp)
}
The particlethingy spawns but it doesnt follow the object. How do I solve this?
The simplest way to make one node's position track another is to use the node hierarchy: make the particle emitter a child node of the powerup sprite.
let powerUp = SKSpriteNode(imageNamed: "PowerUp.png")
// ...
let powerUpTrail = SKEmitterNode(fileNamed: "PowerUpTrail.sks")!
powerUpTrail.targetNode = self
powerUp.addChild(powerUpTrail)
(Also note some Swift style tips: initial-cap is conventionally for type names only; use initial-lowercase for variable names. And unwrap your optional from SKEmitterNode(fileNamed:) once when you create it instead of every time you use it thereafter.)

Resources