func createPaddles() -> SKNode {
let container = SKNode()
let colors = [SKColor.green, SKColor.blue, SKColor.red, SKColor.green, SKColor.blue]
let width = self.frame.size.width
let height = self.frame.size.height
for i in 0...4 {
let node = SKShapeNode(rectOf: CGSize(width: self.frame.size.width / 3, height: self.frame.size.height * 0.15))
node.fillColor = colors[i]
node.strokeColor = colors[i]
node.zPosition = 100
node.position = CGPoint(x: -width * 1/6 + width / 3 * i, y: height * 0.075)
container.addChild(node)
}
return container
}
node.position is giving me an error saying it's too complex. Is this because calculating the frame width twice in one equation is too much for the compiler? Seems unlikely and this doesn't seem overly complex to me. Am I missing something here?
EDIT: Great group effort from everyone, thank you! This is what eventually worked (a combination of a few different answers):
node.position = CGPoint(x: -width / 6.0 + width / 3.0 * CGFloat(i), y: height * 0.075)
One of the reasons why this happens is that you have a type mismatch. However, instead of printing the type mismatch error, the compiler tries to find a way to match the types... and fails.
In your specific case, i is an Int while other variables in the expression are CGFloat:
-width * 1/6 + width / 3 * CGFloat(i)
Also note that 1/6 will be probably considered to be integer division, with the result being 0 although I am not sure about the priority right now. You probably want:
-width / 6 + width / 3 * CGFloat(i)
to be sure.
Generally it has to do with the compiler type system inferring the proper types to execute the statement. You can often shortcut the process by providing the types for it.
Note that simplifying your equation results in this:
node.position = CGPoint(x: width * (i - 0.5) / 3.0, y: height * 0.075)
You can also expressly convert types so that the compiler has less to do:
node.position = CGPoint(x: CGFloat(width) * (CGFloat(i) - CGFloat(0.5)) / CGFloat(3.0), y: CGFloat(height) * CGFloat(0.075))
This error occurs when the compiler needs to do a lot of type checking. See this answer for a good explanation: https://stackoverflow.com/a/29931329/6658553
In your specific case, try reducing the number of operators you are using, and split it into multiple calculations.
node.position = CGPoint(x: -width * 1/6 + width / 3 * i, y: height * 0.075)
could be refactored to something like this:
let newXPosition: CGFloat = -width * 1/6 + width / 3 * CGFloat(i)
node.position = CGPoint(x: newXPosition, y: height * 0.075)
It is not a very clear error, but it's because you are trying to multiply CGFloats with Ints
width is a CGFloat
let width = self.frame.size.width
"i" in your for loop is an int
for i in 0...4 {
let node = SKShapeNode(rectOf: CGSize(width: self.frame.size.width / 3, height: self.frame.size.height * 0.15))
node.fillColor = colors[i]
node.strokeColor = colors[i]
node.zPosition = 100
node.position = CGPoint(x: -width * 1/6 + width / 3 * i, y: height * 0.075)
container.addChild(node)
}
You can separate them to sub clocks as others have mentioned (probably preferred) but at the core of it you need to ensure that all of the values in your equation are of the same type
easiest way to make them of the same type would be to cast i to CGFloat
CGFloat(i)
Related
I'm currently converting my android game to iOS, and I'm trying to get a bat enemy to 'swoop' the player. I have the curve created in SpriteKit and it looks fine, however the bat does not appear to follow the line correctly, it disappears for a few seconds and then zips past the top right corner going upwards despite running an action.follow on a path that looks drawn properly. The path is attached to a shapeNode which follows the player.
Code:
func createPath(){
//All x values are moved back 1 / 4 of the screens width due to the camera being 1 / 4 of the screens width ahead of the player, and y values are halved due to the player being in the centre of y axis
path.move(to: CGPoint(x: gameScene.frame.width * 3 / 4, y: gameScene.frame.height / 2)) //top right corner
path.addCurve(to: CGPoint(x: -(gameScene.frame.width / 3.5), y: gameScene.frame.height / 6), control1: CGPoint(x: gameScene.frame.width / 2, y: -(gameScene.frame.height / 9)), control2: CGPoint(x: 0, y: -(gameScene.frame.width / 9)))
//to = off screen
//control1 3 / 4 across screen, slighly higher than player
//control2 exactly on player (players node height is screen width / 4.5)
followLine = SKAction.follow(path, asOffset: true, orientToPath: false, duration: 3)
viewLine.path = path
gameScene.addChild(viewLine)
}
func update(dt: Double){
if(GameScene.gV.distanceM == 4 && !run){//DistanceM is just a timer (seconds)
run = true
bat.run(followLine)
}
if run {
/*time += dt//timer to reset bats position
if time > 4 {
run = false
time = 0
}*/
} else {
bat.position = CGPoint(x: gameScene.pigeonCam.position.x + gameScene.frame.width / 2, y: gameScene.pigeonCam.position.x + gameScene.frame.height / 2)//keep bat at top right of screen
}
viewLine.position = gameScene.pigeon.pigeon.position//get node to follow the player
}
My best guess is that while moving the shapeNode appears to move the path it actually doesn't, if so, any other way I could get this path to "follow" the player
After much trial and error I realised the node asOffset is referring to is the one that runs the action, so I put the bat at the top right corner of the screen and adjusted the values, my final values looked as so:
let width = gameScene.frame.width
let height = gameScene.frame.height
path.move(to: CGPoint(x: 0, y: 0)) //top right corner
//0, 0
path.addCurve(to: CGPoint(x: -(width * 1.2), y: -(height / 3.5)), control1: CGPoint(x: -(width / 4), y: -(height / 1.5)), control2: CGPoint(x: -(width * 3 / 4), y: -(height / 2)))
This makes the bat follow the line perfectly, though the visual representation of the line is messed up
I am trying to create a line chart which represents a set of values (x and y) in a smooth bezier curve. This works fine, except when the x-values are close to each other and the y-values go from a continuous line to a lower or higher value. The values are not shown in the chart itself, but here is an image illustrating my problem:
As you can see, the line makes a backwards movement before continuing to the next point. I would like this to not happen and smoothen out. To generate the data points, I use this library from Minh Nguyen, which has helped me a lot. The only problem is this issue still. For easiness, here is the code I currently use:
private func controlPointsFrom(points: [CGPoint]) -> [CurvedSegment] {
var result: [CurvedSegment] = []
let delta: CGFloat = 0.3
for i in 1..<points.count {
let A = points[i-1]
let B = points[i]
let controlPoint1 = CGPoint(x: A.x + delta*(B.x-A.x), y: A.y + delta*(B.y - A.y))
let controlPoint2 = CGPoint(x: B.x - delta*(B.x-A.x), y: B.y - delta*(B.y - A.y))
let curvedSegment = CurvedSegment(controlPoint1: controlPoint1, controlPoint2: controlPoint2)
result.append(curvedSegment)
}
for i in 1..<points.count-1 {
let M = result[i-1].controlPoint2
let N = result[i].controlPoint1
let A = points[i]
let MM = CGPoint(x: 2 * A.x - M.x, y: 2 * A.y - M.y)
let NN = CGPoint(x: 2 * A.x - N.x, y: 2 * A.y - N.y)
result[i].controlPoint1 = CGPoint(x: (MM.x + N.x)/2, y: (MM.y + N.y)/2)
result[i-1].controlPoint2 = CGPoint(x: (NN.x + M.x)/2, y: (NN.y + M.y)/2)
}
return result
}
func createCurvedPath(_ dataPoints: [CGPoint]) -> UIBezierPath? {
let path = UIBezierPath()
path.move(to: dataPoints[0])
var curveSegments: [CurvedSegment] = []
let useDataPoints = dataPoints.filter { ($0.y < 1000) }
curveSegments = controlPointsFrom(points: useDataPoints)
for i in 1..<useDataPoints.count {
path.addCurve(to: useDataPoints[i], controlPoint1: curveSegments[i - 1].controlPoint1, controlPoint2: curveSegments[i - 1].controlPoint2)
}
return path
}
For documentation, I would refer to the tutorial/blogpost I linked earlier. I figure the issue should be somewhere in the calculation of controlPoint1 and controlPoint2 in the controlPointsFrom function. When I remove the delta or make it 0, it just become straight lines but then the issue doesn't occur either. So the math should be different I think, to keep track of the previous value and perhaps don't create a control point with a higher or lower y-value when the next point is lower or higher, respectively. But I am unable to figure out how to make it work. Any smart mind who can make this happen?
Would be forever grateful!
try this:
Smooth UIBezierPath
https://medium.com/#ramshandilya/draw-smooth-curves-through-a-set-of-points-in-ios-34f6d73c8f9
I use the following function to append physicsbodies on tiles from a SKTileMapNode:
static func addPhysicsBody(to tileMap: SKTileMapNode, and tileInfo: String){
let tileSize = tileMap.tileSize
let halfWidth = CGFloat(tileMap.numberOfColumns) / 2 * tileSize.width
let halfHeight = CGFloat(tileMap.numberOfRows) / 2 * tileSize.height
for row in 0..<tileMap.numberOfColumns{
for column in 0..<tileMap.numberOfRows{
let tileDefinition = tileMap.tileDefinition(atColumn: column, row: row)
let isCorrectTile = tileDefinition?.userData?[tileInfo] as? Bool
if isCorrectTile ?? false && tileInfo == "wall"{
let x = CGFloat(column) * tileSize.width - halfWidth
let y = CGFloat(row) * tileSize.height - halfHeight
let tileNode = SKNode()
tileNode.position = CGPoint(x: x, y: y)
tileNode.physicsBody = SKPhysicsBody.init(rectangleOf: tileSize, center: CGPoint(x: tileSize.width / 2, y: tileSize.height / 2))
tileNode.physicsBody!.isDynamic = false
tileNode.physicsBody!.restitution = 0.0
tileNode.physicsBody!.categoryBitMask = Constants.PhysicsCategories.wall
tileNode.physicsBody!.collisionBitMask = Constants.PhysicsCategories.player | Constants.PhysicsCategories.npc | Constants.PhysicsCategories.enemy
nodesForGraph.append(tileNode)
tileMap.addChild(tileNode)
}
}
}
}
However if I use this, I have a physicsbody per tile. I want to connect physicsbodies to bigger ones to get a better performance. I know that this can be with init(bodies: [SKPhysicsBody]). But how can I do that?
How can I find out which body is next to another body to group them?
The physicsbodies in the tileMap aren't all next to each other. Some are big blocks of physicsbodies, some are single physicsbodies with no bodies next to them. So I can't simply put every physicsbody in an array and group them.
Here's an image that shows how it looks like at the moment.
I hope the explanation is clear enough. If not, I will try to explain it better.
Has anyone done this before and can point me in the right direction? I would appreciate any help.
EDIT:
Before I tried this:
static var bodies = [SKPhysicsBody]()
static func addPhysicsBody(to tileMap: SKTileMapNode, and tileInfo: String){
let tileSize = tileMap.tileSize
let halfWidth = CGFloat(tileMap.numberOfColumns) / 2 * tileSize.width
let halfHeight = CGFloat(tileMap.numberOfRows) / 2 * tileSize.height
for column in 0..<tileMap.numberOfColumns{
for row in 0..<tileMap.numberOfRows{
let tileDefinition = tileMap.tileDefinition(atColumn: column, row: row)
let isCorrectTile = tileDefinition?.userData?[tileInfo] as? Bool
if isCorrectTile ?? false && tileInfo == "wall"{
let x = CGFloat(column) * tileSize.width - halfWidth
let y = CGFloat(row) * tileSize.height - halfHeight
let tileNode = SKNode()
tileNode.position = CGPoint(x: x, y: y)
tileNode.physicsBody = SKPhysicsBody.init(rectangleOf: tileSize, center: CGPoint(x: tileSize.width / 2, y: tileSize.height / 2))
tileNode.physicsBody!.isDynamic = false
tileNode.physicsBody!.restitution = 0.0
tileNode.physicsBody!.categoryBitMask = Constants.PhysicsCategories.wall
tileNode.physicsBody!.collisionBitMask = Constants.PhysicsCategories.player | Constants.PhysicsCategories.npc | Constants.PhysicsCategories.enemy
//nodesForGraph.append(tileNode)
bodies.append(tileNode.physicsBody!)
tileMap.addChild(tileNode)
}
}
}
tileMap.physicsBody = SKPhysicsBody(bodies: bodies)
}
But when I do this, the physicsbodies are totally messed up..
I recommend applying a line sweep algorithm to merge the tiles together.
You can do this in four steps;
Iterate through the position of the tiles in your SKTileMap.
Find the tiles that are adjacent to one another.
For each group of adjacent tiles, collect:
a down-left corner coordinate and
an up-right corner coordinate.
Draw a square, and move on to the next group of tiles until you run out of tile coordinates.
The first step: creating an array containing all of your position nodes.
func tilephysics() {
let tilesize = tileMap.tileSize
let halfwidth = CGFloat(tileMap.numberOfColumns) / 2.0 * tilesize.width
let halfheight = CGFloat(tileMap.numberOfRows) / 2.0 * tilesize.height
for col in 0 ..< tileMap.numberOfColumns {
for row in 0 ..< tileMap.numberOfRows {
if (tileMap.tileDefinition(atColumn: col, row: row)?.userData?.value(forKey: "ground") != nil) {
let tileDef = tileMap.tileDefinition(atColumn: col, row: row)!
let tile = SKSpriteNode()
let x = round(CGFloat(col) * tilesize.width - halfwidth + (tilesize.width / 2))
let y = round(CGFloat(row) * tilesize.height - halfheight + (tilesize.height / 2))
tile.position = CGPoint(x: x, y: y)
tile.size = CGSize(width: tileDef.size.width, height: tileDef.size.height)
tileArray.append(tile)
tilePositionArray.append(tile.position)
}
}
}
algorithm()
}
The second and third step: finding adjacent tiles, collecting the two corner coordinates, and adding them to an array:
var dir = [String]()
var pLoc = [CGPoint]()
var adT = [CGPoint]()
func algorithm(){
let width = tileMap.tileSize.width
let height = tileMap.tileSize.height
let rWidth = 0.5 * width
let rHeight = 0.5 * height
var ti:Int = 0
var ti2:Int = 0
var id:Int = 0
var dl:CGPoint = CGPoint(x: 0, y: 0)
var tLE = [CGPoint]()
var tRE = [CGPoint]()
for t in tilePositionArray {
if (ti-1 < 0) || (tilePositionArray[ti-1].y != tilePositionArray[ti].y - height) {
dl = CGPoint(x: t.x - rWidth, y: t.y - rHeight)
}
if (ti+1 > tilePositionArray.count-1) {
tLE.append(dl)
tRE.append(CGPoint(x: t.x + rWidth, y: t.y + rHeight))
} else if (tilePositionArray[ti+1].y != tilePositionArray[ti].y + height) {
if let _ = tRE.first(where: {
if $0 == CGPoint(x: t.x + rWidth - width, y: t.y + rHeight) {id = tRE.index(of: $0)!}
return $0 == CGPoint(x: t.x + rWidth - width, y: t.y + rHeight)}) {
if tLE[id].y == dl.y {
tRE[id] = CGPoint(x: t.x + rWidth, y: t.y + rHeight)
} else {
tLE.append(dl)
tRE.append(CGPoint(x: t.x + rWidth, y: t.y + rHeight))
}
} else {
tLE.append(dl)
tRE.append(CGPoint(x: t.x + rWidth, y: t.y + rHeight))
}
}
ti+=1
}
The fourth step: drawing a rectangle and moving on to the next shape:
for t in tLE {
let size = CGSize(width: abs(t.x - tRE[ti2].x), height: abs(t.y - tRE[ti2].y))
let loadnode = SKNode()
loadnode.physicsBody = SKPhysicsBody(rectangleOf: size)
loadnode.physicsBody?.isDynamic = false
loadnode.physicsBody?.affectedByGravity = false
loadnode.physicsBody?.restitution = 0
loadnode.physicsBody?.categoryBitMask = 2
loadnode.position.x = t.x + size.width / 2
loadnode.position.y = t.y + size.height / 2
scene.addChild(loadnode)
ti2 += 1
}
}
Apply these steps correctly, and you should see that your tiles are merged together in large squares; like so:
Screenshot without visuals for comparison
Screenshot without visuals showing the physicsbodies
I had a lot of fun solving this problem. If I have helped you, let me know.
I only recently started coding and am looking for new challenges. Please reach out to me if you have challenges or projects I could possibly contribute to.
As Knight0fDragon pointed out, there is no way to do exactly what you have asked. Unfortunately, tile maps in SpriteKit leave much to be desired. But you might try this technique to reduce the number of physics bodies.
Idea #1 - Manually Draw Your Physics Bodies
Create your tile map in the editor. Just paint your tile textures onto the map; don't assign any physics bodies to them. Then keep working in the editor to drag Color Sprites (SKSpriteNodes) over parts of your map that need a physics body. Shape the nodes to make the largest rectangle possible for areas that need physics bodies. This works best for for large, flat surfaces like walls, floors, ceilings, platforms, crates, etc. It's tedious but you end up with far fewer physics bodies in your simulation than if you automatically assign bodies to all tiles like you are doing.
Idea #2 - Use No Physics Bodies
This idea would probably require even more work, but you could potentially avoid using physics bodies altogether. First, create your tile map in the editor. Analyze your map to identify which tiles mark a barrier, beyond which the player should not cross. Assign a user data identifier to that type of tile. You would need different categories of identifiers for different types of barriers, and you may also need to design your artwork to fit this approach.
Once your barrier tiles are sufficiently identified, write code which checks the user data value for the tile currently occupied by the player sprite and restrict the sprite's movement accordingly. For example, if the player enters a title that marks an upper boundary, your movement code would not allow the player sprite to move up. Likewise, if the player enters a tile that marks the leftmost boundary, your movement code will not let the player travel left.
You can check out this related post where I basically suggest the same ideas. Unfortunately, SpriteKit's tile maps have no perfect solution for this problem.
In my current project I create some node that I would like to create physics bodies for.
This creates the nodes:
let seg = SKShapeNode(ellipseOf: CGSize(width: 10 / xScale, height: 10 / yScale))
let offset = (seg.frame.height / 1.5) * CGFloat(i + 1)
seg.position = CGPoint(x: anchorPoint.x, y: ((size.height / 2) / yScale) + (10 / yScale) - offset)
segments.append(seg)
addChild(seg)
And this is how I currently try to create the bodies:
seg.physicsBody = SKPhysicsBody(circleOfRadius: 10 / xScale / yScale / 2)
I dont know how I would go about creating the correct one. It should function like a normal physics body(edge based bodies don't collide with stuff).
Currently when the circles roll down it looks like this although they are initially circles:
Also they look very pixelated. Do you have any idea why?
Thanks for your help!
I'm trying to make a matching game, where these circles appear randomly in a rectangular area, without overlapping. Here's the spawning function:
func SpawnRed(){
var Red = SKSpriteNode(imageNamed: "Matched_Red")
Red.size = CGSize(width: 50, height: 50)
Red.zPosition = 1
let MinValueX = self.size.width / 3 + 50
let MaxValueX = self.size.width / 1.5 - 50
let MinValueY = self.size.height / 1.5 + 25
let MaxValueY = self.size.height / 6
let SpawnPointX = UInt32(MaxValueX - MinValueX)
let SpawnPointY = UInt32(MaxValueY - MinValueY)
Red.position = CGPointMake(CGFloat(arc4random_uniform(SpawnPointX)) + MinValueY,CGFloat(arc4random_uniform(SpawnPointY)))
self.addChild(Red)
}
But for some reason, I keep getting
"Thread 1: EXC_BAD_INSTRUCTION(code=EXC_1386_INVOP,subcode=0x0)" error.
Can you find the solution? Also, it'll be so helpful if you tell me how to spawn nodes without overlapping.