basically for now I got TitleScene, where are present name of the my game and when tap anywhere on screen it jumps to GameScene, it looks something like this:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKScene *scene = [GameScene sceneWithSize:self.size];
SKTransition *transition = [SKTransition pushWithDirection:SKTransitionDirectionUp duration:1.0f];
[self.view presentScene:scene transition:transition];
}
My question how can i change this method to button?
maybe give your sprite a name of "button" so you know which one youre touching
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *touchedNode = [self nodeAtPoint:location];
if (touchedNode && [touchedNode.name isEqual:#"button"]) {
// your code here
}
}
}
Related
I have GamePlayScene that when ended, removes all nodes and displays the Score and an option to restart. I also have a button on that scene that takes you to another scene to display upgradable items. I would like to make a function that adds a back button on the "upgrade items" scene that returns me to the ended GamePlay scene. The code that I have right now returns me back to the GamePlay scene but restarts the game instead of displaying the Game Over text. I've tried searching online but I can't manage to find a good solution. My Code:
GamePlayScene.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
...
else if (self.restart) {
[[GameState sharedInstance] saveState];
if ([node.name isEqualToString:#"UpgradesButton"]){
UpgradeShipScene *upgradeScene = [UpgradeShipScene sceneWithSize:(self.frame.size)];
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:upgradeScene transition:transition];
}
...
}
UpgradesScene.m
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"BackButton"]){
for (SKNode *node in [self children]){
[node removeFromParent];
}
GamePlayScene *scene = [GamePlayScene sceneWithSize:self.view.bounds.size];
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:scene transition:transition];
}
}
You can use a SKNode as a "fake" SKScene, and then use a SKAction to animate it (to present or hide).
I have a SKSpriteNode, which is a ball. My view don't have gravity. I'm able to move the ball by applying an impulse over it in the start and I want to make it to move to the user touch point which we can get in -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method without affecting its motion.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInView:self.view];
SKAction *moveToPoint = [SKAction moveByX:location.x y:location.y duration:2];
[ball runAction:moveToPoint];
}
}
Its not seems to be working. Help me guys.
If you want to send the node directly to a point use the method:
[SKAction moveTo:location duration:2];
The SKAction moveBy: method moves the node by the x and y values provided, whereas the moveTo method to the point in the node's parent.
EDIT:
Also, change the line:
CGPoint location = [touch locationInView:self.view];
to
CGPoint location = [touch locationInNode:self];
A UIView has a different coordinate system than a SKScene.
EDIT 2: You can remove affect of physics while touch exists
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
ball.physicsBody.dynamic = NO;
CGPoint location = [touch locationInNode: self];
SKAction *moveToPoint = [SKAction moveTo:location duration:2];
[ball runAction:moveToPoint];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
ball.physicsBody.dynamic = YES;
}
So... I am working on this small Mario-like game for iOS and have run into a problem that I cannot seem to fix.
When using my controls (see the screenshot) they sometime get "stuck" as in, the iOS device did not notice that I let go of a button. This causes the player to keep on moving in a direction after you have let go of the button.
Edit: It seems that the malfunction happens when I click two buttons at the exact same time.
Hopefully, someone will be able to spot what I can do differently in the following code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
[self touchStateChanged:touchLocation buttonState:YES];
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint prevTouchLocation = [touch previousLocationInNode:self];
[self touchStateChanged:prevTouchLocation buttonState:NO];
CGPoint touchLocation = [touch locationInNode:self];
[self touchStateChanged:touchLocation buttonState:YES];
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Some other logic... {...}
[self touchStateChanged:touchLocation buttonState:NO];
}
}
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
CGPoint touchLocation = [touch locationInNode:self];
[self touchStateChanged:touchLocation buttonState:NO];
}
}
-(void)touchStateChanged:(CGPoint)location buttonState:(BOOL)state
{
SKNode *n = [self nodeAtPoint:location];
if ([n.name isEqualToString:#"LeftMove"]) {
self.player.moveLeft = state;
}
else if ([n.name isEqualToString:#"RightMove"]) {
self.player.moveRight = state;
}
else if ([n.name isEqualToString:#"Jump"]) {
self.player.didJump = state;
}
}
Screenshot showing buttons:
Thanks :-)
Partly Solved...
Current solution:
Added an instance variable called _touchEvent of type UIEvent and added the following line to touchesEnded:
_touchEvent = event;
Added the following code to my update method:
if (_touchEvent && (int)[[_touchEvent allTouches]count] == 0) {
self.player.moveLeft = NO;
self.player.moveRight = NO;
self.player.didJump = NO;
}
if (_touchEvent && (int)[[_touchEvent allTouches]count] == 1) {
for (UITouch *touch in [_touchEvent allTouches]) {
SKNode *n = [self nodeAtPoint:[touch locationInNode:self]];
if ([n.name isEqualToString:#"Jump"]) {
self.player.moveLeft = NO;
self.player.moveRight = NO;
}
}
}
I think the problem is the location of touch (in -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event method) is outside any node.
Try to implement something like this
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (SKNode *node in yourButtonsArray) {
// Some other logic... {...}
[node setState:NO];
}
}
Or you should take a look at KKButtonBehaviour from KoboldKit framework.
I'm trying to pause the scene and I can pause it with this code but how do I resume it when I click the same button?
#property (SK_NONATOMIC_IOSONLY, getter = isPaused) BOOL paused;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
SKSpriteNode *pause = (SKSpriteNode*)[self childNodeWithName:#"pause"];
CGPoint location = [touch locationInNode:self];
if([pause containsPoint:location])
{
self.scene.view.paused = YES;
}
}
}
Try this:
self.scene.view.paused = !self.scene.view.paused;
I got the following Warning:
Incompatible pointer types sending 'MenuScene' to parameter of type 'uiviewcontroller'
I want to call the leaderboard from Game Center by klick on this button in my menu scene.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode: self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"playButton"]) {
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
MyScene *gameScene = [[MyScene alloc]initWithSize:CGSizeMake(self.size.width, self.size.height)];
[self.scene.view presentScene:gameScene transition:transition];
} else if ([node.name isEqualToString:#"leaderboardButton"]) {
[[GameCenterHelper defaultHelper] showLeaderboardOnViewController:self];
}
at this last lane, i got the warning
you should present a ViewController from root view controller, so you should do:
if ([node.name isEqualToString:#"leaderboardButton"]) {
UIViewController *vc = self.view.window.rootViewController;
[[GameCenterHelper defaultHelper] showLeaderboardOnViewController:vc];