touchesBegan function not working in Xcode 8.3

I’m using Xcode 8.3 and I’ve been having a few problems with getting the touchesBegan function to run.

I made a new game with SpriteKit. I then added a SpriteKit Scene called GameStart.sks. I created a corresponding Cocoa Touch Class.

From GameViewController.m I changed the line that starts GameScene to

GameStart *scene = (GameStart *)[SKScene nodeWithFileNamed:@"GameStart"];

When I ran in the simulator, it went to the GameStart scene (which I added a Color Sprite so I know it works).

From there, I edited the GameStart.m file and populated it with the following code:

- (void)didMoveToView:(SKView *)view {
	
}
- (void)touchDownAtPoint:(CGPoint)pos {}

- (void)touchMovedToPoint:(CGPoint)pos {}

- (void)touchUpAtPoint:(CGPoint)pos {}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	for (UITouch *t in touches) {[self touchDownAtPoint:[t locationInNode:self]];}
	NSLog(@"touch works!");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	for (UITouch *t in touches) {[self touchMovedToPoint:[t locationInNode:self]];}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
	for (UITouch *t in touches) {[self touchUpAtPoint:[t locationInNode:self]];}
}

Notice the touchesBegan I added an NSLog() line which should output “touch works!” to the console.

I run it in the simulator and when I click, nothing happens! When I do the sample one the touch works fine (from GameScene the “Hello World!”).

This lead me to believe I have setup GameStart incorrectly. Have I missed a crucial step?

A screenshot of the image of my code and file structure

I have missed a crucial step that didn’t seem to be necessary in the video.

I clicked GameStart.sks
went to the 4th icon on the right panel, a rectangle resembling a flag
and entered GameStart into Custom Class.

Simple solution and it works!

1 Like

Thanks for the answer, I was going to ask the same question.