How to randomly pick a node at a position? - swift

I have five different colored circles, each called in the same function but with different physicsBodies and names. I would like them all to move upwards from the bottom of the screen with only one node on the screen at a time from one set position (i.e, CGPointMake(100, -500)) Like after a randomly colored node is generated, after 4 seconds a different randomly picked one will appear?
Is there a way to keep all of them individually identified and do this?
Code:
func circles() {
let sprites = SKSpriteNode(imageNamed: "Circle1")
sprites.position = CGPointMake(45, -200)
sprites.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprites.physicsBody?.dynamic = true
sprites.physicsBody?.affectedByGravity = false
sprites.physicsBody?.categoryBitMask = BodyType.player.rawValue
sprites.physicsBody?.contactTestBitMask = BodyType.enemy.rawValue | BodyType.other.rawValue | BodyType.seven.rawValue | BodyType.nine.rawValue | BodyType.five.rawValue
sprites.size = CGSizeMake(45, 45)
all.addChild(sprites)
let sprite2 = SKSpriteNode(imageNamed: "Circle2")
sprite2.position = CGPointMake(60, -750)
sprite2.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite2.physicsBody?.categoryBitMask = BodyType.eight.rawValue
sprite2.physicsBody?.contactTestBitMask = BodyType.nine.rawValue | BodyType.other.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.seven.rawValue
sprite2.physicsBody?.dynamic = true
sprite2.physicsBody?.affectedByGravity = false
sprite2.size = CGSizeMake(45, 45)
addChild(sprite2)
let sprite3 = SKSpriteNode(imageNamed: "circle3")
sprite3.position = CGPointMake(100, -1200)
sprite3.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite3.physicsBody?.categoryBitMask = BodyType.six.rawValue
sprite3.physicsBody?.contactTestBitMask = BodyType.seven.rawValue | BodyType.other.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.nine.rawValue
sprite3.physicsBody?.dynamic = true
sprite3.physicsBody?.affectedByGravity = false
sprite3.size = CGSizeMake(45, 45)
addChild(sprite3)
let sprite4 = SKSpriteNode(imageNamed: "Circle4")
sprite4.position = CGPointMake(150, -1400)
sprite4.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite4.physicsBody?.categoryBitMask = BodyType.ground.rawValue
sprite4.physicsBody?.contactTestBitMask = BodyType.other.rawValue | BodyType.player.rawValue | BodyType.five.rawValue | BodyType.enemy.rawValue | BodyType.seven.rawValue
sprite4.physicsBody?.dynamic = true
sprite4.physicsBody?.affectedByGravity = false
sprite4.size = CGSizeMake(45, 45)
addChild(sprite4)
let sprite5 = SKSpriteNode(imageNamed: "Circle5")
sprite5.position = CGPointMake(200, -850)
sprite5.physicsBody = SKPhysicsBody(circleOfRadius: 40)
sprite5.physicsBody?.categoryBitMask = BodyType.ya.rawValue
sprite5.physicsBody?.contactTestBitMask = BodyType.five.rawValue | BodyType.player.rawValue | BodyType.other.rawValue | BodyType.seven.rawValue | BodyType.nine.rawValue
sprite5.physicsBody?.dynamic = true
sprite5.physicsBody?.affectedByGravity = false
sprite5.size = CGSizeMake(45, 45)
addChild(sprite5)
}
Help much appreciated.

You can use the name property of SKNode.
From the documentation:
Used to assign a name to a node for later reference. When choosing a
name for a node, you should decide whether each node gets a unique
name or whether some nodes will share a common name. If you give the
node a unique name, you can find the node later by calling the
childNodeWithName: method. If a name is shared by multiple nodes, the
name usually means that these are all a similar object type in your
game. In this case, you can iterate over those objects by calling the
enumerateChildNodesWithName:usingBlock: method.

Related

How To Setup Multiple Physics Bodies in SpriteKit

I need to have two physics bodies on my playerSprite. I need one body to be a smaller contact area and to only detect collisions on certain bodies. My code is inside my PlayerNode class is:
let walkingBitMask = PhysicsCategory.Door | PhysicsCategory.Wall | PhysicsCategory.SlideTerminator | PhysicsCategory.Shootable
let contactBitMask = PhysicsCategory.Monster | PhysicsCategory.Door | PhysicsCategory.Item | PhysicsCategory.Slide | PhysicsCategory.Teleport
let mainBody = SKPhysicsBody(rectangleOf: CGSize(width: 40, height: 50))
mainBody.collisionBitMask = walkingBitMask
mainBody.contactTestBitMask = contactBitMask
let pitBody = SKPhysicsBody(rectangleOf: CGSize(width: 8, height: 8))
pitBody.contactTestBitMask = PhysicsCategory.Pit
pitBody.collisionBitMask = 0
self.physicsBody = SKPhysicsBody(bodies: [mainBody,pitBody])
self.physicsBody?.affectedByGravity = false
self.physicsBody?.categoryBitMask = PhysicsCategory.Player
self.name = "player"
self.physicsBody?.allowsRotation = false
The Pit category is all I want to interact with the pitBody. My code in setting up the Pit sprites is:
sprite.physicsBody=SKPhysicsBody(rectangleOf: CGSize(width: 8, height: 8))
sprite.physicsBody?.affectedByGravity = false
sprite.physicsBody?.isDynamic = false
sprite.name = "pit"
sprite.physicsBody?.allowsRotation = false
sprite.physicsBody?.categoryBitMask = PhysicsCategory.Pit
sprite.physicsBody?.collisionBitMask = 0
The problem I'm running into is that the player sprite stops moving when it encounters a pit sprite. Everything else works fine.
When you create a physics body using SKPhysicsBody.init(bodies:), the properties on the children are ignored. Only the shapes of the child bodies are used, according to the docs.
So the test bit masks specified on mainBody and pitBody are ignored, and the compound body of the player that is part of the simulation only specifies the categoryBitMask:
self.physicsBody?.categoryBitMask = PhysicsCategory.Player
This means it will collide with other bodies, since it's collisionBitMask default value is 0xFFFFFFFF (all bits set).
Configure the collisionBitMask and contactTestBitMask on the player body.
If you need to use separate collisions, consider using a SKPhysicsJointFixed to fuse the two physics bodies together.

IOS Charts positioning of gridlines

I am struggling to find out how to set gridlines on my chart to specific points on the x axis and to centre labels on the lines corresponding to the time of day. I have a simple chart, the data represents a day with about 280 data points (one for every 5 minute period). I want to show gridlines at times of the day equal to 03:00, 06:00, 12:00, 15:00, 18:00 and 21:00 and show the corresponding labels centred below the gridlines. I have have also tried LimitLines, which works for placing the vertical lines at the correct points, but there does not seem to be the ability to align the time labels with the centre of the limit lines.
I know this can be done because I have seen screenshots on-line showing time labels centred on gridlines (or limitlines) for specific periods on the day, but I've spent hours looking for how to do this an I'm drawing a blank.
My code below (note the commented out failed attempt to get limitlines to work).
let tideHeights = LineChartDataSet(entries: dataEntries)
tideHeights.drawCirclesEnabled = false
tideHeights.colors = [NSUIColor.black]
//let timings = ["00:00", "03:00", "06:00", "12:00", "15:00", "18:00", "21:00"]
let data = LineChartData(dataSet: tideHeights)
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
//lineChartView.xAxis.drawGridLinesEnabled = false
//let gridLine = values.count / timings.count
//var xPos = 0
//for label in timings {
//let limitLine = ChartLimitLine(limit: Double(xPos), label: label)
//limitLine.labelPosition = .bottomLeft
//limitLine.lineWidth = 0.5
//limitLine.lineColor = .black
//limitLine.valueTextColor = .black
//lineChartView.xAxis.addLimitLine(limitLine)
//xPos += gridLine
//}
lineChartView.xAxis.labelCount = 7
lineChartView.xAxis.labelPosition = .bottom
lineChartView.xAxis.granularity = 1
lineChartView.xAxis.avoidFirstLastClippingEnabled = true
lineChartView.legend.enabled = false
lineChartView.data = data
The output is below, the gridlines are at system set positions, how can I control where they are placed?
Update:
After applying the very helpful suggestion by Stephan Schlecht, I am still getting strange results. With lineChartView.xAxis.setLabelCount(9, force: true) I get:
and with lineChartView.xAxis.setLabelCount(4, force: true) I get:
It seems for some reason that the 1st gridline after the Axis is ignored.
Code is:
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
let marker = ChartMarker()
marker.setMinuteInterval(interval: 5)
marker.chartView = lineChartView
lineChartView.marker = marker
lineChartView.xAxis.setLabelCount(4, force: true)
lineChartView.xAxis.avoidFirstLastClippingEnabled = true
lineChartView.xAxis.labelPosition = .bottom
lineChartView.leftAxis.labelPosition = .insideChart
lineChartView.rightAxis.drawLabelsEnabled = false
lineChartView.legend.enabled = false
lineChartView.data = data
lineChartView.data?.highlightEnabled = true
Update 2
To reproduce, I have the following class which creates the data (btw, it is a stub class, eventually it will contain a paid api, so for now I generate the data for test purposes:
class TideHeights {
var tideHeights = [Double]()
var tideTimes = [Date]()
var strTimes = [String]()
var intervalMins = 5
init (intervalMins: Int) {
self.intervalMins = intervalMins
let slots = 24 * 60.0 / Double(intervalMins)
let seconds = 24 * 60 * 60.0 / slots
let radians = 2 * Double.pi / slots
let endDate = Date().endOfDay
var date = Date().startOfDay
var radianOffset = 0.0
repeat {
tideHeights.append(sin(radianOffset) + 1)
tideTimes.append(date)
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "HH:mm"
strTimes.append(timeFormatter.string(from: date))
date = date.advanced(by: seconds)
radianOffset += radians
} while date <= endDate
}
}
this class is instantiated and used to draw the graph in the following two functions in ViewController.
func drawChart() -> Void {
let tideData = TideHeights(intervalMins: 5)
lineChartView.dragEnabled = true
lineChartView.setScaleEnabled(false)
lineChartView.pinchZoomEnabled = false
setChartValues(dataPoints: tideData.strTimes, values: tideData.tideHeights)
}
func setChartValues(dataPoints: [String], values: [Double]) -> Void {
//initialise and load array of chart data entries for drawing
var dataEntries: [ChartDataEntry] = []
for i in 0..<values.count {
let dataEntry = ChartDataEntry(x: Double(i), y: values[i])
dataEntries.append(dataEntry)
}
let tideHeights = LineChartDataSet(entries: dataEntries)
tideHeights.drawCirclesEnabled = false
tideHeights.colors = [NSUIColor.black]
let gradientColors = [ChartColorTemplates.colorFromString("#72E700").cgColor,
ChartColorTemplates.colorFromString("#724BEB").cgColor]
let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)!
tideHeights.fillAlpha = 0.7
tideHeights.fill = Fill(linearGradient: gradient, angle: 90)
tideHeights.drawFilledEnabled = true
let data = LineChartData(dataSet: tideHeights)
lineChartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: dataPoints)
let marker = ChartMarker()
marker.setMinuteInterval(interval: 5)
marker.chartView = lineChartView
lineChartView.marker = marker
lineChartView.xAxis.setLabelCount(4, force: true)
lineChartView.xAxis.avoidFirstLastClippingEnabled = true
lineChartView.xAxis.labelPosition = .bottom
lineChartView.leftAxis.labelPosition = .insideChart
lineChartView.rightAxis.drawLabelsEnabled = false
lineChartView.legend.enabled = false
lineChartView.data = data
lineChartView.data?.highlightEnabled = true
}
The documentation says:
setLabelCount(int count, boolean force): Sets the number of labels for
the y-axis. Be aware that this number is not fixed (if force == false)
and can only be approximated. If force is enabled (true), then the
exact specified label-count is drawn – this can lead to uneven numbers
on the axis.
see https://weeklycoding.com/mpandroidchart-documentation/axis-general/
Remark: although the y-axis is mentioned in the documentation of the function, it can actually be applied to both axes.
So instead of
lineChartView.xAxis.labelCount = 7
use
lineChartView.xAxis.setLabelCount(9, force: true)
Note: you need 9 label counts not 7.
Test
If one combines a sine wave of 288 points with your code above
for i in 0...288 {
let val = sin(Double(i) * 2.0 * Double.pi / 288.0) + 1
dataEntries.append(ChartDataEntry(x: Double(i), y: val))
}
and forces the label count = 9 as shown, then it looks like this:
Stephan was correct in his answer to this question. To force gridlines and alignment, use setLabelCount(x, force: true) the additional complexity related to my specific project and the data contained in the labels.

Detect overlaping if enumerating nodes

I would like to know how should I detect overlapping nodes while enumerating them? Or how should I make that every random generated position in Y axis is at least some points higher or lower.
This is what I do:
1 - Generate random number between -400 and 400
2 - Add those into array
3 - Enumerate and add nodes to scene with generated positions like this:
var leftPositions = [CGPoint]()
for _ in 0..<randRange(lower: 1, upper: 5){
leftPositions.append(CGPoint(x: -295, y: Helper().randomBetweenTwoNumbers(firstNumber: leftSparkMinimumY, secondNumber: leftSparkMaximumY)))
}
leftPositions.enumerated().forEach { (index, point) in
let leftSparkNode = SKNode()
leftSparkNode.position = point
leftSparkNode.name = "LeftSparks"
let leftSparkTexture = SKTexture(imageNamed: "LeftSpark")
LeftSpark = SKSpriteNode(texture: leftSparkTexture)
LeftSpark.name = "LeftSparks"
LeftSpark.physicsBody = SKPhysicsBody(texture: leftSparkTexture, size: LeftSpark.size)
LeftSpark.physicsBody?.categoryBitMask = PhysicsCatagory.LeftSpark
LeftSpark.physicsBody?.collisionBitMask = PhysicsCatagory.Bird
LeftSpark.physicsBody?.contactTestBitMask = PhysicsCatagory.Bird
LeftSpark.physicsBody?.isDynamic = false
LeftSpark.physicsBody?.affectedByGravity = false
leftSparkNode.addChild(LeftSpark)
addChild(leftSparkNode)
}
But like this sometimes they overlap each other because the generated CGPoint is too close to the previous one.
I am trying to add some amount of triangles to the wall and those triangles are rotated by 90°
To describe in image what I want to achieve:
And I want to avoid thing like this:
Your approach to this is not the best, i would suggest only storing the Y values in your position array and check against those values to make sure your nodes will not overlap. The following will insure no two sparks are within 100 points of each other. You may want to change that value depending on your node's actual height or use case.
Now, obviously if you end up adding too many sparks within an 800 point range, this just will not work and cause an endless loop.
var leftPositions = [Int]()
var yWouldOverlap = false
for _ in 0..<randRange(lower: 1, upper: 5){
//Moved the random number generator to a function
var newY = Int(randY())
//Start a loop based on the yWouldOverlap Bool
repeat{
yWouldOverlap = false
//Nested loop to range from +- 100 from the randomly generated Y
for p in newY - 100...newY + 100{
//If array already contains one of those values
if leftPosition.contains(p){
//Set the loop Bool to true, get a new random value, and break the nested for.
yWouldOverlap = true
newY = Int(randY())
break
}
}
}while(yWouldOverlap)
//If we're here, the array does not contain the new value +- 100, so add it and move on.
leftPositions.append(newY)
}
func randY() -> CGFloat{
return Helper().randomBetweenTwoNumbers(firstNumber: leftSparkMinimumY, secondNumber: leftSparkMaximumY)
}
And here is a different version of your following code.
for (index,y) in leftPositions.enumerated() {
let leftSparkNode = SKNode()
leftSparkNode.position = CGPoint(x:-295,y:CGFloat(y))
leftSparkNode.name = "LeftSparks\(index)" //All node names should be unique
let leftSparkTexture = SKTexture(imageNamed: "LeftSpark")
LeftSpark = SKSpriteNode(texture: leftSparkTexture)
LeftSpark.name = "LeftSparks"
LeftSpark.physicsBody = SKPhysicsBody(texture: leftSparkTexture, size: LeftSpark.size)
LeftSpark.physicsBody?.categoryBitMask = PhysicsCatagory.LeftSpark
LeftSpark.physicsBody?.collisionBitMask = PhysicsCatagory.Bird
LeftSpark.physicsBody?.contactTestBitMask = PhysicsCatagory.Bird
LeftSpark.physicsBody?.isDynamic = false
LeftSpark.physicsBody?.affectedByGravity = false
leftSparkNode.addChild(LeftSpark)
addChild(leftSparkNode)
}

Crash on launch, error with SKphysics Joint

I am creating a game and i am trying to create a character that has 2 different nodes, the legs and the torso. I tried to link them using a fixed type joint, however when i do that the torso slides off of the legs sometimes while moving around. Now i am trying to create the character using a distance limit between the 2 nodes hoping that they won't slide away from each other now,however it keeps crashing on launch any ideas?
here is my code
func CreateHero (){
soldierLegs.position = CGPoint(x: 405 , y: 139)
soldierLegs.zPosition = 1
soldierLegs.anchorPoint.x = 0.6
soldierLegs.anchorPoint.y = 0.7
let soldierLegsBody:SKPhysicsBody = SKPhysicsBody(rectangleOfSize:
soldierLegs.size)
soldierLegsBody.dynamic = true
soldierLegsBody.affectedByGravity = true
soldierLegsBody.allowsRotation = false
//body.restitution = 0.4
soldierLegsBody.categoryBitMask = BodyType.soldierL.rawValue
soldierLegsBody.contactTestBitMask = BodyType.enemy1.rawValue |
BodyType.enemy2.rawValue | BodyType.enemy3.rawValue |
BodyType.desertForegroundCase.rawValue
soldierLegs.physicsBody = soldierLegsBody
soldierTorso.position = soldierLegs.position
soldierTorso.zPosition = 2
soldierTorso.anchorPoint.x = 0.25
soldierTorso.anchorPoint.y = 0.1
let soldierTorsoBody:SKPhysicsBody = SKPhysicsBody(rectangleOfSize:
soldierTorso.size)
soldierTorsoBody.dynamic = true
soldierTorsoBody.affectedByGravity = true
soldierTorsoBody.allowsRotation = false
soldierTorsoBody.categoryBitMask = BodyType.soldierT.rawValue
soldierTorsoBody.contactTestBitMask = BodyType.enemy1.rawValue |
BodyType.enemy2.rawValue | BodyType.enemy3.rawValue |
BodyType.desertForegroundCase.rawValue
soldierTorso.physicsBody = soldierTorsoBody
let Joint =
SKPhysicsJointLimit.jointWithBodyA(soldierLegs.physicsBody!, bodyB:
soldierTorso.physicsBody!, anchorA: soldierLegs.position, anchorB:
soldierTorso.position)
Joint.maxLength = 0.01
self.addChild(soldierTorso)
self.addChild(soldierLegs)
self.physicsWorld.addJoint(Joint)
}
According to the Apple official document :
Creating a Limit Joint
class func joint(withBodyA: SKPhysicsBody, bodyB: SKPhysicsBody, anchorA: CGPoint, anchorB: CGPoint)
Creates a new limit joint.
anchorA
A connection point on the first body in the scene’s coordinate system.
anchorB
A connection point on the second body in the scene’s coordinate system.
In your case anchorA is equal to anchorB (the same point), there is no limit, this one cause the crash.

Change SpriteNode PhysicsBody Size at Run Time

I want to be able to change a node's physicsBody height when the user swipes downwards, but have not been able to find out how to do this, beside resetting the entire physicsBody.
When I originally load the node, I use the below code:
nodeHero.color = UIColor .grayColor()
nodeHero.size.width = 20
nodeHero.size.height = 45
nodeHero.position.x = -frame.size.width/2 + 45
nodeHero.position.y = pointMainY + 30 + nodeHero.size.height/2
nodeHero.zPosition = 110
nodeHero.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(nodeHero.size.width, nodeHero.size.height))
nodeHero.physicsBody?.mass = 1
nodeHero.physicsBody?.angularVelocity = 0
nodeHero.physicsBody?.allowsRotation = false
nodeHero.physicsBody?.restitution = 0
nodeHero.physicsBody?.categoryBitMask = bitHero
addChild(nodeHero)
And when I swipe down, I want to be able to do something like this (this doesn't work):
nodeHero.size.height = 28
nodeHero.physicsBody?.size.height = 28
But instead I have to use the nodeHero.physicsBody = SKPhysicsBody() again, which resets all the other physicsBody properties, so I have to do this:
nodeHero.size.height = 28
nodeHero.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(nodeHero.size.width, nodeHero.size.height))
nodeHero.physicsBody?.mass = 1
nodeHero.physicsBody?.angularVelocity = 0
nodeHero.physicsBody?.allowsRotation = false
nodeHero.physicsBody?.restitution = 0
nodeHero.physicsBody?.categoryBitMask = bitHero
According to SpriteKit documentation the area of a SKPhysicsBody can't be modified, so you need to create another SKPhysicsBody instance and copy the values you want to keep from the previous instance.