Float is not convertible to CGFloat and CGFloat is not convertible to Float - iphone

I have a problem with swift on XCode 6 beta 4, is driving me nuts I'm in IOS 8 developing a game and I have follow a tutorial but I get this: Float is not convertible to CGFloat and then when I recast as CGFloat I get the other. Here is the code:
override func didMoveToView(view: SKView) {
/* Setup your scene here */
//physics
self.physicsWorld.gravity = CGVectorMake(0.0, -5.0)
// Bird
var birdTexture = SKTexture(imageNamed: "kirby")
birdTexture.filteringMode = SKTextureFilteringMode.Nearest
bird.setScale(0.5)
bird.position = CGPoint(x: self.frame.size.width * 0.35, y: self.frame.size.height * 0.6)
bird.physicsBody = SKPhysicsBody(circleOfRadius:bird.size.height/2)
bird.physicsBody.dynamic = true
bird.physicsBody.allowsRotation = false
self.addChild(bird)
//ground
ground.setScale(2.0)
ground.position = CGPointMake(self.size.width/2, ground.size.height/2)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width , ground.size.height))
ground.physicsBody.dynamic = false
self.addChild(ground)
//pipes
//create pipes
//^
//movement of the pipes
let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width)
let movePipe = SKAction.moveByX(-distanceToMove, y: CGFloat(0.0), duration: NSTimeInterval(0.01) * distanceToMove) <----- this line is the problem
}
What is going on, I move the line:
CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width)
I get the second error, then a recast:
Float(CGFloat(self.frame.size.width + 2.0 * pipeUp.texture.size().width))
or
Float(self.frame.size.width + 2.0 * pipeUp.texture.size().width)
gives me the first, so what do swift wants here, that is insane. Catch 22? Any help?
EDIT: I'm using a 13-inch mac late 2009, Intel Core 2 Duo, on mavericks OS X 10.9.4 (13E28) if any help. I saw something about the building options may affect the float types, but I do not know where are they.

This will be interpreted as a CGFloat and not as a NSTimeInterval as you cast and then multiply:
let movePipe = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01) * distanceToMove)
Try to multiply then cast to NSTimeInterval:
let movePipe = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
Also, as suggested already, you don't need to cast your distance. The following will work just fine:
let distanceToMove = self.frame.size.width + 2.0 * pipeUp.texture.size().width

Just make sure every operand has the same type. Literals (2.0) have always their type inferred. In your case, I see no problem because
let distanceToMove: CGFloat = self.frame.size.width + 2.0 * pipeUp.texture.size().width
has all the operands of type CGFloat so there is no need to cast at all.
On the next line, again make sure to use the correct types. You don't need to cast the literals:
SKAction.moveByX(-distanceToMove, y: 0.0, duration: 0.01 * NSTimeInterval(distanceToMove))

Related

CALayer bounds don't change after setting them to a certain value

I am trying to display an image as the content of a CALayer slightly zoomed in by changing its bounds to a bigger size. (This is so that I can pan over it later.)
For some reason however setting the bounds does not change them or trigger an animation to do so.
This is the code I use to change the bounds:
self.imageLayer.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
I have a function to compute the CGRect, but this dummy one leads to exactly the same result of the size not changing.
I have also determined, that while I can't see the size change, if I check the bounds of the layer right after setting it, it correctly has the value I set it to.
The following code is executed after setting the bounds. I couldn't find anything in it, that changes them back.
self.imageLayer.add(self.generatePanAnimation(), forKey: "pan")
func generatePanAnimation() -> CAAnimation {
var positionA = CGPoint(x: (self.bounds.width / 2), y: self.bounds.height / 2)
var positionB = CGPoint(x: (self.bounds.width / 2), y: self.bounds.height / 2)
positionA = self.generateZoomedPosition()
positionB = self.generateZoomedPosition()
let panAnimation = CABasicAnimation(keyPath: "position")
if self.direction == .AtoB {
panAnimation.fromValue = positionA
panAnimation.toValue = positionB
} else {
panAnimation.fromValue = positionB
panAnimation.toValue = positionA
}
panAnimation.duration = self.panAndZoomDuration
self.panAnimation = panAnimation
return panAnimation
}
func generateZoomedPosition() -> CGPoint {
let maxRight = self.zoomedImageLayerBounds.width / 2
let maxLeft = self.bounds.width - (self.zoomedImageLayerBounds.height / 2)
let maxUp = self.zoomedImageLayerBounds.height / 2
let maxDown = self.bounds.height - (self.zoomedImageLayerBounds.height / 2)
let horizontalFactor = CGFloat(arc4random()) / CGFloat(UINT32_MAX)
let verticalFactor = CGFloat(arc4random()) / CGFloat(UINT32_MAX)
let randomX = maxLeft + horizontalFactor * (maxRight - maxLeft)
let randomY = maxDown + verticalFactor * (maxUp - maxDown)
return CGPoint(x: randomX, y: randomY)
}
I even tried setting the bounds as shown below, but it didn't help.
CATransaction.begin()
CATransaction.setValue(true, forKey: kCATransactionDisableActions)
self.imageLayer.bounds = CGRect(x: 0, y: 0, width: 10, height: 10)
CATransaction.commit()
I really hope someone has an idea. Thanks a lot!
The way to change the apparent drawing size of a layer is not to change its bounds but to change its transform. To make the layer look larger, including its drawing, apply a scale transform.

Swift 3 - Background loop not working properly

I am trying to make a Flappy Bird like game and have to make my background loop infinitely. I found some tutorials online but it is just not working for me and I can't figure out why.
This is my code so far
var Background = SKSpriteNode()
var txtBG = SKTexture(imageNamed: "Background")
var txtLand = SKTexture(imageNamed: "Ground")
var Ground = SKSpriteNode()
var moving:SKNode!
// Cena
override func didMove(to view: SKView) {
scene?.anchorPoint = CGPoint(x: 0, y: 0)
moving = SKNode()
self.addChild(moving)
//Ground
Ground = SKSpriteNode(texture: txtLand)
Ground.position = CGPoint(x:0, y: txtLand.size().height * 1.3)
Ground.setScale(2.5)
self.addChild(Ground)
//Background
Background = SKSpriteNode(texture: txtBG)
Background.setScale(2.0)
Background.position = CGPoint(x: self.size.width, y: txtLand.size().height * 5.2 )
let BGmove = SKAction.moveBy(x: -txtBG.size().width * 2.0, y: txtLand.size().height * 5.2 , duration: TimeInterval(15))
let BGRepo = SKAction.moveBy(x: txtBG.size().width * 2.0, y: txtLand.size().height * 5.2, duration: 0.0)
let BGLoop = SKAction.repeatForever(SKAction.sequence([BGmove,BGRepo]))
for i in 0 ..< 2 + Int(self.frame.size.width / ( txtBG.size().width * 2 )) {
let i = CGFloat(i)
Background = SKSpriteNode(texture: txtBG)
Background.setScale(2.0)
Background.position = CGPoint(x:txtBG.size().width/2 + txtBG.size().width * i * 2.0, y: txtLand.size().height * 5.2)
Background.run(BGLoop)
self.addChild(Background)
}
What happens is the background starts moving horizontally but also moving upwards and I don't understand why.
Any help will be much appreciated!
Your Background Move actions have the background moving by x and by y. If you want to use SKAction.moveBy(x: CGFloat, y: CGFloat, duration: TimeInterval) then set y to 0 or you can use SKAction.moveTo(x: CGFloat, duration: TimeInterval) to only move the x value.

Changing to swift 3. Line of code now returns immutable value error [duplicate]

I converted a project from Swift 2 to Swift 3 and now I get an error in the following line:
for i:CGFloat in 0 ..< 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ) += 1 {
which says: left side of mutating operator isn't mutable '..<' returns immutable value
let skyTexture = SKTexture(imageNamed: "sky")
skyTexture.filteringMode = .nearest
let moveSkySprite = SKAction.moveBy(x: -skyTexture.size().width * 2.0, y: 0, duration: TimeInterval(0.1 * skyTexture.size().width * 2.0))
let resetSkySprite = SKAction.moveBy(x: skyTexture.size().width * 2.0, y: 0, duration: 0.0)
let moveSkySpritesForever = SKAction.repeatForever(SKAction.sequence([moveSkySprite,resetSkySprite]))
for i:CGFloat in 0 ..< 2.0 + self.frame.size.width / ( skyTexture.size().width * 2.0 ) += 1 {
let sprite = SKSpriteNode(texture: skyTexture)
sprite.setScale(2.0)
sprite.zPosition = -20
sprite.position = CGPoint(x: i * sprite.size.width, y: sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
sprite.run(moveSkySpritesForever)
moving.addChild(sprite)
}
In essence what you want is still a loop through integer values incremented by 1. So you can just calculate the last int value and use a simple for each loop:
for i in 0..<Int(self.frame.size.width / ( skyTexture.size().width * 2.0 )) + 2 {
//do your stuff here
sprite.position = CGPoint(x: CGFloat(i) * sprite.size.width, y: sprite.size.height / 2.0 + groundTexture.size().height * 2.0)
}

Swift - SKAction moveByX, How to get my node to move from the top/down

please forgive me as I'm a bit of a Swift noob and am creating my first game :D
Ok, so I have two nodes... one is Guava and the other is Pepper. Guava moves from the bottom of the screen to the top. I want the Pepper to move from the top of the screen to the bottom. Here's the code I have, which works fine for the Guava.
// movement of guavas
let guavaToMove = CGFloat(self.frame.size.width + 2.0 * guavaNodeTexture.size().width)
let moveGuava = SKAction.moveByX(-guavaToMove, y: self.frame.size.width, duration: NSTimeInterval(0.01 * guavaToMove))
let removeGuava = SKAction.removeFromParent()
// movement of peppers
let pepperToMove = CGFloat(self.frame.size.width + 2.0 * pepperNodeTexture.size().width)
let movePepper = SKAction.moveByX(-pepperToMove, y: self.frame.size.width, duration: NSTimeInterval(0.01 * pepperToMove))
let removePepper = SKAction.removeFromParent()
GuavaMoveAndRemove = SKAction.sequence([moveGuava,removeGuava])
PepperMoveAndRemove = SKAction.sequence([movePepper,removePepper])
Now I have the Guava Node anchored to the bottom of the screen and it floats up perfectly. I have the Pepper Node anchored to the top of the screen and am trying to get it to float down in a similar fashion.
Here is the code for both functions, spawnGuava and spawnPepper where both nodes/textures are anchored:
func spawnPepper(){
//The way the Guava spawn and move
let pepperNode = SKNode()
let pepper = SKSpriteNode(texture: pepperNodeTexture)
pepper.setScale(0.30)
pepper.position = CGPointMake (self.size.width + 140, self.size.height * 0.75)
pepper.alpha = 0.75
pepper.physicsBody = SKPhysicsBody(rectangleOfSize: pepper.size)
pepper.physicsBody?.affectedByGravity = false
pepper.physicsBody?.dynamic = true
pepper.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
pepper.physicsBody?.collisionBitMask = 1
pepper.physicsBody?.contactTestBitMask = PhysicsCatagory.Boognish
pepper.zPosition = 50
pepperNode.addChild(pepper)
pepper.runAction(PepperMoveAndRemove)
self.addChild(pepperNode)
if scoreIncreased == true {
}
//the way the pepper collide
}
func spawnGuava(){
//The way the Guava spawn and move
let guavaNode = SKNode()
let guava = SKSpriteNode(texture: guavaNodeTexture)
guava.setScale(0.75)
guava.position = CGPointMake (self.size.width - 40, self.size.height * 0.05)
guava.physicsBody = SKPhysicsBody(rectangleOfSize: guava.size)
guava.physicsBody?.affectedByGravity = false
guava.physicsBody?.dynamic = true
guava.alpha = 0.75
guava.physicsBody?.categoryBitMask = PhysicsCatagory.Guava
guava.physicsBody?.collisionBitMask = 1
guava.physicsBody?.contactTestBitMask = PhysicsCatagory.Boognish
guava.zPosition = 0
guavaNode.addChild(guava)
guava.runAction(GuavaMoveAndRemove)
self.addChild(guavaNode)
if scoreIncreased == true {
}
//the way the guava collide
}
Please help me configure this code accordingly so that the peppers fall from the sky :)
THANKS in advance. First post :)
FYI: I have tried modifying the code many times myself to no avail, so I have reverted it to the baseline you see now. IF I anchored the pepper to where the Guava is anchored then both would float up at the same time.
You're trying to move guavas and peppers in the opposite direction, but it seems like you are moving them in the same direction. Try removing or adding negative signs in movePepper:
let movePepper = SKAction.moveByX(-pepperToMove, y: -self.frame.size.width, duration: NSTimeInterval(0.01 * pepperToMove))
// movement of peppers
let pepperToMove = CGFloat(self.frame.size.width + 2.0 * pepperNodeTexture.size().width)
let movePepper = SKAction.moveByX(-pepperToMove, y: self.frame.size.height / -1, duration: NSTimeInterval(0.01 * pepperToMove))
let removePepper = SKAction.removeFromParent()
just has to divide the y value by a negative, derp... :D

Need help incorporating randoms in Swift

I am trying to get my sprites on random positions on the screen but it says "CGFloat is not convertible to Double"
var randomNumber = arc4random()
bat1 = SKSpriteNode(imageNamed: "rsz_silverbat.png")
bat1.position = CGPoint(x: self.frame.size.width * 0.1, y: self.frame.size.height * randomNumber)
bat1.position = CGPoint(x: self.view.frame.size.width * CGFloat(0.1), y: self.view.frame.size.height * CGFloat(randomNumber))