Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok so I am making a game where things fall down the screen, its at a random where they spawn at the top on the X value randomly. sometimes when they spawn they go really close to the edge of the screen to the point where they are hard to see. So what I need is some way to make them spawn a little bit farther away from the edges. here is my code
Swift
}*/
    func spawnChlorine(){
        var Chlorine = SKSpriteNode(imageNamed: "Chlorine.png")
        
        var minValue = self.size.width / 8
        var maxValue = self.size.width - 20
        var spawnPoint = UInt32(maxValue - minValue)
        Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (spawnPoint )) ,y: self.size.height)
        let action = SKAction.moveToY(-20, duration: 5.0)
        let actionDone = SKAction.removeFromParent()
        Chlorine.runAction(SKAction.sequence([action, actionDone]))
        Chlorine.runAction(SKAction.repeatActionForever(action))
        Chlorine.physicsBody = SKPhysicsBody(rectangleOfSize: Chlorine.size)
        Chlorine.physicsBody?.categoryBitMask = PhysicsCatagory.Chlorine
        Chlorine.physicsBody?.contactTestBitMask = PhysicsCatagory.Shot
        Chlorine.physicsBody?.affectedByGravity = true
        Chlorine.physicsBody?.dynamic = true
        self.addChild(Chlorine)
    }
Posted

1 solution

Rather simple.

compute once the values in global variable.

Lets say:
play field width is 800
drop from edge is 100
sprite width is 30
drop width is computed
drop offset is computed

Swift
var fw= 800
var de= 100
var sw= 30

var dw= fw-2*de
var do= de-sw/2


and in your function, change to this line
Swift
Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (dw )+do) ,y: self.size.height)


variable names are up to you.

Update: I don't know swift, but this change should fit
Swift
func spawnChlorine(){
    var Chlorine = SKSpriteNode(imageNamed: "Chlorine.png")

    var fw= self.size.width
    var de= 100
    var sw= 30

    var dw= UInt32(fw-2*de) // dw is the narrow width of drop
    var ok= UInt32(de-sw/2) // ok is an offset to center the drops
    Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (dw )+ok) ,y: self.size.height)
    let action = SKAction.moveToY(-20, duration: 5.0)
    let actionDone = SKAction.removeFromParent()
    Chlorine.runAction(SKAction.sequence([action, actionDone]))
    Chlorine.runAction(SKAction.repeatActionForever(action))
    Chlorine.physicsBody = SKPhysicsBody(rectangleOfSize: Chlorine.size)
    Chlorine.physicsBody?.categoryBitMask = PhysicsCatagory.Chlorine
    Chlorine.physicsBody?.contactTestBitMask = PhysicsCatagory.Shot
    Chlorine.physicsBody?.affectedByGravity = true
    Chlorine.physicsBody?.dynamic = true
    self.addChild(Chlorine)
}
 
Share this answer
 
v5
Comments
[no name] 23-Aug-15 22:03pm    
ok so i did that and i now (btw i changed the var do to ok) i get 2 errors. Binary operator '+' cannot be applied to operands of type 'UInt32' and 'Int' and /Users/Alex/Desktop/Algae Escape/Algae Escape/GameScene.swift:103:29: Cannot find an initializer for type 'CGPoint' that accepts an argument list of type '(x: CGFloat, y: CGFloat)' this is my code: }*/
func spawnChlorine(){
var Chlorine = SKSpriteNode(imageNamed: "Chlorine.png")
var fw = 800
var de = 100
var sw = 30

var dw = fw-2*de
var ok = de-sw/2
var minValue = self.size.width / 8
var maxValue = self.size.width - 20
//var spawnPoint = UInt32((maxValue - minValue))
//Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (spawnPoint )) ,y: self.size.height)
Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (dw) + ok) ,y: self.size.height)

let action = SKAction.moveToY(-20, duration: 5.0)
let actionDone = SKAction.removeFromParent()
Chlorine.runAction(SKAction.sequence([action, actionDone]))
Chlorine.runAction(SKAction.repeatActionForever(action))
Chlorine.physicsBody = SKPhysicsBody(rectangleOfSize: Chlorine.size)
Chlorine.physicsBody?.categoryBitMask = PhysicsCatagory.Chlorine
Chlorine.physicsBody?.contactTestBitMask = PhysicsCatagory.Shot
Chlorine.physicsBody?.affectedByGravity = true
Chlorine.physicsBody?.dynamic = true

self.addChild(Chlorine)
}
Patrice T 24-Aug-15 1:39am    
see solution update

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900