Move a SKSpriteNode with a single tap, but move continously when long tap - Sprite Kit - ios

I'm trying to change some SKActions of an existing Sprite Kit tutorial project, but I'm running into issues when it comes to movement. The Tutorial and GitHub project is here:
https://www.codefellows.org/blogs/simple-sprite-kit-game-tutorial-part1
https://github.com/megharastogi/GameTutorial
As you can see in the code below, each tap only moves the node once. How do I change it so that a long tap will move continuous move the node? I tried a few things like repeatActionForever, but that didn't work very well.
-(void)addShip
{
//initalizing spaceship node
ship = [SKSpriteNode spriteNodeWithImageNamed:#"Spaceship"];
[ship setScale:0.5];
ship.zRotation = - M_PI / 2;
//Adding SpriteKit physicsBody for collision detection
ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship.size];
ship.physicsBody.categoryBitMask = shipCategory;
ship.physicsBody.dynamic = YES;
ship.physicsBody.contactTestBitMask = obstacleCategory;
ship.physicsBody.collisionBitMask = 0;
ship.physicsBody.usesPreciseCollisionDetection = YES;
ship.name = #"ship";
ship.position = CGPointMake(120,160);
actionMoveUp = [SKAction moveByX:0 y:30 duration:.2];
actionMoveDown = [SKAction moveByX:0 y:-30 duration:.2];
[self addChild:ship];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if(touchLocation.y >ship.position.y){
if(ship.position.y < 270){
[ship runAction:actionMoveUp];
}
}else{
if(ship.position.y > 50){
[ship runAction:actionMoveDown];
}
}
}

- (void)didMoveToView:(SKView *)view
{
UILongPressGestureRecognizer *tapper = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(tappedScreen:)];
tapper.minimumPressDuration = 0.1;
[view addGestureRecognizer:tapper];
}
- (void)tappedScreen:(UITapGestureRecognizer *)recognizer
{
float touchY = [self convertPointFromView:[recognizer locationInView:self.view]].y;
SKSpriteNode *ship = [self childNodeWithName:#"ship"];
if (recognizer.state == UIGestureRecognizerStateBegan) {
if(touchY >ship.position.y){
[ship runAction:[SKAction repeatActionForever:actionMoveUp] withKey:#"longTap"];
}else{
[ship runAction:[SKAction repeatActionForever:actionMoveDown] withKey:#"longTap"];
}
}
if (recognizer.state == UIGestureRecognizerStateEnded) {
[ship removeActionForKey:#"longTap"];
}
}
Add these two methods in your code.

Related

Two touches in touchesBegan in objective c

I have two touches in method "touchesBegan" they both starts at the same time, but i want to second touch to wait while first touch end. How can i achieve that? Here are what I have:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location =[touch locationInView:self.view];
SKNode *touchedNode = [self nodeAtPoint:location];
if(touchedNode && [touchedNode.name isEqual:#"tapButton"]){
[self runAction:[SKAction playSoundFileNamed:#"buttons.wav" waitForCompletion:NO]];
[self createBackLn];
[self createBottom];
[self ballCreate];
[tapButton removeFromParent];
}
if (groupSprite.children.count < 3) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, IS_IPAD()?108: 75);
sprite = [SKSpriteNode spriteNodeWithImageNamed:#"board"];
if(IS_IPAD()){
sprite.xScale = 0.5;
sprite.yScale = 0.7;
}else{
sprite.xScale = 0.3;
sprite.yScale = 0.5;
}
sprite.position = location;
sprite.zPosition = 2;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.physicsBody.categoryBitMask = boardCategory;
sprite.physicsBody.contactTestBitMask = ballCategory | secballCategory;
SKAction *delay = [SKAction waitForDuration:IS_IPAD()?0.6:0.4];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:#[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
}
}
}

Issues with touchesBegan method

So the problem is that when I start it the soundOff button is above the soundOn but it's invisible. So all I see is the soundOn button, when I try to tap the soundOn button it really is just hitting the soundOff button and not giving it a chance to run the touchesBegan method properly.
#implementation GameScene
{
SKSpriteNode *soundLogo;
SKSpriteNode *soundOff;
}
-(void) addSoundOff:(CGSize)size {
soundOff = [SKSpriteNode spriteNodeWithImageNamed:#"soundOff"];
//resize sprite
soundOff.size = CGSizeMake(soundOff.size.width/2.25, soundOff.size.height/2.25);
//position it
soundOff.position = CGPointMake(65, 25);
//name sound off
soundOff.name = #"soundOff";
soundOff.alpha = 0;
[self addChild:soundOff];
}
-(void) addSoundOn:(CGSize)size {
SKTexture *soundOn = [SKTexture textureWithImageNamed:#"soundLogo"];
soundLogo = [SKSpriteNode spriteNodeWithTexture:soundOn];
//resize sprite
soundLogo.size = CGSizeMake(soundLogo.size.width/2.25, soundLogo.size.height/2.25);
//position sprite
CGPoint myPoint = CGPointMake(65, 25);
soundLogo.position = myPoint;
//name sound logo
soundLogo.name = #"soundOn";
//add action
[self addChild:soundLogo];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//sound logo pressed to turn sound on/off
if ([node.name isEqualToString:#"soundOn"]) {
soundOff.alpha = 1;
soundLogo.alpha = 0;
NSLog(#"sound on is pressed");
}
if ([node.name isEqualToString:#"soundOff"]) {
soundOff.alpha = 0;
soundLogo.alpha = 1;
NSLog(#"sound off is pressed");
}
}
Rather then changing the alpha to 0/1 what you can do is remove sprite from parent and you can add to child.
Don't add sound off and sound on button at same time first add the default button that is sound on.
now when the sound on button is clicked then remove the sound on button from parent and add sound off.
-(void) addSoundOff:(CGSize)size {
soundOff = [SKSpriteNode spriteNodeWithImageNamed:#"soundOff"];
//resize sprite
soundOff.size = CGSizeMake(soundOff.size.width/2.25, soundOff.size.height/2.25);
//position it
soundOff.position = CGPointMake(65, 25);
//name sound off
soundOff.name = #"soundOff";
soundOff.alpha = 0;
//-------------Remove below line-----------------//
//[self addChild:soundOff];
//Dont add sound off button.....
}
-(void) addSoundOn:(CGSize)size {
SKTexture *soundOn = [SKTexture textureWithImageNamed:#"soundLogo"];
soundLogo = [SKSpriteNode spriteNodeWithTexture:soundOn];
//resize sprite
soundLogo.size = CGSizeMake(soundLogo.size.width/2.25, soundLogo.size.height/2.25);
//position sprite
CGPoint myPoint = CGPointMake(65, 25);
soundLogo.position = myPoint;
//name sound logo
soundLogo.name = #"soundOn";
//add action
[self addChild:soundLogo];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//sound logo pressed to turn sound on/off
if ([node.name isEqualToString:#"soundOn"]) {
[soundLogo removeFromParent];
[self addChild:soundOff];
NSLog(#"sound on is pressed");
}
if ([node.name isEqualToString:#"soundOff"]) {
[soundOff removeFromParent];
[self addChild:soundLogo];
}
}

SpriteKit allow only one touch

Everytime a touch is made, a node is added and will move to another node. I want to avoid that you can touch the node many times, and the node will been added as many times as you click. It should be only added once, and after the SKAction is done, you can touch the node again.
In my Code below, I tried it with userInteractionEnabled. But after the 'Zauberer' is added the third time, it recognizes no touches anymore.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
self.userInteractionEnabled = NO;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"Zauberer"]){
Wurfstein = [SKSpriteNode spriteNodeWithImageNamed:#"Wurfstein.png"];
Wurfstein.position = CGPointMake(Mensch.position.x, Mensch.position.y);
Wurfstein.zPosition = 1;
Wurfstein.scale = 0.6;
Wurfstein.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];
Wurfstein.physicsBody.dynamic = NO;
Wurfstein.physicsBody.allowsRotation = NO;
Wurfstein.physicsBody.usesPreciseCollisionDetection = YES;
Wurfstein.physicsBody.restitution = 0;
Wurfstein.physicsBody.categoryBitMask = SteinCategory ;
Wurfstein.physicsBody.collisionBitMask = ZaubererCategory;
Wurfstein.physicsBody.contactTestBitMask = ZaubererCategory;
SKAction *action = [SKAction moveTo:Zauberer.position duration:0.5];
SKAction *remove = [SKAction removeFromParent];
[self addChild:Wurfstein];
[Wurfstein runAction:[SKAction sequence:#[action,remove]]completion:^{
self.userInteractionEnabled = YES;
[Zauberer removeFromParent];
[self performSelector:#selector(Zauberer) withObject:nil afterDelay:5.0 ];
}];
}
}
Ok, I got it. If you first touch anywhere else on the screen, and than on the 'Zauberer' node, it wouldn't react. You need to put the self.userInteractionEnabled = NO; in the if sentence, to avoid the problem.
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"Zauberer"]){
self.userInteractionEnabled = NO;
Wurfstein = [SKSpriteNode spriteNodeWithImageNamed:#"Wurfstein.png"];
Wurfstein.position = CGPointMake(Mensch.position.x, Mensch.position.y);
Wurfstein.zPosition = 1;
Wurfstein.scale = 0.6;
Wurfstein.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:5];
Wurfstein.physicsBody.dynamic = NO;
Wurfstein.physicsBody.allowsRotation = NO;
Wurfstein.physicsBody.usesPreciseCollisionDetection = YES;
Wurfstein.physicsBody.restitution = 0;
Wurfstein.physicsBody.categoryBitMask = SteinCategory ;
Wurfstein.physicsBody.collisionBitMask = ZaubererCategory;
Wurfstein.physicsBody.contactTestBitMask = ZaubererCategory;
SKAction *action = [SKAction moveTo:Zauberer.position duration:0.5];
SKAction *remove = [SKAction removeFromParent];
[self addChild:Wurfstein];
[Wurfstein runAction:[SKAction sequence:#[action,remove]] completion:^{
self.userInteractionEnabled = YES;
[Zauberer removeFromParent];
[self performSelector:#selector(Zauberer) withObject:nil afterDelay:4.0 ];
}];
}
}

stop Impulse on sprite touch [duplicate]

This question already has an answer here:
stop impulse on SKSpriteKit click
(1 answer)
Closed 8 years ago.
i'm creating an game where there is a pause button. For checking wether its touched i'm checking the location of the touch and if its equal to the paused button name.
When the pausedButton is clicked its call a method which pause the scene.
The problem is that in the touchBegan method whenever you touch the screen it apply an impulse, so when i press the pauseButton and unpause it the applyforce will come after. This is not ideal for the game. I've tried with a bool like shouldImpulse, but havent got it to work. here is my touchedBegan method:
-(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:#"home"]) {
SKTransition *reveal = [SKTransition fadeWithDuration:2.0 ];
Menu *newScene = [[Menu alloc] initWithSize: CGSizeMake(self.size.width,self.size.height)];
// Optionally, insert code to configure the new scene.
[self.scene.view presentScene: newScene transition: reveal];
}
if ([node.name isEqualToString:#"pause"]) {
[self pausedMenu];
}
if ([node.name isEqualToString:#"start"]) {
[self startMenu];
}
showpipes = showpipes + 1;
if (showpipes == 1) {
self.physicsWorld.gravity = CGVectorMake( 0.0, -5.0 );
SKAction* spawn = [SKAction performSelector:#selector(spawnPipes) onTarget:self];
SKAction* delay = [SKAction waitForDuration:2.0];
SKAction* spawnThenDelay = [SKAction sequence:#[spawn, delay]];
SKAction* spawnThenDelayForever = [SKAction repeatActionForever:spawnThenDelay];
[self runAction:spawnThenDelayForever];
}
started = 1;
if (started == 1) {
mover.physicsBody.restitution = 0.0;
mover.physicsBody.velocity = CGVectorMake(0, 0);
[mover.physicsBody applyImpulse:CGVectorMake(0, 15)];
}
}
You can do that with a simple IF, ELSE IF, ELSE:
if([node.name isEqualToString:#"home"])
{
// do stuff...
} else if ([node.name isEqualToString:#"pause"])
{
// do stuff...
} else if ([node.name isEqualToString:#"start"])
{
// do stuff...
} else
{
// do whatever else here...
}

SpriteKit Clicker Game Select Spawn Area for Clickable things?

I'm New to SpriteKit and build a simple CLickerGame in SpriteKit. Now i have the game finish but the thing which should spawn to click on it, spawns from time to time outside the screen. Where can i select the arc4random area where the thing should spawn?
#implementation MyScene
-(void)CreateTestObject
{
SKSpriteNode *TestObject = [SKSpriteNode spriteNodeWithImageNamed:#"TestObject"];
int maxX = CGRectGetMaxX(self.frame);
float ranX = (arc4random()%maxX) + 1;
int maxY = CGRectGetMaxY(self.frame);
float ranY = (arc4random()%maxY) + 1;
TestObject.position = CGPointMake(ranX, ranY);
TestObject = CGSizeMake(75, 75);
TestObject = #"Object";
[self addChild:TestObject];
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
bg = [SKSpriteNode spriteNodeWithImageNamed:#"bg.png"];
bg.position = CGPointMake(160, 284);
bg.size = CGSizeMake(self.frame.size.width, self.frame.size.height);
[self addChild:bg];
[self CreateTestObject];
}
return self;
}
-(void)selectNodeForTouch:(CGPoint)touchLocation
{
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
if([[touchedNode name] isEqualToString:#"TestObject"])
{
SKSpriteNode *TestObject = (SKSpriteNode *)[self childNodeWithName:#"TestObject"];
TestObject.name = #"DisabledTestObject";
SKAction *grow = [SKAction scaleTo:1.2 duration:0.1];
SKAction*shrink = [SKAction scaleTo:0 duration:0.07];
SKAction *removeNode = [SKAction removeFromParent];
SKAction *seq = [SKAction sequence:#[grow, shrink, removeNode]];
[TestObject runAction:seq];
[self CreateTestObject];
[self runAction:[SKAction playSoundFileNamed:#"Sound.aif" waitForCompletion:NO]];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches)
{
//CGPoint location = [touch locationInNode:self];
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
[self selectNodeForTouch:positionInScene];
}
}
#end
Modify the max values for your (x,y) coordinates of any object being added to your view.
For example, on an iPhone 5 display in landscape mode and the spawned object being of size width=100, height=100, the max x value should be no more than 518. Width of screen being 568 less half the width of your object (50).
The same logic applies for the y value. The max y value should be 270 given the same object dimensions.
The above assumes that you have not changed your object's anchor point from (0.5,0.5) to another value.

Resources