This Game:
Game Screenshot
When I write this code.. Player disappears..
Variables:
var previousTimeInterval: TimeInterval = 1
var playerIsFacingRight = true
let playerSpeed = 4.0
Code:
extension GameScene {
override func update(_ currentTime: TimeInterval) {
let deltaTime = currentTime - previousTimeInterval
previousTimeInterval = currentTime
//Player Movement
guard let joystickKnob = joystickKnob else { return }
let xPosition = Double(joystickKnob.position.x)
let displacement = CGVector(dx: deltaTime * xPosition * playerSpeed, dy: 0)
let move = SKAction.move(by: displacement, duration: 0)
player?.run(move)
}
}
Game Screenshot
The update method runs many times.
It's highly probably was disappear by soo many actions for player movement.
You can debug inside the update and check player?.position
Check the player?.zPosition of player, if it the same of background can position behind.
You can use Xcode "Debug view hierarchy" to see where is the player.
Related
I'm working on a small ios game with this joystick library implemented onto it. My issue is after calculating the direction the joystick is going, I want the character to change to a running animation (I implement the animation using an .sks file). It almost works except after the animation has begun, it stops and doesn't finish until the player lets go at the stick. Some of my code is down below. Any help is appreciated.
Function to setup stick:
func setupJoystick() {
addChild(analogJoyStick)
analogJoyStick.trackingHandler = { [unowned self] data in
self.thePlayer.position = CGPoint(x: self.thePlayer.position.x + (data.velocity.x * 0.04), y: self.thePlayer.position.y + (data.velocity.y * 0.04))
let degrees = self.analogJoyStick.data.angular * 360 / (2 * .pi)
if degrees > 0 {
let walkAnimation:SKAction = SKAction(named: "WalkLeft")!
self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
} else if degrees < 0 {
let walkAnimation:SKAction = SKAction(named: "WalkRight")!
self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
}
}
analogJoyStick.beginHandler = { [unowned self] in
let degrees = self.analogJoyStick.data.angular * 360 / (2 * .pi)
if degrees > 0 {
let walkAnimation:SKAction = SKAction(named: "WalkLeft")!
self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
} else if degrees < 0 {
let walkAnimation:SKAction = SKAction(named: "WalkRight")!
self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
}
}
analogJoyStick.stopHandler = { [unowned self] in
self.thePlayer.removeAction(forKey: "animating")
}
}
Here is a visual of the coding:
Spritekit Demo
I read the joystick library instructions and saw two methods (handlers) you can use:
var beginHandler: (() -> Void)? // before move
var stopHandler: (() -> Void)? // after move
In beginHandler() add the (repeated) walk animation:
let walkAnimation: SKAction = SKAction(named: theAnimation)!
thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
And remove the action in stopHandler()
thePlayer.removeAction(forKey: "animating")
Or using the closures (also from the documentation):
joystick.beginHandler = { [unowned self] in
let walkAnimation: SKAction = SKAction(named: ".sks")!
self.thePlayer.run(SKAction.repeatForever(walkAnimation), withKey: "animating")
}
joystick.stopHandler = { [unowned self] in
self.thePlayer.removeAction(forKey: "animating")
}
Any help would be greatly appreciated. I am relatively new to this and just feel like I beginning to understand coding.
My issue -----
I am having difficult trying to fix a problem with a SpriteKit tutorial that I have been enhancing as a way to hone and improve my skills as a newbie.
I am experiencing multiple contacts when my player “crashes” on the ground.
More than one life is removed, which causes my game to generate a 'Attemped to add a SKNode which already has a parent: ’ error adding the player back to the scene.
I have tried player.removeFromParent everywhere I can think of inside my code.
The game works flawlessly as long as I “crash” into “enemies” in the air. Once the player contacts the ground, it’s all over. If I “kill” the player upon contact with the ground, there is no issue, but I want the game to continue as long as the player still has lives whether the contact the ground or and enemy.
I really believe the issue would be fixed if the multiple contact problem could be resolved.
class GameScene: SKScene, SKPhysicsContactDelegate {
// Set up the Texure Atlases
var images = SKSpriteNode()
var textureAtlas = SKTextureAtlas()
var textureArray = [SKTexture]()
var touchingScreen = false
var obstacle = SKSpriteNode()
// Generates a Random number between -350 and 350 (the center of the axis being 0)
let rand = GKRandomDistribution(lowestValue: -350, highestValue: 350)
var timer: Timer?
// This method is called when the Game Launches
override func didMove(to view: SKView) {
// Adds a pixel perfect physicsBody to the player
player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.texture!.size())
player.physicsBody?.categoryBitMask = 1
player.position = CGPoint(x: -400, y: 275)
// Disables the affects of a collisions (+pushing, rotation etc.) on the player when a collision with another SKSpriteNode occurs
player.physicsBody?.allowsRotation = false
player.physicsBody?.collisionBitMask = 0
addChild(player)
physicsWorld.gravity = CGVector(dx: 0, dy: -2)
physicsWorld.contactDelegate = self
timer = Timer.scheduledTimer(timeInterval: 1.5, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)
}
// This method is called when the User touches the Screen
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touchingScreen = true
}
// This method is called when the User stops touching the Screen
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
touchingScreen = false
}
// This method is called before each frame is rendered
override func update(_ currentTime: TimeInterval) {
// Constrains the player to the scene area
if player.position.y > 275 {
player.position.y = 275
}
// Moves the player up when the screen is being touched
if touchingScreen {
player.physicsBody?.velocity = CGVector(dx: 0, dy: 200
)
}
}
func didBegin(_ contact: SKPhysicsContact) {
// Exits the method if either node is nil (doesn't exist)
guard let nodeA = contact.bodyA.node else { return }
guard let nodeB = contact.bodyB.node else { return }
// Check to see if either node is player and, if so, call the playerHit method and pass in the other node
if nodeA == player {
playerHit(nodeB)
} else if nodeB == player {
playerHit(nodeA)
}
}
func createEnemy() {
// Check for Bonus Creation
checkForBonusCreation()
// Choose a Random Enemy
let pickEnemy = Int(arc4random_uniform(3))
switch pickEnemy {
case 0:
obstacle = SKSpriteNode(imageNamed: "enemy-balloon")
animateBalloon()
case 1:
obstacle = SKSpriteNode(imageNamed: "enemy-bird")
animateBird()
case 2:
obstacle = SKSpriteNode(imageNamed: "enemy-plane")
animatePlane()
default:
return
}
// Positions the enemy
obstacle.zPosition = -2
obstacle.position.x = 768
obstacle.size = (CGSize(width: obstacle.size.width * 0.7, height: obstacle.size.width * 0.7))
// Prevents the obstacle from being spawned too low on the scene
if obstacle.position.y < -150 {
obstacle.position.y = -150
}
addChild(obstacle)
// Adds pixel perfect collision detection to the enemies
obstacle.physicsBody = SKPhysicsBody(texture: obstacle.texture!, size: obstacle.texture!.size())
// Then we set isDynamic to false so grivty will not affect the obstacles
obstacle.physicsBody?.isDynamic = false
// Assigns 1 to it's contactTestBitMask so that it know to detect a collision with the player
obstacle.physicsBody?.contactTestBitMask = 1
// Names the obstacle so we can track collisions properly
obstacle.name = "enemy"
// Spawn an enemy at a random y-axis
obstacle.position.y = CGFloat(rand.nextInt())
// Moves the obstacles across to and off the left hand side of the screen over 9 seconds athen removes thier nodes so they don't chew up memory
let move = SKAction.moveTo(x: -768, duration: 9)
let remove = SKAction.removeFromParent()
let action = SKAction.sequence([move, remove])
obstacle.run(action)
}
func playerHit(_ node: SKNode) {
if node.name == "enemy" {
player.removeFromParent()
node.removeFromParent()
lives -= 1
balloonPop()
showLivesRemaining()
} else if node.name == "ground" {
player.removeFromParent()
lives -= 1
balloonPop()
showLivesRemaining()
}
}
func balloonPop() {
player.removeFromParent()
}
func showLivesRemaining() {
if lives >= 3 {
lives = 3
} else if lives <= 0 {
lives = 0
player.removeFromParent()
}
}
// Jump to the restart or quit scene
func restartGame() {
player.removeFromParent()
let wait = SKAction.wait(forDuration: 2.0)
let showPlayer = SKAction.run {
player.position = CGPoint(x: -400, y: 275)
self.addChild(player)
}
let sequence = SKAction.sequence([wait, showPlayer])
run(sequence)
}
// Displays GAME OVER and takes the player to the "Restart or Quit" choice scene
func endGame() {
player.removeFromParent()
let gameOver = SKSpriteNode(imageNamed: "game-over")
gameOver.zPosition = 15
addChild(gameOver)
// Waits 2 seconds and fade into the Restart Scene
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
if let scene = RestartScene(fileNamed: "RestartScene") {
scene.scaleMode = .aspectFill
self.view?.presentScene(scene, transition: SKTransition.crossFade(withDuration: 1.5))
}
}
}
}
What a lot of games do is give a buffer period after a player is hit where they flash the player or turn the player red for a couple of seconds. during this time the player is not prone to more hits and gives the player an opportunity to move to safety.
func didBegin(_ contact: SKPhysicsContact) {
let contactAName = contact.bodyA.node?.name
let contactBName = contact.bodyB.node?.name
//don't detect contact if the player is hit
if ((contactAName == "player") || (contactBName == "player")) && !player.isHit {
if (contactAName == "ground") || (contactBName == "ground") {
print("groundcontact with player")
player.isHit = true
//good idea to flash player or pulse player to let user know player is hit
self.run(.wait(forDuration: 3.0)) {
//wait for 3 seconds and make the player hittable again
self.player.isHit = false
}
return
}
}
}
Edit
the contact detection code fires at up to 60 times a second. When your player hits an enemy you remove that enemy, making more contact to the enemy not possible. However when the player hits the ground the ground does not get removed and thus player keeps losing lives. after the player hits the ground try bumping the player up a 100px away from the ground and see if that stops the multiple contact issue.
I'm working on game and the game works perfect on all iPhones and iPad expect iPhone X and iPhone 8 Plus
When I add sprite to the scene the game crash (only on iPhone X and iPhone 8 Plus) because the sprite is already added to scene, On others iPhone the app not crash, I think the bug is in my timer, maybe the bug it's the iPhone simulator? What do you think?
Code:
class GameScene: SKScene, SKPhysicsContactDelegate {
var finger = SKSpriteNode(imageNamed: "Finger") //Finger to swipe the biscuit
var biscuit = SKSpriteNode(imageNamed: "Biscuit")
var glass = SKSpriteNode(imageNamed: "Glass")
var defaultbiscuitpos:CGPoint = CGPoint()
var defaultfingerpos:CGPoint = CGPoint()
/*** Drag Biscuit vars ***/
var touchpoint:CGPoint = CGPoint()
var istouching:Bool = false
var fadeoutfinger = SKAction()
var fadeinfinger = SKAction()
var fingergroup = SKAction()
var burnanimtion = SKAction()
var movefinger = SKAction()
let fingertimer:String = "fingertimer"
var isGameover:Bool = false
//Game mode enum
enum gamemode {
case dip
case ready
case out
case gameover
}
//Game mode (Dip,Ready,Out or game over by enum) **Now is Dip
var mymode = gamemode.dip
override func didMove(to view: SKView) {
//Finger
finger.name = "Finger"
finger.position = CGPoint(x: biscuit.position.x + finger.frame.width, y: biscuit.position.y)
defaultfingerpos = finger.position
finger.alpha = 1.0
finger.zPosition = 5
//Start finger timer to make animation
createTimer(name: fingeranimation, waitt: 3.0, sprite: finger, actioname: fingertimer)
}
//Finger timer func
func fingeranimation () {
//Check if timer is over 4 seconds and the title is dip
if mymode == gamemode.dip {
//Add finger to screen
addChild(finger)
//Set fade in animation for finger
fadeinfinger = SKAction.fadeIn(withDuration: 2.0)
//Set move animation for finger
movefinger = SKAction.moveTo(y: glass.frame.midX, duration: 2.0)
//Set fade out animation for finger
fadeoutfinger = SKAction.fadeOut(withDuration: 2.0)
fingergroup = SKAction.group([fadeinfinger,movefinger,fadeoutfinger])
finger.run(fingergroup, completion: {
//Remove finger from screen
self.finger.removeFromParent()
//Return the finger to apper and return the finger to default position
self.finger.alpha = 1.0
self.finger.position = self.defaultfingerpos
})
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: self)
//Tap on biscuit
if biscuit.contains(location) && mymode != gamemode.gameover {
touchpoint = location
istouching = true
biscuit.physicsBody?.pinned = false
//Stop the finger animation timer
stopTimer(actioname: fingertimer, sprite: finger)
}
}
//Make timer function
func createTimer (name:#escaping os_block_t , waitt:TimeInterval, sprite:SKSpriteNode?,actioname: String) {
let myaction = SKAction.sequence([SKAction.wait(forDuration: waitt), SKAction.run(name)])
run(SKAction.repeatForever(myaction), withKey: actioname)
}
//Stop timer function
func stopTimer(actioname:String, sprite:SKSpriteNode?) {
removeAction(forKey: actioname)
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if istouching && isGameover == false {
let dt:CGFloat = 1.0/15
let distance = CGVector(dx: touchpoint.x - biscuit.position.x, dy: touchpoint.y - biscuit.position.y * 1.65)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
self.biscuit.physicsBody!.velocity = velocity
}
}
}
This problem is now fixed with the release of Xcode 9.1.
I tried to move random my sprites (the squares) but all sprite go to same position and ״vibrating״.
What is the best way to move sprite forever and with random position?
//Make random shape
func makeShape () {
//Check if have more than 12 shapes
if shapesamount <= 4 {
sprite = shape.copy() as! SKShapeNode
sprite.name = "Shpae \(shapesamount)"
shapes.addChild(sprite)
shapesamount += 1
moveRandom(node: sprite)
}
}
//Move shape radmonly
func moveRandom (node: SKShapeNode) {
move = SKAction.move(to: CGPoint(x:CGFloat.random(min: frame.minX, max: frame.maxX), y:CGFloat.random(min: frame.minY
, max: frame.maxY)), duration: shapespeed)
node.run(move)
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
if istouching && !isOver {
let dt:CGFloat = 1.0/50.0
let distance = CGVector(dx: touchpoint.x - circle.position.x, dy: touchpoint.y - circle.position.y)
let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
self.circle.physicsBody!.velocity = velocity
}
if isOver == false {
for nodee in shapes.children {
moveRandom(node: nodee as! SKShapeNode)
}
}
}
}
shapes is SKNode
You are constantly adding move actions to your nodes, this is why it is vibrating. You are literally making them move in every direction every frame.
Instead, add a check to see if any actions are running, if not, then change the position:
if isOver == false {
for nodee in shapes.children {
guard !nodee.hasActions else {continue}
moveRandom(node: nodee as! SKShapeNode)
}
}
Running into some trouble with my code. I'm trying to make zombies follow my player around with the following code:
class GameScene: SKScene, SKPhysicsContactDelegate {
func Enemies() {
Enemy = SKSpriteNode(imageNamed: "Spaceship")
Enemy.size = CGSize(width: 50, height: 50)
Enemy.color = UIColor(red: 0.9, green: 0.1, blue: 0.1, alpha: 1.0)
Enemy.colorBlendFactor = 1.0
//physics
Enemy.physicsBody = SKPhysicsBody(rectangleOf: Enemy.size)
Enemy.physicsBody?.isDynamic = true
Enemy.physicsBody?.affectedByGravity = false
Enemy.name = "Enemy"
Enemy.position.y = -frame.size.height/2
let positionX = arc4random_uniform(UInt32(frame.size.width))
Enemy.position.x = CGFloat(positionX)
addChild(Enemy)
}
override func didMove(to view: SKView) {
enemyTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(GameScene.Enemies), userInfo: nil, repeats: true)
override func update(_ currentTime: TimeInterval) {
Enemy.run(SKAction.move(to: ship.position, duration: 3))
}
If I run this code, I can spawn the zombies, but they will not follow my main player, they will just go to the position that he was at when they were spawned (i.e. zombie spawned at time = 0 will go to the ship position at time = 0, zombie spawned at time = 1 will go to the ship position at time = 1, and so on). However, if I run this code while only spawning one zombie like so:
override func didMove(to view: SKView) {
Enemies()
}
The lone zombie will follow my player around. Any idea why the code works for one zombie, but not for multiple zombies?
I would not recommend constantly adding actions on your update cycle, you are going to suffer from performance loss due to all the things that happen behind the scenes. Instead, use an SKAction.customAction that you would only add to your sprite once.
Here is an example of a custom action that will do what you want, remember only assign it once. (Code is not tested, so may need edits)
let customActionBlock =
{
(node,elapsedTime) in
let dx = ship.x - node.position.x
let dy = ship.y - node.position.y
let angle = atan2(dx,dy)
node.position.x += sin(angle) * speedPerFrame
node.position.y += cos(angle) * speedPerFrame
}
let duration = TimeInterval(Int.max) //want the action to run infinitely
let followPlayer = SKAction.customAction(withDuration:duration,actionBlock:customActionBlock)
Enemy.run(action:followPlayer,withKey:"follow")
Maybe you should removeAllActions() on Enemy before readding an action. It seems that you have actions that take 3 seconds, but you add an action every frame, so it has at most 180 actions for a node at once.