Randomly spawn SKNode on the x position? - swift

I have a game and I can't seem to get the node to randomly spawn on x position, it will randomly spawn but often will go off screen and the game is then useless and have to restart it:
func addEnemy () {
//enemy
let minValue = self.size.width / 8;
let maxValue = self.size.width-20;
let spawnPoint = UInt32(maxValue - minValue);
Enemy = SKSpriteNode(imageNamed: "Enemy")
Enemy.size = CGSize(width: 150, height: 200)
Enemy.position = CGPoint(x: CGFloat(arc4random_uniform(spawnPoint)), y: self.size.height)
self.addChild(Enemy)
}

let min: CGFloat = 15.0
let max: CGFloat = 200.0
let randomCGFloatBetweenMinAndMax = CGFloat(rand())/CGFloat(RAND_MAX)*(max-min)+min
or if you prefer
let randomCGFloatBetweenMinAndMax2 = CGFloat(arc4random_uniform(UInt32(max-min)) + UInt32(min))

Related

Node spacing issues on a vertical scrolling game with variable speeds in Spritekit

So I'm creating a game in Swift with Spritekit and have ran into an issue with getting my game to work. I'm still a beginner with programming so I've likely missed out on a solution to this myself.
Anyway, so the game concept is a simple arcade vertical scroller that involves a player trying to dodge platforms as it descends downward. The mechanics (so far) are a stationary player on the y axis that can move left and right along the x axis while the platforms scroll upward along with the background moving with the platforms to give a visual effect of descent. I've gotten a build working to be fully playable, but there's an issue with spawning the platforms perfectly spaced out. Here's a sketch:
Concept Image
The picture on the left what I'm trying to achieve, while the one on the right is my current and flawed method. The main issue with the one on the right, is that it uses a collision to trigger spawning which means the spawn trigger node (red line) has to be 1 pixel tall to allow for perfect spacing. If the spawn trigger node is more than 1 pixel tall, then the collision may not trigger on that the first pixel of contact and trigger the node a few pixels deep which throws off the entire spacing. Also if the spawn trigger is only 1 pixel tall, it often won't trigger unless the everything is scrolling at slow speeds.
I've tried to think of other methods to approach this but I'm at a loss. I cannot use a simple timer to spawn nodes at intervals because the speed at which the game scrolls is variable and is constantly changing by player controls. The only two other options I can think of (which I don't know how to do either) is either spawn node sets at fixed y-positions and set that on a loop, or change it so the player is actually descending downward while everything is generating around it (seems tougher and maybe unnecessary). I'm considering just rewriting my createPlatforms() method if I need to, but here's the code for that and the background anyway:
var platformGroup = Set<SKSpriteNode>()
var platformSpeed: CGFloat = 0.6 { didSet { for platforms in platformGroup { platforms.speed = platformSpeed } } }
var platformTexture: SKTexture!
var platformPhysics: SKPhysicsBody!
var platformCount = 0
var backgroundPieces: [SKSpriteNode] = [SKSpriteNode(), SKSpriteNode()]
var backgroundSpeed: CGFloat = 1.0 { didSet { for background in backgroundPieces { background.speed = backgroundSpeed } } }
var backgroundTexture: SKTexture! { didSet { for background in backgroundPieces { background.texture = backgroundTexture } } }
func createPlatforms() {
let min = CGFloat(frame.width / 12)
let max = CGFloat(frame.width / 3)
var xPosition = CGFloat.random(in: -min ... max)
if platformCount >= 20 && platformCount < 30 {
stage = 0
setTextures()
xPosition = frame.size.width * 0.125
} else if platformCount == 30 {
stage = 2
setTextures()
} else if platformCount >= 50 && platformCount < 60 {
stage = 0
setTextures()
xPosition = 184
} else if platformCount == 60 {
stage = 3
setTextures()
}
platformPhysics = SKPhysicsBody(rectangleOf: CGSize(width: platformTexture.size().width, height: platformTexture.size().height))
let platformLeft = SKSpriteNode(texture: platformTexture)
platformLeft.physicsBody = platformPhysics.copy() as? SKPhysicsBody
platformLeft.physicsBody?.isDynamic = false
platformLeft.physicsBody?.affectedByGravity = false
platformLeft.physicsBody?.collisionBitMask = 0
platformLeft.scale(to: CGSize(width: platformLeft.size.width * 3, height: platformLeft.size.height * 3))
platformLeft.zPosition = 20
platformLeft.name = "platform"
platformLeft.speed = platformSpeed
let platformRight = SKSpriteNode(texture: platformTexture)
platformRight.physicsBody = platformPhysics.copy() as? SKPhysicsBody
platformRight.physicsBody?.isDynamic = true
platformRight.physicsBody?.collisionBitMask = 0
platformRight.scale(to: CGSize(width: platformRight.size.width * 3, height: platformRight.size.height * 3))
platformRight.zPosition = 20
platformRight.name = "platform"
platformRight.speed = platformSpeed
let scoreNode = SKSpriteNode(color: UIColor.clear, size: CGSize(width: frame.width, height: 32))
scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size)
scoreNode.physicsBody?.isDynamic = false
scoreNode.zPosition = 100
scoreNode.name = "scoreDetect"
scoreNode.speed = platformSpeed
let platformTrigger = SKSpriteNode(color: UIColor.orange, size: CGSize(width: frame.width, height: 4))
platformTrigger.physicsBody = SKPhysicsBody(rectangleOf: platformTrigger.size)
platformTrigger.physicsBody?.isDynamic = true
platformTrigger.physicsBody?.affectedByGravity = false
platformTrigger.physicsBody?.categoryBitMask = Collisions.detect
platformTrigger.physicsBody?.contactTestBitMask = Collisions.spawn
platformTrigger.physicsBody?.collisionBitMask = 0
platformTrigger.physicsBody?.usesPreciseCollisionDetection = true
platformTrigger.zPosition = 100
platformTrigger.name = "platformTrigger"
platformTrigger.speed = platformSpeed
let newNodes: Set<SKSpriteNode> = [platformLeft, platformRight, scoreNode, platformTrigger]
for node in newNodes {
platformGroup.insert(node)
}
let yPosition = spawnNode.position.y - transitionPlatform.size().height
let gapSize: CGFloat = -frame.size.width / 6
print(gapSize)
platformLeft.position = CGPoint(x: xPosition + platformLeft.size.width - gapSize, y: -yPosition)
platformRight.position = CGPoint(x: xPosition + gapSize, y: -yPosition)
scoreNode.position = CGPoint(x: frame.midX, y: platformLeft.position.y - platformLeft.size.height / 2)
platformTrigger.position = CGPoint(x: frame.midX, y: platformLeft.position.y)
print(platformLeft.position.y)
print(platformLeft.frame.midY)
let endPosition = frame.maxY + frame.midY
let moveAction = SKAction.moveBy(x: 0, y: endPosition, duration: 7)
for node in newNodes {
let moveSequence = SKAction.sequence([
moveAction,
SKAction.removeFromParent(),
SKAction.run {
self.platformGroup.remove(node)
}
])
addChild(node)
nodeArray.append(node)
node.run(moveSequence)
}
platformCount += 1
}
func startPlatforms() {
let create = SKAction.run { [unowned self] in
self.createPlatforms()
}
run(create)
}
func createBackground() {
for i in 0 ... 1 {
let background = backgroundPieces[i]
background.texture = backgroundTexture
background.anchorPoint = CGPoint(x: 0, y: 0)
background.zPosition = -5
background.size = CGSize(width: frame.size.width, height: frame.size.width * 2.5)
background.position = CGPoint(x: 0, y: background.size.height + (-background.size.height) + (-background.size.height * CGFloat(i)))
self.addChild(background)
nodeArray.append(background)
let scrollUp = SKAction.moveBy(x: 0, y: background.size.height, duration: 5)
let scrollReset = SKAction.moveBy(x: 0, y: -background.size.height, duration: 0)
let scrollLoop = SKAction.sequence([scrollUp, scrollReset])
let scrollForever = SKAction.repeatForever(scrollLoop)
background.run(scrollForever)
}
}
Does anybody have any suggestions on how I approach this or change it so it would work perfectly everytime?

SpriteKit tunneling when user moves node

I have a question about (continuous) SpriteKit collision detection.
I have a SwiftUI/SpriteKit project in which balls fall from above and the user tries to catch the balls with a cup.
The user moves the cup horizontally by dragging their finger.
The physics work well with one exception -- the balls properly collide with each other, and with the cup, except when the cup is being moved.
When the cup is being moved by the user, the balls that had fallen into the cup tunnel right through the side of the cup.
Again, the balls collide with the cup correctly, and settle in the cup, except when the cup is moving.
EDIT: I added categoryBitMask and collisionBitMask to both the balls and the cup, as suggested in the comments. But the problem remains.
enum CollisionTypes: UInt32 {
case ball = 1
case cup = 2
}
The cup:
func cup() -> SKSpriteNode {
let screenSize = UIScreen.main.bounds
let cupNode = SKSpriteNode(imageNamed: "cup.png")
let cupNodeChild = SKSpriteNode(imageNamed: "cupOutline.png")
cupNode.position = CGPoint(x: screenSize.width*0.5, y: MainData.cupY)
cupNode.size = CGSize(width: screenSize.width * 0.25, height: screenSize.width * 0.35)
cupNode.name = "cup"
cupNode.zPosition = 1.0
cupNodeChild.size = CGSize(width: screenSize.width * 0.25, height: screenSize.width * 0.35)
cupNodeChild.name = "cupCollisionNode"
cupNodeChild.physicsBody = SKPhysicsBody(texture: cupNodeChild.texture!, size: CGSize(width: screenSize.width * 0.25, height: screenSize.width * 0.35))
cupNodeChild.physicsBody?.restitution = 0.0
cupNodeChild.physicsBody?.isDynamic = false
cupNodeChild.physicsBody?.usesPreciseCollisionDetection = true
cupNodeChild.physicsBody?.categoryBitMask = CollisionTypes.cup.rawValue
cupNodeChild.physicsBody?.collisionBitMask = CollisionTypes.ball.rawValue
cupNode.addChild(cupNodeChild)
return cupNode
}
The balls:
func initializeBalls(screenWidth: CGFloat, screenHeight: CGFloat) {
MainData.balls.removeAll()
for i in 0..<numOfBalls {
let radius = screenWidth * 0.02
let x = screenWidth * CGFloat.random(in: 0...1)
let y = screenHeight
let ballNode = SKSpriteNode(imageNamed: "ball.png")
ballNode.position = CGPoint(x: x, y: y)
ballNode.size = CGSize(width: radius*2, height: radius*2)
ballNode.name = "ball\(i)"
ballNode.physicsBody = SKPhysicsBody(circleOfRadius: radius)
ballNode.physicsBody?.usesPreciseCollisionDetection = true
ballNode.physicsBody?.mass = 5.0
ballNode.physicsBody?.isDynamic = true
ballNode.physicsBody?.categoryBitMask = CollisionTypes.ball.rawValue
ballNode.physicsBody?.collisionBitMask = CollisionTypes.cup.rawValue
ballNode.physicsBody!.contactTestBitMask = ballNode.physicsBody!.collisionBitMask
ballNode.physicsBody?.restitution = 0.1
MainData.balls.append((
x: x,
y: y,
radius: radius,
img: "ball.png",
node: ballNode
))
}
}
Here's how I move the cup, in my GameScene:
func touchMoved(toPoint pos : CGPoint) {
let action = SKAction.move(to: CGPoint(x: pos.x, y: MainData.cupY), duration: 0)
cupNode?.run(action)
}
Why is this tunneling happening, and how do I avoid it?

Why does the app lag a lot when calling functions

I am building a game that requires a function (out of several) to be called depending if a condition is true or not. The condition checks the variable every 0.0001 seconds for game purposes, but the functions are called every 4-6 seconds. I just needed the
Whenever I was testing the game on a device, I noticed that the app lags at certain times. I believe its when a function is called but I'm not sure.
This is an SKAction that calls the function that checks the condition:
let triangle = SKAction.sequence([SKAction.run(obstaclesFunc),SKAction.wait(forDuration: 0.0001)])
run(SKAction.repeatForever(triangle))
This is the condition checker:
func obstaclesFunc() {
GameScene.nextObstacle = Int(arc4random_uniform(6) + 1 )
if(GameScene.first == true){
GameScene.randomNum = 1
}
if (GameScene.pass == true){
number = GameScene.nextObstacle
print(number)
//Rectangle Call Function
if(GameScene.randomNum == 1){
sideRectangles()
//More code at bottom
}
I noticed that the biggest lag occurs when this function is called:
func sideRectangles() {
let rectangle1 = SKSpriteNode(imageNamed: "rectangleWalls")
let rectangle2 = SKSpriteNode(imageNamed: "rectangleWalls")
let rectangle3 = SKSpriteNode(imageNamed: "rectangleWalls")
let rectangleTexture = SKTexture(imageNamed: "rectangleWalls")
//Physics World
rectangle1.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle1.physicsBody?.categoryBitMask = PhysicsNumbering.rectangle_1
rectangle1.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle1.physicsBody?.affectedByGravity = false
rectangle1.physicsBody?.isDynamic = true
rectangle1.physicsBody?.collisionBitMask = 0
//Physics World
rectangle2.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle2.physicsBody?.categoryBitMask = PhysicsNumbering.rectangle_2
rectangle2.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle2.physicsBody?.affectedByGravity = false
rectangle2.physicsBody?.isDynamic = true
rectangle2.physicsBody?.collisionBitMask = 0
//Physics World
rectangle3.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle3.physicsBody?.categoryBitMask = PhysicsNumbering.rectangle_3
rectangle3.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle3.physicsBody?.affectedByGravity = false
rectangle3.physicsBody?.isDynamic = true
rectangle3.physicsBody?.collisionBitMask = 0
let moveDown = SKAction.moveTo(y: self.frame.minY - rectangle3.size.height / 2, duration: 2)
let moveDown2 = SKAction.moveTo(y: self.frame.minY - rectangle3.size.height / 2, duration: 3)
let moveDown3 = SKAction.moveTo(y: self.frame.minY - rectangle3.size.height / 2, duration: 4)
let removeNode = SKAction.removeFromParent()
rectangle1.position = CGPoint(x: self.frame.minX - rectangle3.size.height * 0.1, y: self.frame.maxY + rectangle3.size.width) //use 4.5 for the close one
rectangle2.position = CGPoint(x: self.frame.minX - rectangle3.size.width / 40 , y: self.frame.maxY + rectangle3.size.height) //use 4.5 for the close one
rectangle3.position = CGPoint(x: self.frame.midX - rectangle3.size.width / 1.55, y: self.frame.maxY + rectangle3.size.height * 1.8)
rectangle1.run(SKAction.sequence([moveDown,removeNode]))
rectangle2.run(SKAction.sequence([moveDown2,removeNode]))
rectangle3.run(SKAction.sequence([moveDown3,removeNode]))
//second set
let rectangle5 = SKSpriteNode(imageNamed: "rectangleWalls")
let rectangle6 = SKSpriteNode(imageNamed: "rectangleWalls")
let rectangle7 = SKSpriteNode(imageNamed: "rectangleWalls")
//Physics World
rectangle5.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle5.physicsBody?.categoryBitMask = PhysicsNumbering.rectangle_5
rectangle5.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle5.physicsBody?.affectedByGravity = false
rectangle5.physicsBody?.isDynamic = true
rectangle5.physicsBody?.collisionBitMask = 0
//Physics World
rectangle6.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle6.physicsBody?.categoryBitMask = PhysicsNumbering.rectangle_6
rectangle6.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle6.physicsBody?.affectedByGravity = false
rectangle6.physicsBody?.isDynamic = true
rectangle6.physicsBody?.collisionBitMask = 0
//Physics World
rectangle7.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle7.physicsBody?.categoryBitMask = PhysicsNumbering.rectangle_7
rectangle7.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle7.physicsBody?.affectedByGravity = false
rectangle7.physicsBody?.isDynamic = true
rectangle7.physicsBody?.collisionBitMask = 0
rectangle5.zPosition = 0
rectangle6.zPosition = 0
rectangle7.zPosition = 0
rectangle5.position = CGPoint(x: self.frame.maxX + rectangle3.size.height * 0.1, y: self.frame.maxY + rectangle3.size.width) //use 4.5 for the close one
rectangle6.position = CGPoint(x: self.frame.maxX + rectangle3.size.width / 40 , y: self.frame.maxY + rectangle3.size.height) //use 4.5 for the close one
rectangle7.position = CGPoint(x: self.frame.midX + rectangle3.size.width / 1.55, y: self.frame.maxY + rectangle3.size.height * 1.8)
rectangle5.run(SKAction.sequence([moveDown,removeNode]))
rectangle6.run(SKAction.sequence([moveDown2,removeNode]))
rectangle7.run(SKAction.sequence([moveDown3,removeNode]))
addChild(rectangle1)
addChild(rectangle2)
addChild(rectangle3)
addChild(rectangle5)
addChild(rectangle6)
addChild(rectangle7)
}
Can someone please help me find out what is causing this lag? I cannot seem to figure it out. Thank You in advance!
Generating physicsBodies on the fly is very expensive and that is why your game will lag everytime it tries to do that.
I suggest you create the objects and put them in an array of the obstacle and then copy the array items whenever you need to recreate them.
You have soooooo much code that is being repeated for every object, I've tried to reduce the amount of code by reusing a lot of it for every obstacle.
FYI SKAction.wait(forDuration: 0.0001) is the same as 0 so it is pointless
The code in your obstaclesFunc contains irrelevant or code not contained in your questions so I have omitted that functionality, if it is dire to your game you'll have to figure it out and put it in the generateObstacles func.
//variables needed in your scene
private var rectangles = [SKSpriteNode]()
private var moveDown: SKAction!
private var moveDown2: SKAction!
private var moveDown3: SKAction!
private var removeNode: SKAction!
//in your didMove func
//preload the obstacles into an array
createRectangles()
//in your startGame func
//this func call will start the generation of the obstacles
generateObstacles()
func createRectangles() {
let rectangle1 = createRectangle(category: PhysicsNumbering.rectangle_1)
rectangles.append(rectangle1)
let rectangle2 = createRectangle(category: PhysicsNumbering.rectangle_2)
rectangles.append(rectangle2)
let rectangle3 = createRectangle(category: PhysicsNumbering.rectangle_3)
rectangles.append(rectangle3)
let rectangle5 = createRectangle(category: PhysicsNumbering.rectangle_5)
rectangles.append(rectangle5)
let rectangle6 = createRectangle(category: PhysicsNumbering.rectangle_6)
rectangles.append(rectangle6)
let rectangle7 = createRectangle(category: PhysicsNumbering.rectangle_7)
rectangles.append(rectangle7)
let width: CGFloat = rectangle3.size.width
let height: CGFloat = rectangle3.size.height
//position the rectangles afterward creating them since they appear to be relevant to each other
rectangle1.position = CGPoint(x: frame.minX - height * 0.1, y: frame.maxY + width) //use 4.5 for the close one
rectangle2.position = CGPoint(x: frame.minX - width / 40 , y: frame.maxY + height) //use 4.5 for the close one
rectangle3.position = CGPoint(x: frame.midX - width / 1.55, y: frame.maxY + height * 1.8)
rectangle5.position = CGPoint(x: frame.maxX + height * 0.1, y: frame.maxY + width) //use 4.5 for the close one
rectangle6.position = CGPoint(x: frame.maxX + width / 40 , y: frame.maxY + height) //use 4.5 for the close one
rectangle7.position = CGPoint(x: frame.midX + width / 1.55, y: frame.maxY + height * 1.8)
//now create the actions so that they can be used again and again
createActions(height: height / 2)
}
func createActions(height: CGFloat) {
moveDown = SKAction.moveTo(y: frame.minY - height, duration: 2)
moveDown2 = SKAction.moveTo(y: frame.minY - height, duration: 3)
moveDown3 = SKAction.moveTo(y: frame.minY - height, duration: 4)
removeNode = SKAction.removeFromParent()
}
func generateObstacles() {
let randomWaitTime = Int(arc4random_uniform(6) + 1 )
self.run(.wait(forDuration: randomWaitTime) {
loadAndMoveRectangles()
//now call the generate func again to get a diff random time to wait
generateObstacles()
}
}
func loadAndMoveRectangles() {
let rectangle1 = rectangles[0].copy() as? SKSpriteNode
addChild(rectangle1)
let rectangle2 = rectangles[1].copy() as? SKSpriteNode
addChild(rectangle2)
let rectangle3 = rectangles[2].copy() as? SKSpriteNode
addChild(rectangle3)
let rectangle5 = rectangles[3].copy() as? SKSpriteNode
addChild(rectangle5)
let rectangle6 = rectangles[4].copy() as? SKSpriteNode
addChild(rectangle6)
let rectangle7 = rectangles[5].copy() as? SKSpriteNode
addChild(rectangle7)
rectangle1.run(.sequence([moveDown, removeNode]))
rectangle2.run(.sequence([moveDown2, removeNode]))
rectangle3.run(.sequence([moveDown3, removeNode]))
rectangle5.run(.sequence([moveDown, removeNode]))
rectangle6.run(.sequence([moveDown2, removeNode]))
rectangle7.run(.sequence([moveDown3, removeNode]))
}
func createRectangle(category: UInt32) -> SKSpriteNode {
let rectangle = SKSpriteNode(imageNamed: "rectangleWalls")
rectangle.zPosition = 0
rectangle.physicsBody = SKPhysicsBody(texture: rectangleTexture, size: CGSize(width: rectangle1.size.width - 20, height: rectangle1.size.height - 20))
rectangle.physicsBody?.categoryBitMask = category
rectangle.physicsBody?.contactTestBitMask = PhysicsNumbering.player
rectangle.physicsBody?.affectedByGravity = false
rectangle.physicsBody?.isDynamic = true
rectangle.physicsBody?.collisionBitMask = 0
return rectangle
}

Move a node based off of the randomized position of another node

I want to make it so my node (ball) always ends up resetting above my other node block1 based on where block 1 ends up. However, I could not think of any code that would help me do that. I was thinking of making a function saying something ball.position = block1 + 20 but I have no ideas of how to go about that. Thanks for any help!
Code:
func random() -> CGFloat {
return CGFloat(Float(arc4random()) / 0xFFFFFFFF)
}
func random(min: CGFloat, max: CGFloat) -> CGFloat {
return random() * (max - min) + min
}
func resetScene (){
let ball = childNode(withName: BallCategoryName) as! SKSpriteNode
ball.removeFromParent()
ball.physicsBody?.velocity = CGVector( dx: 0, dy: 0 )
ball.physicsBody?.collisionBitMask = BallCategory
ball.physicsBody?.collisionBitMask = BorderCategory | PaddleCategory
ball.zRotation = 0.0
addChild(ball)
let block1 = childNode(withName: Block1Name) as! SKSpriteNode
block1.removeFromParent()
let actualX = random(min:85, max: 300)
block1.position = CGPoint(x: actualX, y: 190)
addChild(block1)
//ball.position = CGPoint(x: , y: )
//This is the line i would like to change
canRestart = false
}
assuming you are just trying to set the Y position of your ball you can use
ball.position.y = block1.position.y + 20
or if you need to set the x and the y
ball.position = CGPoint(x: block1.position.x, y: block1.position.y + 20)

Spawn Balls random position out of the screen

I would like to bring up enemy (var enemis) from outside the screen whether the top, bottom, left and right of the screen. And these enemy have a random direction in tranversant the screen. For the moment, my code do spawning enemy out of the screen top, bottom, left and right but with one direction only and I want make a random direction
func CreationEnnemis(){
let Enemis = SKSpriteNode(imageNamed: "Meteroites.png")
let choixDeCote = arc4random() % 4 + 1
switch choixDeCote {
case 1 : //Haut
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 200
SpawnX = UInt32(MaxValue - MinValue)
SpawnX = arc4random_uniform(SpawnX)
SpawnY = UInt32(self.size.height)
break
case 2 ://Bas
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 200
SpawnX = UInt32(MaxValue - MinValue)
SpawnX = arc4random_uniform(SpawnX)
SpawnY = UInt32(self.size.height) - UInt32(self.size.height)
break
case 3 : //Gauche
let MinValue = self.size.height / 8
let MaxValue = self.size.height - 200
SpawnX = 0
SpawnY = UInt32(MaxValue - MinValue)
SpawnY = arc4random_uniform(SpawnY)
break
case 4 ://Droite
let MinValue = self.size.height / 8
let MaxValue = self.size.height - 200
SpawnX = UInt32(self.size.width)
SpawnY = UInt32(MaxValue - MinValue)
SpawnY = arc4random_uniform(SpawnY)
break
default :
break
}
Enemis.position = CGPoint(x: CGFloat(SpawnX), y: CGFloat(SpawnY))
Enemis.setScale(4)
Enemis.physicsBody = SKPhysicsBody(rectangleOfSize: Enemis.size)
Enemis.physicsBody?.affectedByGravity = false
Enemis.physicsBody?.dynamic = true
let action = SKAction.moveTo(CGPoint(x: -50,y: -10),duration: 2.5)
let actionFini = SKAction.removeFromParent()
Enemis.runAction(SKAction.sequence([action, actionFini]))
Enemis.runAction(SKAction.repeatActionForever(action))
self.addChild(Enemis)
}
This is just an example to give you an idea how you can spawn enemies at random positions and move them in random directions. I don't use Swift extensively, and this is more like just to show you at which direction you can go, and how to solve the problem. I left to you to care about Swift 2 syntax :D Also, I am currently on outdated version of Swift, so not sure what works for me, will work for you, but the logic is the same.
Here you will see how you can:
spawn a node and move it to the opposite side of a screen
move a node to the random point of the opposite side of a screen
randomize duration of spawning
create a random point along the one of the screen's borders
create a random number between two numbers
using SKAction to do all this
One thing which is important here is how to use strong reference to self inside closure. Because of my Swift version, as I said, what works for me, probably will not work for you, but the logic is the same. Read more here about strong reference cycles if interested :
Shall we always use [unowned self] inside closure in Swift
Always pass weak reference of self into block in ARC?
What is the difference between a weak reference and an unowned reference?
Here is an code example:
import SpriteKit
class GameScene:SKScene, SKPhysicsContactDelegate{
override func didMoveToView(view: SKView) {
self.physicsWorld.contactDelegate = self
createEnemies()
}
deinit{
print("deinit called")
}
func randomBetweenNumbers(firstNum: CGFloat, secondNum: CGFloat) -> CGFloat{
return CGFloat(arc4random()) / CGFloat(UINT32_MAX) * abs(firstNum - secondNum) + min(firstNum, secondNum)
}
//Helper method for spawning a point along the screen borders. This will not work for diagonal lines.
func randomPointBetween(start:CGPoint, end:CGPoint)->CGPoint{
return CGPoint(x: randomBetweenNumbers(start.x, secondNum: end.x), y: randomBetweenNumbers(start.y, secondNum: end.y))
}
func createEnemies(){
//Randomize spawning time.
//This will create a node every 0.5 +/- 0.1 seconds, means between 0.4 and 0.6 sec
let wait = SKAction .waitForDuration(0.5, withRange: 0.2)
weak var weakSelf = self //Use weakSelf to break a possible strong reference cycle
let spawn = SKAction.runBlock({
var random = arc4random() % 4 + 1
var position = CGPoint()
var moveTo = CGPoint()
var offset:CGFloat = 40
println(random)
switch random {
//Top
case 1:
position = weakSelf!.randomPointBetween(CGPoint(x: 0, y: weakSelf!.frame.height), end: CGPoint(x: weakSelf!.frame.width, y: weakSelf!.frame.height))
//Move to opposite side
moveTo = weakSelf!.randomPointBetween(CGPoint(x: 0, y: 0), end: CGPoint(x:weakSelf!.frame.width, y:0))
break
//Bottom
case 2:
position = weakSelf!.randomPointBetween(CGPoint(x: 0, y: 0), end: CGPoint(x: weakSelf!.frame.width, y: 0))
//Move to opposite side
moveTo = weakSelf!.randomPointBetween(CGPoint(x: 0, y: weakSelf!.frame.height), end: CGPoint(x: weakSelf!.frame.width, y: weakSelf!.frame.height))
break
//Left
case 3:
position = weakSelf!.randomPointBetween(CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: weakSelf!.frame.height))
//Move to opposite side
moveTo = weakSelf!.randomPointBetween(CGPoint(x: weakSelf!.frame.width, y: 0), end: CGPoint(x: weakSelf!.frame.width, y: weakSelf!.frame.height))
break
//Right
case 4:
position = weakSelf!.randomPointBetween(CGPoint(x: weakSelf!.frame.width, y: 0), end: CGPoint(x: weakSelf!.frame.width, y: weakSelf!.frame.height))
//Move to opposite side
moveTo = weakSelf!.randomPointBetween(CGPoint(x: 0, y: 0), end: CGPoint(x: 0, y: weakSelf!.frame.height))
break
default:
break
}
weakSelf!.spawnEnemyAtPosition(position, moveTo: moveTo)
})
let spawning = SKAction.sequence([wait,spawn])
self.runAction(SKAction.repeatActionForever(spawning), withKey:"spawning")
}
func spawnEnemyAtPosition(position:CGPoint, moveTo:CGPoint){
let enemy = SKSpriteNode(color: SKColor.brownColor(), size: CGSize(width: 40, height: 40))
enemy.position = position
enemy.physicsBody = SKPhysicsBody(rectangleOfSize: enemy.size)
enemy.physicsBody?.affectedByGravity = false
enemy.physicsBody?.dynamic = true
enemy.physicsBody?.collisionBitMask = 0 // no collisions
//Here you can randomize the value of duration parameter to change the speed of a node
let move = SKAction.moveTo(moveTo,duration: 2.5)
let remove = SKAction.removeFromParent()
enemy.runAction(SKAction.sequence([move, remove]))
self.addChild(enemy)
}
func didBeginContact(contact: SKPhysicsContact) {
}
/*
Added for debugging purposes
override func touchesBegan(touches: NSSet, withEvent event: UIEvent?) {
//Just make a transition to the other scene, in order to check if deinit is called
//You have to make a new scene ... I named it WelcomeScene
var scene:WelcomeScene = WelcomeScene(fileNamed: "WelcomeScene.sks")
scene.scaleMode = .AspectFill
self.view?.presentScene(scene )
}
*/
}
And here is the result:
The important part is located in createEnemies() method:
//Top
case 1:
position = weakSelf!.randomPointBetween(CGPoint(x: 0, y: weakSelf!.frame.height), end: CGPoint(x: weakSelf!.frame.width, y: weakSelf!.frame.height))
//Move to opposite side
moveTo = weakSelf!.randomPointBetween(CGPoint(x: 0, y: 0), end: CGPoint(x:weakSelf!.frame.width, y:0))
break
Here you define spawning location, which can be any point along the top border. Or more precisely a little bit above top border. Nodes are spawned offscreen. And next, you create (randomize) a point where you would like to move a node, and that is an opposite side in compare to spawn location. So, that can be any random point along bottom border.
If you want to stop spawning, you will do this:
if(self.actionForKey("spawning") != nil){
self.removeActionForKey("spawning")
}
About your physics bodies setup... Note that I've set collisionBitMask of nodes to 0.
enemy.physicsBody?.collisionBitMask = 0 // no collisions
When moving nodes by actions in SpriteKit you are pulling them out of physics simulation and you can get unexpected behaviours if you are expecting to see realistic physics simulation. So, use actions only if you are not interested in collisions (or other sort of physics simulation), but rather just in contact detection. If you need collisions as well, use physics engine and move nodes by applying impulses or forces.
Hope this helps!
Thanks a lot !
I make a different version of your code because i found solution before your answer
func CreationMeteorites(){
let Meteorites = SKSpriteNode(imageNamed: "Meteroites.png")
let choixDeCote = arc4random() % 4 + 1
switch choixDeCote {
case 1 : //Haut
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 200
SpawnX = UInt32(MaxValue - MinValue)
SpawnX = arc4random_uniform(SpawnX)
SpawnY = UInt32(self.size.height)
directionX = Int(arc4random()) % Int(self.frame.size.width)
directionY = 0
action = SKAction.moveTo(CGPoint(x: CGFloat(directionX),y: CGFloat(directionY)),duration: 4)
break
case 2 ://Bas
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 200
SpawnX = UInt32(MaxValue - MinValue)
SpawnX = arc4random_uniform(SpawnX)
SpawnY = 0
directionX = Int(arc4random()) % Int(self.frame.size.width)
directionY = Int(self.frame.size.height)
action = SKAction.moveTo(CGPoint(x: CGFloat(directionX),y: CGFloat(directionY)),duration: 4)
break
case 3 : //Gauche
let MinValue = self.size.height / 8
let MaxValue = self.size.height - 200
SpawnX = 0
SpawnY = UInt32(MaxValue - MinValue)
SpawnY = arc4random_uniform(SpawnY)
directionY = Int(arc4random()) % Int(self.frame.size.height)
directionX = Int(self.frame.size.width)
action = SKAction.moveTo(CGPoint(x: CGFloat(directionX),y: CGFloat(directionY)),duration: 3)
break
case 4 ://Droite
let MinValue = self.size.height / 8
let MaxValue = self.size.height - 200
SpawnX = UInt32(self.size.width)
SpawnY = UInt32(MaxValue - MinValue)
SpawnY = arc4random_uniform(SpawnY)
directionY = Int(arc4random()) % Int(self.frame.size.height)
directionX = 0
action = SKAction.moveTo(CGPoint(x: CGFloat(directionX),y: CGFloat(directionY)),duration: 3)
break
default :
break
}
//Positioner les météorites
Meteorites.position = CGPoint(x: CGFloat(SpawnX), y: CGFloat(SpawnY))
Meteorites.setScale(4)
Meteorites.physicsBody = SKPhysicsBody(circleOfRadius: 30)
Meteorites.physicsBody?.affectedByGravity = false
Meteorites.physicsBody?.dynamic = true
Meteorites.physicsBody?.categoryBitMask = PhysicsCategories.Meteorites
Meteorites.physicsBody?.contactTestBitMask = PhysicsCategories.Meteorites
let actionFini = SKAction.removeFromParent()
Meteorites.runAction(SKAction.sequence([action, actionFini]))
Meteorites.runAction(SKAction.repeatActionForever(action))
self.addChild(Meteorites)
}
And about the collisions do you know a tutorial with a good explain because i don't understand how make collisions.
For anyone that is interested to do this in objective C inside GameScene:
-(void) randomSpawnPosition{
NSUInteger randPos = arc4random_uniform(4);
CGPoint spawnPosition;
CGFloat randFloatX = arc4random_uniform(self.frame.size.width + 10);
CGFloat randFloatY = arc4random_uniform(self.frame.size.height + 10);
switch (randPos) {
//top
case 1:
spawnPosition = CGPointMake(randFloatX, self.frame.size.height+10);
break;
//bottom
case 2:
spawnPosition = CGPointMake(randFloatX, 0-10);
break;
//left
case 3:
spawnPosition = CGPointMake(0 - 10, randFloatY);
break;
//right
case 4:
spawnPosition = CGPointMake(self.frame.size.width + 10, randFloatY);
break;
}
[self addEnemy:spawnPosition];
}