touchesBegan does not work

Asked 2 years ago, Updated 2 years ago, 69 views

I created a program like this on Playground in Xcode.
However, touchesBegan does not work
If it works, it should show "touched", but it doesn't

import UIKit
import SpriteKit
import PlaygroundSupport

class set:SKScene{
    // var Progress = 0
    // var button : SKNode ?= nil

    func start(){
        letsceneWidth = 450.0
        letsceneHeight=800.0
        // var ProcessSeed = 0
        let screenView = SKView ( frame : CGRect ( x : 0.0, y : 0.0, width : sceneWidth, height : sceneHeight ))
        PlaygroundPage.current.liveView=sceneView
        let back: SKScene=SKScene(size: CGSize(width:sceneWidth, height:sceneHeight))
        back.backgroundColor=UIColor.white
        sceneView.presentScene(back)

        let character = SKSpriteNode (imageNamed: "SobacchiStand.png")
        character.position = CGPoint (x:sceneWidth/2, y:sceneHeight/2)
        character.xScale = 0.5;
        character.yScale = 0.5;
        back.addChild(character)

        let button = SKSpriteNode (imageNamed: "Button.png")
        button.xScale=0.1;
        button.yScale=0.1;
        button.position = CGPoint (x:400.0, y:100.0)
        button.zPosition=1
        button.name = "button"
        back.addChild(button)

        button.isUserInteractionEnabled=true
        }
    func doit(){
        print("Run")
        print("Yes"")
    }

   /* override functouchesBegan(_touches:Set<UITouch>, with event:UIEvent?){
        iflet touch=touches.first as UITouch?{
            let location = touch.location(in:self)
            if self.atPoint(location).name == "button" {
                print("button tapped")
            }
        }
    }*/
    override functouchesBegan(_touches:Set<UITouch>, with event:UIEvent?)   
{     // super.touchesBegan (touches, with:event)
        print("touched")
   }
    override functouchesEnded(_touches:Set<UITouch>, with event:UIEvent?) {
        super.touchesEnded(touches, with:event)
       print("touchend")
    }
}

let first = set()
let second = first.start()
varProgress = 0

/* while Progress <=100 {
    first
}*/

There are no errors in themselves, and I can't find any mistakes on any of the sites, but is there something wrong?

swift spritekit

2022-09-29 22:18

1 Answers

There are no errors and

As I pointed out in the comment section of Question, please write the code you provided in Playground and copy what you executed.If you present something different, no matter how correct and accurate the answer is, it will not solve the problem.

The second line from the end

let second=first.start()

This is where the warning "Constant'second'inferred to have type'(), which may be unexpected" appears.This shows that the code presented is different from the code executed in Playground.The method start() has no return value, in other words, the return value is Void (()), so if you deduce the type of variable second, you should delete Void (()).

Let's get down to business.

Line 15

let back: SKScene=SKScene(size: CGSize(width:sceneWidth, height:sceneHeight))

Rewrite this

letback=set(size:CGSize(width:sceneWidth, height:sceneHeight))

This change results as intended, but it is a useless code.
Also, as a recommended naming convention for Swift, make sure that class names begin with uppercase letters.

 class set:SKScene{
// Recommendations are as follows:
classSet:SKScene{


2022-09-29 22:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.