Int value Persist - iphone

I want to implement longpressgesture when my touchbegan method there. I am using NSTimer and record the counter when counter became 3 . I recognized that is long press. But when i release the button and then again press mycounter persist previous value and increment on previous value .Although I am assign counter egual to zero . Please help any help would be apperciated.
var counter : Int = 0
var timer :NSTimer?
override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer = NSTimer()
counter == 0;
timer = NSTimer.scheduledTimerWithTimeInterval(1, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}
func updateCounter() {
print(counter++)
if (counter > 3){
timer!.invalidate()
timer = nil;
}
}
override public func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
timer!.invalidate()
timer = nil;
counter == 0;
}
override public func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
if (counter>=3){
print("hello")
}
}

That's the classic equation == versus assign = operator confusion
timer = NSTimer() // this line is actually not needed
counter = 0
...
timer?.invalidate() // better use question mark to avoid a potential crash
timer = nil
counter = 0
and remove all semicolons.

Related

Cannot call value of non-function type 'Closure Name'

I define a Closure named touchBlock, and user it in override func touchesEnded, then I get an error :
Cannot call value of non-function type '((ELControl, UIEvent, UIEvent)
-> Void?)?
Here is my code:
var touchBlock: ((_ view: ELControl, _ touches: UIEvent, _ event: UIEvent) -> Void?)?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if self.touchBlock != nil {
self.touchBlock(nil, touches, event)
} else {
super.touchesEnded(touches, with: event)
}
}
Please help me to fix it.
You have to unwrap the optional
self.touchBlock!(nil, touches, event)
or preferable with optional binding
if let block = self.touchBlock {
block(nil, touches, event) ...
But consider that the passed arguments don't match the types of the closure at all (view and event are non-optional and the type of touches is completely different).
You need to unwrap your closure variable first. You can use optional chaining syntax for instance(pay attention to question mark ? appeared right after self.touchBlock inside the if statement)
var touchBlock: ((_ view: ELControl, _ touches: UIEvent, _ event: UIEvent) -> Void?)?
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
if self.touchBlock != nil {
self.touchBlock?(nil, touches, event)
} else {
super.touchesEnded(touches, with: event)
}
}
There are 2 cases which needs to handled in your code.
var touchBlock: ((_ view: ELControl, _ touches: UIEvent, _ event: UIEvent) -> Void?)?
Your block definition shows that you can only pass non-nil values to the block and even your block is optional. So as #Igor suggested you should call the block as:
self.touchBlock?(<non-nil-value>, touches, event)
Here, ? mark with block, will handle optional block condition.
But if you want some other operation if block is nil. Then you can use if-let condition for that:
if let block = self.touchBlock {
block(<non-nil-value>, touches, event)
} else {
super.touchesEnded(touches, with: event)
}
But, if you want to pass nil value as well, define you block as:
// Set ELControl view as optional with ? mark
var touchBlock: ((_ view: ELControl?, _ touches: UIEvent, _ event: UIEvent) -> Void?)?
and call:
if let block = self.touchBlock {
block(nil, touches, event)
} else {
super.touchesEnded(touches, with: event)
}

swift 4 two dpads with spritekit

A screenshot of my game
I want to make a game (with Spritekit) where you can use the left dpad to move the player around in a tile map which already works. With the right one you can aim at opponents which works too. Althought I enabled multiple touch only one controller works at the same time.
Joystick means the same as dpad.
import SpriteKit
import GameplayKit
class GameScene: SKScene {
//These are just the touch functions
//touch functions
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches {
if touches.first!.location(in: cam).x < 0 {
moveStick.position = touches.first!.location(in: cam)
}
if touches.first!.location(in: cam).x > 0 {
shootStick.position = touches.first!.location(in: cam)
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches {
if touches.first!.location(in: cam).x < 0 {
moveStick.moveJoystick(touch: touches.first!)
}
if touches.first!.location(in: cam).x > 0 {
shootStick.waponRotate(touch: touches.first!)
}
}
}
open override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches {
resetMoveStick()
resetShootStick()
}
}
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
for _ in touches {
resetMoveStick()
resetShootStick()
}
}
// update function
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
let jSForce = moveStick.velocityVector
self.player.position = CGPoint(x: self.player.position.x + jSForce.dx,
y: self.player.position.y + jSForce.dy)
cam.position = player.position
}
}
As KnightOfDragon pointed out, you are using .first. That means your code is looking for the first touch in your scene and then going from there. Your game won't let you use both joysticks at the same time because you won't let them be used at the same time.
These if-statements that you have in your various touch functions:
for _ in touches {
if touches.first!.location(in: cam).x < 0 {
}
if touches.first!.location(in: cam).x > 0 {
}
}
Should look like this:
for touch in touches {
let location = touch.location(in: self)
if location.x < 0 {
moveStick.moveJoystick(touch: location)
}
if if location.x > 0 {
shootStick.waponRotate(touch: location)
}
}
This should fix any errors you have.

Creating a stopwatch in swift

I have tried searching other answers and I cannot find one that applies to my scenario. I am writing a game in swift and want to create a stopwatch that determines how the long the player is playing. When the user touches down, the stopwatch will start and when a certain action happens, then the timer stops and resets. I'd like to use minutes, seconds, milliseconds (i.e. 00:00.00).
Currently, the time function kind of works. It doesn't start at 0, it starts at the current seconds in time (I know thats when I start it, but I don't know how to start it at 0). It also only updates when I touch the screen, I need it to count from 00:00.00 and update on its own until the cancel action is fired.
Thank you for your time.
Here is what I have so far:
class GameScene: SKScene {
var activeTimer = SKLabelNode()
//var bestTime = SKLabelNode()
var startTime = TimeInterval()
//var currentTime = TimeInterval()
//var countingTime = TimeInterval()
var updateTimerAction = SKAction()
override func didMove(to view: SKView) {
activeTimer = self.childNode(withName: "active_timer") as! SKLabelNode
//bestTime = self.childNode(withName: "best_time") as! SKLabelNode
startTime = TimeInterval(Calendar.current.component(.second, from:Date()))
updateTimerAction = SKAction.sequence([SKAction.run(updateTimer), SKAction.wait(forDuration: 1.0)])
}
func startGame() {
// Other code
startGameTimer()
}
func resetGame() {
// Other code
stopGameTimer()
}
func startGameTimer() {
run(SKAction.repeatForever(updateTimerAction), withKey: "timer")
}
func updateTimer() {
activeTimer.text = stringFromTimeInterval(interval: startTime) as String
}
func stringFromTimeInterval(interval: TimeInterval) -> NSString {
let ti = NSInteger(interval)
let ms = Int((interval.truncatingRemainder(dividingBy: 1)) * 1000)
let seconds = ti % 60
let minutes = (ti / 60) % 60
return NSString(format: "%0.2d:%0.2d.%0.2d",minutes,seconds,ms)
}
func stopGameTimer() {
removeAction(forKey: "timer")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
startGame()
for touch in touches {
// other code
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
// other code
}
}
override func update(_ currentTime: TimeInterval) {
updateTimer()
if <action> {
// stop timer and reset game
resetGame()
}
}
Instead of using a TimeInterval object, it might be better to use a Timer object which calls a function that increments the value of a label (or an internal variable) from 0 onwards accordingly after every 1 second(or can be made lesser). And for stopping the clock just call the Timer object's invalidate function.
Check the documentation for more info: https://developer.apple.com/documentation/foundation/timer
The following lines of code come from an actual macOS game that I submitted to Mac App Store some 3 weeks ago. It counts down a number to 0. In your case, change
self.currentSeconds -= 1
to
self.currentSeconds += 1
timeBackNode is an empty node that holds SKLabelNode objects timeNode0 and timeNode1. So create it when the game starts with didMove.
var currentSeconds = Int() // currentSeconds
var timeNode0 = SKLabelNode() // timeNode0
var timeNode1 = SKLabelNode() // timeNode1
func setupTimeGoal() {
enumerateChildNodes(withName: "//TimerBack", using: { timeBackNode, _ in
let goal = self.timeDict["num"] as! Int
self.currentSeconds = goal // In your case, goal is 0 since you are going to count up.
self.timeNode0 = SKLabelNode(fontNamed: "Your font's font name")
self.timeNode0.text = self.makeTimeWithSeconds(secs: goal)
self.timeNode0.fontSize = 26
self.timeNode0.fontColor = SKColor.black
self.timeNode0.horizontalAlignmentMode = .center
self.timeNode0.position = CGPoint(x: 1, y: -22)
timeBackNode.addChild(self.timeNode0)
self.timeNode1 = SKLabelNode(fontNamed: "Your font's font name")
self.timeNode1.text = self.makeTimeWithSeconds(secs: goal) // this function translate a counting number into a time code like 00:00:00
self.timeNode1.fontSize = 26
self.timeNode1.fontColor = SKColor.cyan
self.timeNode1.horizontalAlignmentMode = .center
self.timeNode1.position = CGPoint(x: 0, y: -23)
timeBackNode.addChild(self.timeNode1)
})
}
func repeatTime() {
let waitAction = SKAction.wait(forDuration: 1.0)
let completionAction = SKAction.run {
if self.currentSeconds == 0 && self.gameState == .playing {
self.removeAction(forKey: "RepeatTime")
self.proceedLoss()
return
}
self.currentSeconds -= 1
self.timeNode0.text = self.makeTimeWithSeconds(secs: self.currentSeconds)
self.timeNode1.text = self.makeTimeWithSeconds(secs: self.currentSeconds)
}
let seqAction = SKAction.sequence([waitAction, completionAction])
let repeatAction = SKAction.repeatForever(seqAction)
self.run(repeatAction, withKey: "RepeatTime")
}
Here is what I ended up doing. While El Tomato's answer appears to work, given the setup of my code I went a little different route. After talking with some other friends, I chose to user a timer due to the design of my application. If I happen to run into any problems using a Timer object I will update this question.
The code below will start on a touch, update in the format: "00:00.00" and stop when the action is fired. This time will pause until another touch and the timer will then start from zero.
class GameScene: SKScene {
var seconds = 0
var timer = Timer()
var timeStarted = Bool()
var activeTimer = SKLabelNode()
//var bestTime = SKLabelNode()
override func didMove(to view: SKView) {
activeTimer = self.childNode(withName: "active_timer") as! SKLabelNode
//bestTime = self.childNode(withName: "best_time") as! SKLabelNode
timeStarted = false
}
func startGame() {
startGameTimer()
timeStarted = true
}
func resetGame() {
timeStarted = false
stopGameTimer()
seconds = 0
}
func startGameTimer() {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: (#selector(updateTimer)), userInfo: nil, repeats: true)
}
func updateTimer() {
seconds += 1
activeTimer.text = timeString(time: TimeInterval(seconds))
}
func timeString(time:TimeInterval) -> String {
let hours = Int(time) / 3600
let minutes = Int(time) / 60 % 60
let seconds = Int(time) % 60
return String(format:"%02i:%02i.%02i", hours, minutes, seconds)
}
func stopGameTimer() {
timer.invalidate()
removeAction(forKey: "timer")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if !timeStarted {
startGame()
}
for touch in touches {
// other code
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
// other code
}
}
override func update(_ currentTime: TimeInterval) {
if timeStarted {
updateTimer()
}
if <action> {
// stop timer and reset game
resetGame()
}
}
}

Move sprite forever

I am trying to move sprite to the right when I press a button on the screen. However, when I try doing it I only have a solution to move the sprite to a certain point. So... I want the sprite to move to the right forever or until i do something else.
This is in Xcode using Swift in SpriteKit.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch : AnyObject in touches{
let pointInTouch = touch.locationInNode(self)
let tappedNode = nodeAtPoint(pointInTouch)
let tappeNodeName = tappedNode.name
if tappeNodeName == "Btn"{
player.physicsBody?.velocity = CGVectorMake(0, 0)
let action = SKAction.applyImpulse(CGVectorMake(400, 0), duration: 1)
player.runAction(SKAction.repeatActionForever(action))
print("Touched!")
}
}
}
You can simply move your node to the right untill it does exist the scene
class GameScene: SKScene {
private var player: SKSpriteNode!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
let pointInTouch = touch.locationInNode(self)
let tappedNode = nodeAtPoint(pointInTouch)
if tappedNode.name == "Btn"{
let moveToRight = SKAction.moveToX(self.frame.width + player.frame.width, duration: 5)
player.runAction(moveToRight)
}
}
}
Update: constant speed
If you want the player to move with constant speed you can use this code.
As you can see I am calculating the duration using space / speed. You just need to find the best constant value for speed for your scenario.
class GameScene: SKScene {
private var player: SKSpriteNode!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let touch = touches.first else { return }
let pointInTouch = touch.locationInNode(self)
let tappedNode = nodeAtPoint(pointInTouch)
let deltaX = self.frame.width + player.frame.width - player.position.x
let speed: CGFloat = 10 // <-- change this to find the best value for you
let duration: NSTimeInterval = NSTimeInterval(deltaX / speed)
if tappedNode.name == "Btn"{
let moveToRight = SKAction.moveByX(deltaX, y:0, duration: duration)
player.runAction(moveToRight)
}
}
}

Pause and Resume SKScene

I was testing my game on my iPhone and I realized that I have a problem with the home button. When I pause the game and load another app the game is not paused and continues when the app is not on screen.
Here's the code:
class GameScene: SKScene, SKPhysicsContactDelegate{
var PauseButton = SKSpriteNode(imageNamed: "pause")
var Resume = SKSpriteNode(imageNamed: "resume1")
}
init(size: CGSize, won: Bool) {
super.init(size: size)
//pause
PauseButton.position = CGPointMake(330, 700)
PauseButton.zPosition = 3;
PauseButton.size = CGSizeMake(40, 40);
PauseButton.name = "PauseButton"
self.addChild(PauseButton)
//resume
Resume = SKSpriteNode(imageNamed: "resume1")
Resume.position = CGPointMake(520, 450)
Resume.zPosition = 100
Resume.setScale(1.3)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
show = self.scene as SKScene!
for touch in touches {
let Location:CGPoint = touch.locationInNode(self)
let Node:SKNode = self.nodeAtPoint(Location)
if(Node == PauseButton){
self.show.paused = true
PauseButton.removeFromParent()
self.addChild(Resume)
}
if(Node == Resume){
Resume.removeFromParent()
self.show.paused = false
self.addChild(PauseButton)
}
How do I get the game to pause when I press the home button?
Pause the view not the scene.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
guard let view = view else {
return
}
view.paused = !view.paused
}
SpriteKit will auto Resume when you comeback to your app. so you need :
Step 1 : create a variable :
var wasPaused : Bool = false
Step 2 : change it in your pause/resume function :
wasPaused = true // pause func
wasPaused = false // resume fun
Step 3 : in update fun of your scene:
override func update(currentTime: CFTimeInterval) {
if isPausing{
self.paused = true
return
}
}