Swift SpriteKit contain nodes within screen width - swift

I have randomly spawning nodes moving vertically down the view.
Here is the code for doing this:
let meteTexture = SKTexture(imageNamed: "redmete.png")
let movementAmount = arc4random() % UInt32(self.frame.width)
let meteOffset = CGFloat(movementAmount) - self.frame.width / 2
let meteTime = arc4random_uniform(4) + 3;
let moveMete = SKAction.move(by: CGVector(dx: 0, dy: -2 * self.frame.height), duration: TimeInterval(meteTime))
redmete = SKSpriteNode(texture: meteTexture)
redmete.position = CGPoint(x: self.frame.midX + meteOffset, y: self.frame.midY + self.frame.height / 2)
My only problem is that as the meteOffset uses the centre of the sprite therefore it can occasionally spawn so 50% or so is out of the view.
I have tried
let movementAmount = arc4random() % UInt32(self.frame.width - meteTexture.size().width / 2)
I've also tried
let meteOffset = CGFloat(movementAmount) - meteTexture.size().width / 2 - self.frame.width / 2
But neither keep the whole of the sprite within the view. How can I do this?

You will want the initial position to be between the sub range of your frame height that is inset by half the height of the meteor.
You would need to have :
let meteorHeight = meteTexture.size().height
let verticalRange = self.frame.height - meteorHeight
let randomXPosition = meteorHeight/2 + arc4random() % verticalRange
Same goes for the horizontal position (if you want that to be random as well) :
let meteorWidth = meteTexture.size().width
let horizontalRange = self.frame.width - meteorWidth
let randomYPosition = meteorWidth/2 + arc4random() % horizontalRange
You can then set the position directly with the random XY coordinates
redmete.position = CGPoint(x:randomXPosition, y:randomYPosition)
If you don't want the meteor to appear too close to the edges, you can reduce the value of the verticalRange and horizontalRange further by subtracting a fixed offset or multiplying by a fraction.

SOLVED:
let meteTexture = SKTexture(imageNamed: "redmete.png")
let movementAmount = arc4random() % UInt32(self.frame.width / 2)
let meteOffset = CGFloat(movementAmount) + meteTexture.size().width/2 - self.frame.width / 2
let meteTime = arc4random_uniform(4) + 3;
let moveMete = SKAction.move(by: CGVector(dx: 0, dy: -2 * self.frame.height), duration: TimeInterval(meteTime))
redmete = SKSpriteNode(texture: meteTexture)
redmete.position = CGPoint(x: self.frame.midX + meteOffset, y: self.frame.midY + self.frame.height / 2)

Related

Amoeba-shaped controls with SpriteKit

I am new to SpriteKit, and I am trying to create a distorted circle that acts like "amoeba". What I am trying to do is to use SKShapeNode initialised with with UIBezierPath created over 8-10 points on a circle with some randomness (something like):
let theta : Double = (Double(i) * Double.pi / 180.0) * (36.0 + Double.random(in: -1...1))
let radius : CGFloat = CGFloat(100.0 + Double.random(in: 0...20))
let a = CGFloat(cos(theta)) * radius
let b = CGFloat(sin(theta)) * radius
This part works, but then, I am starting to build the path using addCurve() method and the shapes come out really ugly - probably because I don't fully understand how the method works and what should I use for control points.
Appreciate if you have any better ideas or help me with using addCurve() in a better way.
Here is what I've done - posting as it might be useful to someone, parameters could be tweaked to your liking.
let path = UIBezierPath()
let numPoints = Int.random(in: 5...25)
let totalPoints = numPoints * 3
var pt : [CGPoint] = Array(repeating: CGPoint(), count: totalPoints)
var coef = 1.0
for i : Int in 0..<(totalPoints) {
let theta = (Double(i) * Double.pi / 180.0) * (360.0/Double(totalPoints) + Double.random(in: -1.0...1.0))
coef = (i % 3 == 2 ? 1 : 0.25)
let radius = CGFloat(100.0 * coef + Double.random(in: 0...20))
let a = CGFloat(cos(theta)) * radius
let b = CGFloat(sin(theta)) * radius
pt[i] = CGPoint(x: a, y: b)
// the code snippet below is to show the curve and critical points
let point : SKShapeNode = SKShapeNode(circleOfRadius: 5)
point.fillColor = i % 3 == 0 ? .green : .cyan
point.position = pt[i]
point.zPosition = 10
self.addChild(point)
// end of code snippet
}
path.move(to: CGPoint(x: self.view!.bounds.minX + pt[0].x, y: self.view!.bounds.minY + pt[0].y))
for i in 1...numPoints{
path.addCurve(to: pt[(i * 3) % (totalPoints)],
controlPoint1: pt[(i * 3 - 1)],
controlPoint2: pt[(i * 3 - 2)])
}
path.close()
let amoeba = SKShapeNode(path: path.cgPath)
amoeba.strokeColor = .orange
self.addChild(amoeba)

Spritekit Add nodes without overlapping

I am trying to add 20 nodes at random locations on the screen without any of the nodes overlapping. Ive got the adding of the nodes at random locations part but I sill get some that overlap. What I have done so far is: I would be greatful for a point in the right dirrection.
while i < 20 {
let bubbleSize = self.frame.width / 12
let bubble = SKShapeNode(circleOfRadius: bubbleSize)
let widthL = -self.frame.size.width / 2 + bubble.frame.size.width / 2
let widthH = self.frame.size.width / 2 - bubble.frame.size.width / 2
let heightL = -self.frame.size.height / 2 + bubble.frame.size.height/ 2
let heightH = self.frame.size.height / 2 - bubble.frame.size.height / 2
var randWidth = randomNumber(range: widthL..<widthH)
var randHeight = randomNumber(range: heightL..<heightH)
bubble.fillColor = SKColor.cyan
bubble.position = CGPoint(x: randWidth, y: randHeight)
self.addChild(bubble)
}
func randomNumber(range: Range<CGFloat>) -> CGFloat {
//function that gives a random number of a range of CGFloats entered
let min = range.lowerBound
let max = range.upperBound
return CGFloat(arc4random_uniform(UInt32(CGFloat(max - min)))) + min
}
This should get you started. It essentially divides the screen into a grid and places the bubbles in grid locations while ensuring no other bubble is at that position. there is lots more you could do with this, like add slight random placement to the location (random generate some small offsets -2 to +2 for x and y values so that they don't look so perfectly placed).
let bubbleSize = self.frame.width / 12
let minX = 0 - self.frame.size.width / 2
let maxX = self.frame.size.width / 2
let minY = 0 - self.frame.size.height/ 2
let maxY = self.frame.size.height/ 2
var usedIndexes: [Int]!
let xRange = maxX * 2
let yRange = maxY * 2
let cols = xRange / bubbleSize
let rows = yRange / bubbleSize
func createBubbles(count: Int) {
for _ in 0..<count {
createBubble()
}
}
func createBubble() {
let index = findSlot()
let posX = index % cols
let posY = index / cols
let bubble = SKShapeNode(circleOfRadius: bubbleSize)
bubble.fillColor = SKColor.cyan
bubble.position = CGPoint(x: posX, y: posY)
self.addChild(bubble)
}
func findSlot() -> Int {
//preventative measure to stop endless loop from happening
guard usedIndexes.count > rows * cols else { return 0 }
let randomX = randomNumber(range: 0..<cols)
let randomY = randomNumber(range: 0..<rows)
let index = randomX + randomY * cols
if usedIndexes.contains(index) {
return findSlot()
}
else {
usedIndexes.append(index)
}
return index
}

How to connect two SCNSpheres in 3D space using Bezier path in Swift?

I have the following code:
func createScene(){
count += 1
let sphereGeom = SCNSphere(radius: 1.5)
sphereGeom.firstMaterial?.diffuse.contents = UIColor.redColor()
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: 0, y: 0))
let radius = 3.0
var radians = Double(0)
var yPosition = Float(5.4)
while count <= 20 {
if radians >= 2{
radians -= 2
}
let sphereNode = SCNNode(geometry: sphereGeom)
let angle = Double(radians * M_PI)
let xPosition = Float(radius * cos(angle))
let zPosition = Float(radius * sin(angle))
sphereNode.position = SCNVector3(xPosition, yPosition, zPosition)
let cgX = CGFloat(xPosition)
let cgY = CGFloat(yPosition)
path.addQuadCurveToPoint(CGPoint(x: cgX, y: cgY), controlPoint: CGPoint(x: (cgX / 2), y: (cgY / 2)))
path.addLineToPoint(CGPoint(x: (cgX - (cgX * 0.01)), y: cgY))
path.addQuadCurveToPoint(CGPoint(x: 1, y: 0), controlPoint: CGPoint(x: (cgX / 2), y: ((cgY / 2) - (cgY * 0.01))))
let shape = SCNShape(path: path, extrusionDepth: 3.0)
shape.firstMaterial?.diffuse.contents = UIColor.blueColor()
let shapeNode = SCNNode(geometry: shape)
shapeNode.eulerAngles.y = Float(-M_PI_4)
self.rootNode.addChildNode(shapeNode)
count += 1
radians += 0.5556
yPosition -= 1.35
self.rootNode.addChildNode(sphereNode)
}
I want to add a Bezier path connecting each sphere to the next one, creating a spiral going down the helix. For some reason, when I add this code, the shape doesn't even appear. But when I use larger x and y values, I see the path fine, but it is no way oriented to the size of the spheres. I don't understand why it disappears when I try to make it smaller.
Your SCNShape doesn't ever get extruded. Per Apple doc,
An extrusion depth of zero creates a flat, one-sided shape.
With larger X/Y values your flat shape happens to become visible. You can't build a 3D helix with SCNShape, though: the start and end planes of the extrusion are parallel.
You'll have to use custom geometry, or approximate your helix with a series of elongated SCNBox nodes. And I bet someone out there knows how to do this with a shader.

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];
}

Obstacle random gap not working [SpriteKit] [Swift]

I have a problem in my code. When I run the game, I have two obstacles (one on the left and one on the right). I coded a gap between each obstacle and it has been working. I have a problem where the gap sometimes spawns off of the screen where the player cannot get to. How can I fix this?
Here's my code:
let gapWidth = square.size.width * 1.5
var movementAmount = arc4random() % UInt32(self.frame.size.width/2)
var obstacleOffset = CGFloat(movementAmount) - self.frame.size.width / 2
var obstacle1 = SKSpriteNode(imageNamed: "Obstacle")
obstacle1.zPosition = 30
obstacle1.size = CGSize(width: self.frame.size.width, height: 300)
obstacle1.position = CGPoint(x: CGRectGetMidX(self.frame) + obstacle1.size.width / 2 + gapWidth / 2 + obstacleOffset, y: CGRectGetMidY(self.frame))
self.addChild(obstacle1)
var obstacle2 = SKSpriteNode(imageNamed: "Obstacle")
obstacle2.zPosition = 30
obstacle2.size = CGSize(width: self.frame.size.width, height: 300)
obstacle2.position = CGPoint(x: CGRectGetMidX(self.frame) - obstacle2.size.width / 2 - gapWidth / 2 + obstacleOffset, y: CGRectGetMidY(self.frame))
self.addChild(obstacle2)
You need to make sure the values of the random positions will always be on the screen. You can do that by using code similar to this:
var height = UInt32(self.frame.size.height)
var randomYPos = CGFloat(arc4random_uniform(UInt32(height)))
You can do the same for the random x value, just use the screen width. You only need to do this for obstacle 1, then set the position of obstacle 2 relative to obstacle 1.