I am making a plat form game, but I am having an issue where only one of the movement buttons work. Can someone please take some time to review my code and see what is wrong with it?
I do not get my errors, my guess is that the there is a problem down where I change the sod variable. When I run the app and click and hold the "left button, it moves a little but then slows to a stop. It is as if the script where I changed the variable was only run one time, even though I hold it down. Interestingly enough, before when I ran it it was just fine. Unfortunately, I do not remember what I changed.
Thanks, James
class GameScene: SKScene {
var alien = SKSpriteNode()
var left = SKSpriteNode()
var right = SKSpriteNode()
var jump = SKSpriteNode()
var level = SKSpriteNode()
var background = SKSpriteNode()
var myLabel = SKLabelNode(fontNamed:"Chalkduster")
var playerLight = SKLightNode()
var xspd :Double = 0
var touching :Bool = false
var alienFrame :String = "stand"
var textureFrames :Double = 0
var buttonTouching :Bool = true
var buttonCheck :Int = 0
var ninety :Double = 0
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -9.8)
alien = SKSpriteNode(imageNamed: "alien_stand")
alien.physicsBody = SKPhysicsBody(circleOfRadius: alien.frame.height / 2)
alien.position = CGPoint(x: self.frame.width / 6, y: self.frame.height / 2)
alien.xScale = 0.7
alien.yScale = 0.7
alien.physicsBody?.affectedByGravity = true
alien.physicsBody?.dynamic = true
alien.physicsBody?.allowsRotation = false
alien.zPosition = 1
alien.zRotation = 0
self.addChild(alien)
left = SKSpriteNode(imageNamed: "left")
left.position = CGPoint(x: self.frame.width / 8, y: self.frame.height / 3.5)
left.physicsBody?.pinned = true
left.xScale = 2
left.yScale = 2
left.zPosition = 3
left.alpha = 0.4
self.addChild(left)
right = SKSpriteNode(imageNamed: "right")
right.position = CGPoint(x: self.frame.width / 3, y: self.frame.height / 3.5)
right.physicsBody?.pinned = true
right.xScale = 2
right.yScale = 2
right.zPosition = 4
right.alpha = 0.4
self.addChild(right)
jump = SKSpriteNode(imageNamed: "up")
jump.position = CGPoint(x: (self.frame.width / 8) * 7, y: self.frame.height / 3.5)
jump.physicsBody?.pinned = true
jump.xScale = 2
jump.yScale = 2
jump.zPosition = 5
jump.alpha = 0.4
self.addChild(jump)
myLabel.text = "Hello, World!";
myLabel.fontSize = 45;
myLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame));
self.addChild(myLabel)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = true
if let touch = touches.first {
if xspd > -40 && xspd < 40 {
buttonCheck = 0
if jump.containsPoint(touch.locationInNode(self)) {
alien.physicsBody?.applyImpulse(CGVectorMake(0, 250))
jump.alpha = 0.1
alien.texture = SKTexture(imageNamed: "alien_jump")
buttonCheck += 1
}
if left.containsPoint(touch.locationInNode(self)) {
xspd -= 6
left.alpha = 0.1
alien.xScale = -0.7
buttonCheck += 1
}
if right.containsPoint(touch.locationInNode(self)) {
xspd += 6
right.alpha = 0.1
alien.xScale = 0.7
buttonCheck += 1
}
if buttonCheck > 0 {
buttonTouching = true
}
else {
buttonTouching = false
}
}
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touching = false
}
override func update(currentTime: CFTimeInterval) {
updatePositions()
playerLight.position = CGPoint(x: alien.position.x, y: alien.position.y)
textureFrames += 1
}
func updatePositions() {
myLabel.text = String(round(xspd))
alien.physicsBody?.applyImpulse(CGVector(dx: CGFloat(xspd), dy: 0))
if touching == true && buttonTouching == true && xspd > 0 {
if xspd > 0 {
ninety = 900
}
else {
ninety = -900
}
if alienFrame == "stand" {
alien.texture = SKTexture(imageNamed: "alien_walk1")
alienFrame = "walk1"
}
else {
if alienFrame == "walk1" {
if textureFrames > 9.9 / xspd {
alien.texture = SKTexture(imageNamed: "alien_walk2")
alienFrame = "walk2"
textureFrames = 0
}
}
else {
if alienFrame == "walk2" {
if textureFrames > 9.9 / xspd {
alien.texture = SKTexture(imageNamed: "alien_walk1")
alienFrame = "walk1"
textureFrames = 0
}
}
}
}
}
else {
if xspd < 0.6 && xspd > -0.6 {
alien.texture = SKTexture(imageNamed: "alien_stand")
}
else {
if xspd != 0 {
xspd = xspd * 0.85
}
}
right.alpha = 0.4
left.alpha = 0.4
jump.alpha = 0.4
}
}
}
When you start the game, the xspd variable starts off with a value of zero. When tapping the left node, you subtract 6 from the xspd giving you the result of -6 on the xspd. Further you have the updatePositions() function, which is called every frame I suppose and within that function you apply an impulse using a vector based on the xspd value. The if-condition after that is never fulfilled when tapping the left node first since you get a negative xspd value that breaks the condition (&& xspd > 0), hence you never get any animations with an initial velocity with a negative x.
To fix the animation, you could encapsulate the xspd in an abs() which will always return a positive number.
&& abs(xspd) > 0
The next issue is that your player stops moving because if you tap and hold on the left node, you won´t be able to maintain the movement speed without having to tap the left node repeatedly.
A suggestion from me, you could try the following:
if xspd < 0.6 && xspd > -0.6 {
alien.texture = SKTexture(imageNamed: "alien_stand")
} else if touching == false && xspd != 0 { // Never attempt to decrease movement speed if the player is in fact touching a movement button
xspd = xspd * 0.85
}
I hope I've understood your problem correctly and that this is helpful
Related
I am trying to find a way to shoot more than one bullets as power up increases. Also it's only going up straight on the first 4 power ups, but I would like it to have a little angle as it reaches 5 and up.
Can someone help me implement that with the following codes I currently have?
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var player: SKSpriteNode!
var scoreLabel: SKLabelNode!
var score: Int = 0 {
didSet {
scoreLabel.text = "Score: \(score)"
}
}
var gameTimer: Timer!
var possibleAliens = ["alien", "alien2", "alien3"]
//bitmask for alien and torpedo's physics body
let alienCategory:UInt32 = 0x1 << 1
let photonTorpedoCategory:UInt32 = 0x1 << 0
//lives
var livesArray:[SKSpriteNode]!
//powerUp
var powerUp: Int = 1
//didMove
override func didMove(to view: SKView) {
addLives()
starField = SKEmitterNode(fileNamed: "Starfield")
starField.position = CGPoint(x: 0, y: 1472)
starField.advanceSimulationTime(10)
self.addChild(starField)
starField.zPosition = -1
player = SKSpriteNode(imageNamed: "shuttle")
player.position = CGPoint(x: self.frame.size.width / 3.6, y: player.size.height / 2 + 20)
self.addChild(player)
//physicsWorld
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
//score and scoreLabel
scoreLabel = SKLabelNode(text: "Score: 0")
scoreLabel.position = CGPoint(x: 80, y: self.frame.size.height - 60)
scoreLabel.fontName = "AmericanTypewriter-Bold"
scoreLabel.fontSize = 28
scoreLabel.fontColor = UIColor.white
score = 0
self.addChild(scoreLabel)
//create a timeInterval that can be changed depending on the difficulty
var timeInterval = 0.6
if UserDefaults.standard.bool(forKey: "hard"){
timeInterval = 0.2
}
//gameTimer
gameTimer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(addAlien), userInfo: nil, repeats: true)
//motion Manager initialization in didMove
motionManger.accelerometerUpdateInterval = 0.2
//creatingan acceleration data in didMove
motionManger.startAccelerometerUpdates(to: OperationQueue.current!) { (data: CMAccelerometerData?, error: Error?) in
if let accelerometerData = data{
let acceleration = accelerometerData.acceleration
self.xAcceleration = CGFloat(acceleration.x) * 0.75 + self.xAcceleration * 0.25
}
}
}
//func addLives
func addLives() {
//initialize livesArray from GameScene
livesArray = [SKSpriteNode]()
for live in 1 ... 3 {
let liveNode = SKSpriteNode(imageNamed: "shuttle")
liveNode.name = "live\(live)"
liveNode.position = CGPoint(x: self.frame.size.width - CGFloat((4-live)) * liveNode.size.width, y: self.frame.size.height - 60)
self.addChild(liveNode)
livesArray.append(liveNode)
}
}
//func addAlien
func addAlien() {
//using GK, pick possibleAliens[arrays] randomly, and shuffle
possibleAliens = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: possibleAliens) as! [String]
//bring the aliens to random position
let alien = SKSpriteNode(imageNamed: possibleAliens[0])
let randomAlienPosition = GKRandomDistribution(lowestValue: 0, highestValue: 414)
//make the position constant, use randomAlien and get next integer and use CGFloat then set alien position
let position = CGFloat(randomAlienPosition.nextInt())
alien.position = CGPoint(x: position, y: self.frame.size.height + alien.size.height)
//physicsBody of addAlien
alien.physicsBody = SKPhysicsBody(rectangleOf: alien.size)
alien.physicsBody?.isDynamic = true
alien.physicsBody?.categoryBitMask = alienCategory
alien.physicsBody?.contactTestBitMask = photonTorpedoCategory
alien.physicsBody?.collisionBitMask = 0
self.addChild(alien)
//make aliens move
let animationDuration: TimeInterval = 6
//SKAction to alien will make alien move from top to bottom of the screen, then remove alien from screen and from parent so it doesnt consume too much memory
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: position, y: -alien.size.height), duration: animationDuration))
//THIS ACTION WILL SEE IF IT REACHES THE FINAL DESTINATION BEFORE IT GETS ERASED AND TAKES A LIFE
//A RUN ACTION THAT WILL PLAY SOUND WHEN PLAYER LOSES SOUND.
actionArray.append(SKAction.run{
self.run(SKAction.playSoundFileNamed("loose.mp3", waitForCompletion: false))
if self.livesArray.count > 0 {
let liveNode = self.livesArray.first
liveNode!.removeFromParent()
self.livesArray.removeFirst()
if self.livesArray.count == 0{
let transition = SKTransition.flipHorizontal(withDuration: 0.5)
let gameOver = SKScene(fileNamed: "GameOverScene") as! GameOverScene
gameOver.score = self.score
self.view?.presentScene(gameOver, transition: transition)
}
}
})
actionArray.append(SKAction.removeFromParent())
//make a run function on the alien to pass allong actionArray
alien.run(SKAction.sequence(actionArray))
}
//fire fireTorpedo when tapped
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
fireTorpedo()
}
//func FireTorpedo or bullet
func fireTorpedo(){
//adds sound, image and position of the torpedo
self.run(SKAction.playSoundFileNamed("torpedo.mp3", waitForCompletion: false))
let torpedoNode = SKSpriteNode(imageNamed: "torpedo")
//powerUp switch
switch (powerUp)
{
case 1:
torpedoNode.position.x = player.position.x
torpedoNode.position.y = player.position.y
case 2:
torpedoNode.position.y = player.position.y
torpedoNode.position.x = player.position.x - 10
torpedoNode.position.x = player.position.x + 10
default:
print("out of torpedo ammo")
break
}
torpedoNode.position.y += 5
//add physicsBody for torpedo just like the aliens
torpedoNode.physicsBody = SKPhysicsBody(circleOfRadius: torpedoNode.size.width / 2)
torpedoNode.physicsBody?.isDynamic = true
torpedoNode.physicsBody?.categoryBitMask = photonTorpedoCategory
torpedoNode.physicsBody?.contactTestBitMask = alienCategory
torpedoNode.physicsBody?.collisionBitMask = 0
torpedoNode.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(torpedoNode)
//add animation like in alien
let animationDuration: TimeInterval = 0.3
//make torpedo move up and disappear
var actionArray = [SKAction]()
actionArray.append(SKAction.move(to: CGPoint(x: player.position.x, y: self.frame.size.height + 10), duration: animationDuration))
actionArray.append(SKAction.removeFromParent())
//run the torpedo
torpedoNode.run(SKAction.sequence(actionArray))
}
func didBegin(_ contact: SKPhysicsContact){
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
//check if two bodies touch
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask{
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
//to findout which body is the torpedo and which is alien
if (firstBody.categoryBitMask & photonTorpedoCategory) != 0 && (secondBody.categoryBitMask & alienCategory) != 0 {
torpedoDidCollideWithAlien(torpedoNode: firstBody.node as? SKSpriteNode, alienNode: secondBody.node as? SKSpriteNode)
}
}
func torpedoDidCollideWithAlien (torpedoNode: SKSpriteNode?, alienNode: SKSpriteNode?){
if let explosion = SKEmitterNode(fileNamed: "Explosion"){
if let alien = alienNode{
explosion.position = alien.position
}
self.addChild(explosion)
self.run(SKAction.playSoundFileNamed("explosion.mp3", waitForCompletion: false))
if let torpedo = torpedoNode{
torpedo.removeFromParent()
}
if let alien = alienNode{
alien.removeFromParent()
}
//see the explosion effect longer and not disappear immediately with run function with action and completion handler
self.run(SKAction.wait(forDuration: 2)){
explosion.removeFromParent()
}
//add and update score
score += 5
}
}
override func didSimulatePhysics() {
player.position.x += xAcceleration * 50
if player.position.x < -20 {
player.position = CGPoint(x: self.size.width + 20, y: player.position.y)
} else if player.position.x > self.size.width + 20 {
player.position = CGPoint(x: -20, y: player.position.y)
}
}
}
So far, I almost finished my game. I have a ball that falls due to gravity and the person taps it to make it bounce. Every time the sprite is tapped, +1 is added to the score. There are 3 balls in total that show up when the view loads.I want each ball to show up at a certain point mark(one ball shows up when the person reaches 10 points, another when the player reaches 20).
I tried putting this in update(currentTime: CFTimeInterval), but I end up with this:
(This is the secondball coming in at 10 points.)
Seems like there are an infinite number of balls until the person gets to 21 points which stops the never-ending cascade. If you tap the cascade though, it does pick a ball and makes it jump out which is sorta what I wanted.
This is GameScene.swift (excluding the update(CFTimeInterval) function)
import SpriteKit
class GameScene: SKScene {
var ball: Ball!
var secondball: Ball!
override func didMoveToView(view: SKView) {
//=======Ball 1=======//
let ball = Ball()
ball.position = CGPoint(x:self.frame.midX, y:440)
addChild(ball)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
ball.physicsBody?.dynamic = true
ball.physicsBody?.allowsRotation = false
ball.physicsBody?.friction = 0
ball.physicsBody?.angularDamping = 0
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.usesPreciseCollisionDetection = true
ball.physicsBody!.categoryBitMask = 0
//======Ball 2======//
let secondball = Ball()
secondball.position = CGPoint(x:self.frame.midX * 1.65, y: 440)
addChild(secondball)
secondball.physicsBody = SKPhysicsBody(circleOfRadius: 90)
secondball.physicsBody?.dynamic = false
secondball.physicsBody?.allowsRotation = false
secondball.physicsBody?.friction = 0
secondball.physicsBody?.angularDamping = 0
secondball.physicsBody?.linearDamping = 0
secondball.physicsBody?.usesPreciseCollisionDetection = true
secondball.physicsBody!.categoryBitMask = 0
}
class Ball: SKSpriteNode {
init() {
let texture = SKTexture(imageNamed: "Ball")
super.init(texture: texture, color: .clearColor(), size: texture.size())
userInteractionEnabled = true
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let scene = self.scene as! GameScene
scene.score += 1
physicsBody?.velocity = CGVectorMake(0, 100)
physicsBody?.applyImpulse(CGVectorMake(0, 900))
}
}
override func update(currentTime: CFTimeInterval) {
if (score >= 10){
self.backgroundNode.texture = SKTexture(imageNamed: "orangebackground")
}
if (score >= 20){
self.backgroundNode.texture = SKTexture(imageNamed: "yellowbackground")
}
if (score >= 30) {
self.backgroundNode.texture = SKTexture(imageNamed: "greenbackground")
}
if (score >= 40){
self.backgroundNode.texture = SKTexture(imageNamed: "bluebackground")
}
if (score >= 50){
self.backgroundNode.texture = SKTexture(imageNamed: "darkbluebackground")
}
if (score >= 60){
self.backgroundNode.texture = SKTexture(imageNamed: "purplebackground")
}
if (score >= 70){
self.backgroundNode.texture = SKTexture(imageNamed: "brownbackground")
}
if (score >= 80) {
self.backgroundNode.texture = SKTexture(imageNamed: "maroonbackground")
}
if (score >= 90){
self.backgroundNode.texture = SKTexture(imageNamed: "tanbackground")
}
if (score >= 100){
self.backgroundNode.texture = SKTexture(imageNamed: "pinkbackground")
}
if (score >= 125) {
self.backgroundNode.texture = SKTexture(imageNamed: "bronzebackground")
}
if (score >= 150) {
self.backgroundNode.texture = SKTexture(imageNamed: "silverbackground")
}
if (score >= 175) {
self.backgroundNode.texture = SKTexture(imageNamed: "goldbackground")
}
if (score >= 200) {
self.backgroundNode.texture = SKTexture(imageNamed: "elitebackground")
}
}
}
The update method is called faster then the increased score. Therefore in every call of the update method a new ball is added.
Try something like this:
Move this line of code outside of the update method to make a global variable which has one ball instance:
let secondball = Ball()
Inside of update:
if (score == 10) && secondball.parent == nil {
You have choosed to control the touches part to the Ball class, this is possible but not convenient.
First of all give a name to your balls during the creation:
ball.name = "ball1"
self.addChild(ball)
...
secondball.name = "ball2"
secondball.alpha = 0.0 // hide your secondBall at start
self.addChild(secondball)
Then , move the touchesBegan from your Ball class, to your GameScene:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let positionInScene = touch!.locationInNode(self)
let touchedNode = self.nodeAtPoint(positionInScene)
if let name = touchedNode.name {
switch name {
case "ball1": // if you're touching ball1
self.score += 1
ball.physicsBody?.velocity = CGVectorMake(0, 100)
ball.physicsBody?.applyImpulse(CGVectorMake(0, 900))
if score == 10 {
ball.removeFromParent()
// or make some animation like:
//let fadeOut=SKAction.fadeOutWithDuration(1.0)
//ball.runAction(fadeOut,completion: {
// self.ball.removeFromParent()
//})
secondball.alpha = 1
// or make some animation like:
//let fadeIn=SKAction.fadeInWithDuration(0.5)
//secondball.runAction(fadeIn,completion: {
// self.secondball.physicsBody?.dynamic = true
//})
secondball.physicsBody?.dynamic = true
}
case "ball2": // if you're touching ball2
self.score += 1
secondball.physicsBody?.velocity = CGVectorMake(0, 100) // here you can decrease parameters to more difficult
secondball.physicsBody?.applyImpulse(CGVectorMake(0, 900))
if score == 20 {
// do the same thing maked for ball to show other ball
}
case "ball3": // if you're touching ball3
self.score += 1
secondball.physicsBody?.velocity = CGVectorMake(0, 100) // here you can decrease parameters to more difficult
secondball.physicsBody?.applyImpulse(CGVectorMake(0, 900))
if score == 21 {
// do other stuff here
}
default:
break
}
}
}
I created an SKAction that repeatsForever (it's the planet spinning). But I want to stop it once the lander lands on the landing pad. So far my code is not working. I tried both removeAllActions() and removeActionForKey as you'll see. The contact detection works just fine, and there's a bunch of code not shown which includes the collision delegate, etc.
func createPlanet() {
var planet = SKSpriteNode()
planet.zPosition = 1
planet.name = "mars"
redPlanet = SKSpriteNode(imageNamed: "redPlanet")
redPlanet.name = "red"
redPlanet.zPosition = 2
redPlanet.physicsBody = SKPhysicsBody(texture: redPlanet.texture!, size: size)
redPlanet.physicsBody!.dynamic = false
redPlanet.physicsBody!.categoryBitMask = planetMask
redPlanet.physicsBody!.contactTestBitMask = 0
planet.addChild(redPlanet)
landingPad = SKSpriteNode(imageNamed: "landingPad")
landingPad.name = "pad"
landingPad.zPosition = 3
landingPad.position = CGPoint(x: 0, y: redPlanet.size.height / 2 - 60)
landingPad.physicsBody = SKPhysicsBody(rectangleOfSize: landingPad.size)
landingPad.physicsBody!.dynamic = false
landingPad.physicsBody!.categoryBitMask = landingPadMask
landingPad.physicsBody!.collisionBitMask = landerMask
landingPad.physicsBody!.contactTestBitMask = landerMask
planet.addChild(landingPad)
planet.position = CGPoint(x: frame.size.width / 2, y: -redPlanet.size.height / 6)
let spinner = SKAction.rotateByAngle(CGFloat(M_PI), duration: 3)
planet.runAction(SKAction.repeatActionForever(spinner), withKey: "planetSpin")
addChild(planet)
}
And this...
func didBeginContact(contact: SKPhysicsContact) {
if !hasLanded {
if contact.bodyA.node!.name == "lander" {
hasLanded = true
print("bodyA contact")
physicsWorld.speed = 0
removeAllActions()
removeActionForKey("planetSpin")
} else if contact.bodyB.node!.name == "lander" {
print("bodyB contact")
hasLanded = true
physicsWorld.speed = 0
removeAllActions()
removeActionForKey("planetSpin")
}
}
}
You're not removing actions from the planet node, you're removing them from the scene or parent node or whatever your didBeginContact is a member of.
Make planet a class variable and in didBeginContact, call planet.removeAllActions()
I am having an issue where my platforms distance becomes messed up after a certain score has been reached. I want the game to start just like the spawnDelayForever part in the touchesBegan until the score reaches 5. After the score reaches 5…10…15 and so on, I want the platforms to be able to speed up with the same amount of space in between each spawn no matter what. The issue is when the score reaches 5, the platforms speed but the distance/space in between each spawn is not the same. They are further apart. Can anybody help me?
Hopefully this didn’t confuse anyone but if it did let me clarify it. I just want the platforms to speed up after a certain score has been reached with the same amount of space between each spawn. Meaning the waitForDuration should speed up (less than 2.0) with each speed increase.
Part of my code:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
if gameStarted == false {
gameStarted = true
ball.physicsBody?.affectedByGravity = true
title.removeFromParent()
let spawn = SKAction.runBlock({
()in
self.creatingPlatforms()
})
let delay = SKAction.waitForDuration(2.0)
let spawnDelay = SKAction.sequence([spawn, delay])
let spawnDelayForever = SKAction.repeatActionForever(spawnDelay)
self.runAction(spawnDelayForever)
let distance = CGFloat(self.frame.width + 170 + officialPlatform.frame.width)
let movePlatforms = SKAction.moveByX(-distance, y: 0, duration: NSTimeInterval(0.01 * distance))
let removePlatforms = SKAction.removeFromParent()
moveAndRemove = SKAction.sequence([movePlatforms, removePlatforms])
ball.physicsBody?.velocity = CGVectorMake(0, 0)
ball.physicsBody?.applyImpulse(CGVectorMake(0, 0))
scoreLbl.fontSize = 35
scoreLbl.position = CGPoint(x: self.frame.width / 2, y: scene!.frame.height / 2 + 300)
scoreLbl.fontColor = UIColor.blackColor()
scoreLbl.text = "\(score)"
score = 0
self.addChild(scoreLbl)
}
else {
ball.physicsBody?.velocity = CGVectorMake(0, 0)
ball.physicsBody?.applyImpulse(CGVectorMake(0, 45))
ball.physicsBody?.restitution = 0
scoreNode.physicsBody?.restitution = 0
}
}
func creatingPlatforms() {
scoreNode = SKSpriteNode()
scoreNode.size = CGSize(width: 120, height: 25)
scoreNode.position = CGPoint(x: self.frame.width + 95, y: self.frame.height / 2)
scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.dynamic = false
scoreNode.physicsBody?.categoryBitMask = physicsCategory.score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = physicsCategory.ball
scoreNode.name = "scoreNode"
officialPlatform = SKNode()
let platform = SKSpriteNode(imageNamed: "platform")
platform.size = CGSize(width: 140, height: 25)
platform.position = CGPoint(x: self.frame.width + 95, y: self.frame.height / 2)
platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
platform.physicsBody?.categoryBitMask = physicsCategory.platform
platform.physicsBody?.collisionBitMask = physicsCategory.ball
platform.physicsBody?.contactTestBitMask = physicsCategory.ball
platform.physicsBody?.affectedByGravity = false
platform.physicsBody?.dynamic = false
officialPlatform.name = "official platform"
officialPlatform.runAction(moveAndRemove)
officialPlatform.addChild(platform)
officialPlatform.addChild(scoreNode)
self.addChild(officialPlatform)
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if score >= 5 && score <= 9 {
moveAndRemove.speed = 0.9
}
}
}
I found my answer in another question on here.
swift - Increase speed of objects over time
I changed my self.runAction(spawnDelayForever) in my touchesBegan to self.runAction(spawnDelayForever, withKey: "spawnDelayForever"). Then in the update function, I changed it to:
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if score > 0 {
self.actionForKey("spawnDelayForever")?.speed += 0.001
moveAndRemove.speed += 0.001
}
}
I am working on game where a ball bounces and hits a platform and then bounces right back. Ideally the ball should bounce to exactly the height that it started at. However, in my game the ball slowly keeps bouncing higher and higher. I've looked at the documentation and tried to change all of the physics properties but nothing seems to work.
Any suggestions?
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let ballCategory:UInt32 = 0x1 << 0;
let circleCategory:UInt32 = 0x1 << 1;
let platformCategory:UInt32 = 0x1 << 2;
var ball = SKShapeNode(circleOfRadius: 20.0)
var circle = SKShapeNode(circleOfRadius: 200.0)
var platform = SKShapeNode(rectOfSize: CGSizeMake(10, 1))
var circleColor = 2
var ballColor = 3
var scoreLabel: SKLabelNode!
var score = 0
override func didMoveToView(view: SKView) {
setUpLabels()
self.physicsWorld.contactDelegate = self
backgroundColor = (UIColor.whiteColor())
ball.fillColor = SKColor.redColor()
ball.strokeColor = SKColor.clearColor()
ball.position = CGPoint(x: self.size.width/2, y: self.size.height/2+60)
ball.physicsBody = SKPhysicsBody(circleOfRadius: 20)
ball.physicsBody?.categoryBitMask = ballCategory
ball.physicsBody?.collisionBitMask = platformCategory
ball.physicsBody?.contactTestBitMask = platformCategory
ball.physicsBody?.dynamic = true
ball.physicsBody?.restitution = 1.0
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.linearDamping = 0.0
ball.physicsBody?.friction = 0.0
self.addChild(ball)
circle.fillColor = SKColor.clearColor()
circle.strokeColor = SKColor.redColor()
circle.lineWidth = 5.0
circle.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
circle.physicsBody = SKPhysicsBody(edgeLoopFromPath: circle.path)
circle.physicsBody?.categoryBitMask = circleCategory
self.addChild(circle)
platform.fillColor = SKColor.clearColor()
platform.strokeColor = SKColor.clearColor()
platform.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(10, 1))
platform.position = CGPoint(x: self.size.width/2, y: circle.position.y/2)
platform.physicsBody?.categoryBitMask = platformCategory
platform.physicsBody?.dynamic = false
platform.physicsBody?.friction = 0.0
self.addChild(platform)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
circleColor += 1
if circleColor%3 == 1 {
circle.strokeColor = UIColor.redColor()
}
if circleColor%3 == 2 {
circle.strokeColor = UIColor.greenColor()
}
if circleColor%3 == 3 {
circle.strokeColor = UIColor.yellowColor()
}
if circleColor%3 == 4 {
circle.strokeColor = UIColor.greenColor()
}
if circleColor%3 == 5 {
circle.strokeColor = UIColor.blueColor()
}
if circleColor%3 == 0 {
circle.strokeColor = UIColor.yellowColor()
}
}
func didBeginContact(contact: SKPhysicsContact) {
if ball.fillColor == circle.strokeColor {
score += 1
scoreLabel.text = String("Score \(score)")
}
if ball.fillColor != circle.strokeColor {
let transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let scene = SecondScene(size: self.scene!.size)
scene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view!.presentScene(scene, transition: transition)
}
ballColor = Int(arc4random_uniform(10))
if ballColor%3 == 1 {
ball.fillColor = UIColor.redColor()
}
if ballColor%3 == 2 {
ball.fillColor = UIColor.greenColor()
}
if ballColor%3 == 3 {
ball.fillColor = UIColor.yellowColor()
}
if ballColor%3 == 4 {
ball.fillColor = UIColor.greenColor()
}
if ballColor%3 == 5 {
ball.fillColor = UIColor.blueColor()
}
if ballColor%3 == 0 {
ball.fillColor = UIColor.yellowColor()
}
}
func setUpLabels () {
scoreLabel = SKLabelNode(fontNamed: "Arial")
scoreLabel.position = CGPoint(x: 60, y: self.size.height-30)
scoreLabel.text = String("Score \(score)")
scoreLabel.fontColor = UIColor.blackColor()
scoreLabel.fontSize = 25
self.addChild(scoreLabel)
}
override func update(currentTime: CFTimeInterval) {
}
}
Reduce restitution a little which take control of the bounciness of the physics body. Like this:
ball.physicsBody?.restitution = 0.9
In general numerical differential equation solvers that solve the ode for the next time step will introduce energy into the system, which is why you are seeing what you are seeing. You can mitigate this by introduce a damping force. In the case of SpriteKit this means changing the restitution or linear damping factors.
For more about the drift that occurs see: https://en.wikipedia.org/wiki/Energy_drift or http://www.cs.cmu.edu/~baraff/sigcourse/notesb.pdf.