Spritekit Reset Scene Problems - ios

I'm trying to have a button that when pressed, will reset the scene. The button works fine however on the second time, certain nodes are in different positions and physics contacts are being triggered. Here's my code
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
if (inGame == false) {
inGame = true;
gravity = -7;
SKAction *wait = [SKAction waitForDuration:1.5];
SKAction *moveOff = [SKAction moveBy:CGVectorMake(-25000, 0) duration:100];
SKAction *sequence = [SKAction sequence:#[wait, moveOff]];
[startRamp runAction:sequence];
[tapToStart runAction:sequence];
SKSpriteNode *ball = [SKSpriteNode spriteNodeWithImageNamed:#"Ball"];
ball.position = CGPointMake(CGRectGetMaxX(self.frame)/(CGRectGetMaxX(self.frame)/64), CGRectGetMidY(self.frame)*1.3);
ball.scale = CGRectGetMaxX(self.frame)/((CGRectGetMaxX(self.frame)*8));
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:ball.size.width/2];
ball.physicsBody.categoryBitMask = playerMask;
ball.physicsBody.contactTestBitMask = playerMask2;
[self addChild:ball];
nodeLocation = ball;
ball = nodeLocation;
}
else if (timeCountStart >= 90) gravity *= -1;
else if (moveNextScene == true) {
[self.view presentScene:self.scene transition:[SKTransition doorwayWithDuration:2]];
}
moveNextScene = true;
}

Related

Game increases node count drastically and freezes

I have created two nodes, a projectile node and a target node, each with their respective categories and when they both contact each other, both are removed and depending on the projectile, even more projectiles appear.
The projectiles also are removed when they touch the edge of the screen.
Everything works fine until after a "wave" of targets (I created a method to occasionally spawn 5 targets in a set design) appear and scroll across the screen and projectiles are all over the place contacting more and more targets, now I would understand the node count increasing, but the amount is no where near the amount, the game then lags to the point of the FPS being 2 FPS and the game eventually freezing and the node count stuck to 700.
didBeginContact Method
static inline Enemies *nodeFromBody(SKPhysicsBody *body1, SKPhysicsBody *body2, uint32_t category) {
Enemies *node = nil;
if (body1.categoryBitMask & category) {
node = (Enemies *)body1.node;
}
else if (body2.categoryBitMask & category) {
node = (Enemies *)body2.node;
}
return node;
}
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
SKSpriteNode *projectile = nil;
SKSpriteNode *offScreen = nil;
Enemies *target = nil;
firstBody = contact.bodyA;
secondBody = contact.bodyB;
projectile = nodeFromBody(firstBody, secondBody, projectileCategory);
offScreen = nodeFromBody(firstBody, secondBody, offScreenCategory);
target = nodeFromBody(firstBody, secondBody, targetsCategory);
if (projectile && target) {
if ([projectile.name isEqualToString:#"firstShot"]) {
NSLog(#"firstShot");
[projectile removeAllActions];
[projectile removeFromParent];
[ProjectilePatterns pattern1:self spawnPoint:target.position];
[target removeAllActions];
[target removeFromParent];
[Enemies wave1:self spawnPoint:CGPointMake(CGRectGetMidX(self.frame) - 525, CGRectGetMidY(self.frame) - 50)];
}
}
enemyClassMethod
// These are the regular targets, the other method that spawns the wave is essentially creating new enemies and returns them positioned in certain positions.
+(Enemies *)wave1:(SKScene *)scene spawnPoint:(CGPoint)location {
SKAction *delay = [SKAction waitForDuration:5];
SKAction *remove = [SKAction removeFromParent];
SKAction *delayAndRemove = [SKAction sequence:#[delay, remove]];
Enemies *enemy1 = [[Enemies alloc] init];
enemy1 = [Enemies spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(30, 30)];
enemy1.position = location;
enemy1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy1.size];
enemy1.physicsBody.categoryBitMask = targetsCategory;
enemy1.physicsBody.contactTestBitMask = projectileCategory;
enemy1.physicsBody.collisionBitMask = 0;
[scene addChild:enemy1];
[enemy1 runAction:delayAndRemove];
[enemy1.physicsBody applyImpulse:CGVectorMake(12, 0)];
return enemy1;
}
Projectile Pattern
+(ProjectilePatterns *)pattern1:(SKScene *)scene spawnPoint:(CGPoint)location {
SKAction *rotate = [SKAction rotateByAngle:1 duration:0.1];
SKAction *rotateForever = [SKAction repeatActionForever:rotate];
ProjectilePatterns *patternBomb = [[ProjectilePatterns alloc] init];
patternBomb = [ProjectilePatterns spriteNodeWithImageNamed:#"contactBomb"];
patternBomb.position = CGPointMake(location.x + 10, location.y);
patternBomb.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:patternBomb.size];
patternBomb.physicsBody.categoryBitMask = projectileCategory;
patternBomb.physicsBody.contactTestBitMask = targetsCategory | offScreenCategory;
patternBomb.physicsBody.collisionBitMask = 0;
patternBomb.physicsBody.friction = 0;
patternBomb.physicsBody.linearDamping = 0;
patternBomb.name = #"p1";
ProjectilePatterns *patternBomb2 = [[ProjectilePatterns alloc] init];
patternBomb2 = [ProjectilePatterns spriteNodeWithImageNamed:#"contactBomb"];
patternBomb2.position = CGPointMake(location.x - 10, location.y);
patternBomb2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:patternBomb2.size];
patternBomb2.physicsBody.categoryBitMask = projectileCategory;
patternBomb2.physicsBody.contactTestBitMask = targetsCategory | offScreenCategory;
patternBomb2.physicsBody.collisionBitMask = 0;
patternBomb2.physicsBody.friction = 0;
patternBomb2.physicsBody.linearDamping = 0;
patternBomb2.name = #"p1";
[scene addChild:patternBomb];
[patternBomb runAction:rotateForever];
[patternBomb.physicsBody applyImpulse:CGVectorMake(5, 0)];
[scene addChild:patternBomb2];
[patternBomb2 runAction:rotateForever];
[patternBomb2.physicsBody applyImpulse:CGVectorMake(-5, 0)];
return patternBomb;
return patternBomb2;
}
I should also note that the scene's physicsWorld gravity is (0,0). I did this so the impulses movement would be constant.

Adding a start and end screen in ios game

I am having trouble having my game begin paused so that the player has to press a button to start the game. I also have collision detection working, so when the two objects collide they just fall off of the screen. Here is my current code:
- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.physicsWorld.contactDelegate = self;
[self buildBackground];
[self startScrolling];
_firstPosition = CGPointMake(self.frame.size.width * 0.817f, self.frame.size.height * .40f);
_squirrelSprite = [SKSpriteNode spriteNodeWithImageNamed:#"squirrel"];
_squirrelSprite.position = _firstPosition;
_atFirstPosition = YES;
_squirrelSprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
_squirrelSprite.physicsBody.categoryBitMask = squirrelHitCategory;
_squirrelSprite.physicsBody.contactTestBitMask = nutHitCategory;
_squirrelSprite.physicsBody.collisionBitMask = nutHitCategory;
_squirrelSprite.physicsBody.affectedByGravity = NO;
self.physicsWorld.contactDelegate = self;
[self addChild:_squirrelSprite];
// Declare SKAction that waits 2 seconds
SKAction *wait = [SKAction waitForDuration:3.0];
// Declare SKAction block to generate the sprites
SKAction *createSpriteBlock = [SKAction runBlock:^{
const BOOL isHeads = arc4random_uniform(100) < 50;
NSString* spriteName = isHeads ? #"lightnut.png" : #"darknut.png";
SKSpriteNode *nut = [SKSpriteNode spriteNodeWithImageNamed:spriteName];
BOOL heads = arc4random_uniform(100) < 50;
nut.position = (heads)? CGPointMake(257,600) : CGPointMake(50,600);
nut.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(200,160)];
nut.physicsBody.categoryBitMask = nutHitCategory;
nut.physicsBody.contactTestBitMask = squirrelHitCategory;
nut.physicsBody.collisionBitMask = squirrelHitCategory;
nut.physicsBody.affectedByGravity = NO;
self.physicsWorld.contactDelegate = self;
[self addChild: nut];
SKAction *moveNodeUp = [SKAction moveByX:0.0 y:-700.0 duration:1.3];
[nut runAction: moveNodeUp];
}];
// Combine the actions
SKAction *waitThenRunBlock = [SKAction sequence:#[wait,createSpriteBlock]];
// Lather, rinse, repeat
[self runAction:[SKAction repeatActionForever:waitThenRunBlock]];
}
return self;
}
My touchesBegan:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (_atFirstPosition)
{
SKAction *moveNodeLeft = [SKAction moveByX:-207.8 y:0.0 duration:0.85];
[_squirrelSprite runAction: moveNodeLeft withKey:#"moveleft"];
} else {
SKAction *moveNodeRight = [SKAction moveByX:207.8 y:0.0 duration:0.85];
[_squirrelSprite runAction: moveNodeRight withKey:#"moveright"];
}
_atFirstPosition = !_atFirstPosition;
_squirrelSprite.xScale *= -1.0;
}
Finally, my didBeginContact:
-(void)didBeginContact:(SKPhysicsContact *)contact
{
SKPhysicsBody *firstBody, *secondBody;
firstBody = contact.bodyA;
secondBody = contact.bodyB;
if(firstBody.categoryBitMask == squirrelHitCategory || secondBody.categoryBitMask == nutHitCategory)
{
}
}
I've been trying to figure this out for the last 2 weeks and I have read many tutorials and code to start the game with a simple start button and end screen, but nothing has worked. Any help is greatly appreciated!
I would use a navigation controller and have your start button on the root view and have that button push a viewcontroller that presents your game.

stop impulse on SKSpriteKit click

i'm trying to create an pause button in my game. at the moment when you click on the screen it will apply an impulse on the mover spriteNode. The problem is that it still applying the impulse when i click the pause button. How can i create an pause button without creating the impulse also.
i've tried changing the if statements to an else if and then making the last impulse part the else part, but this wont work.
touchBegan method:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
//if fire button touched, bring the rain
if ([node.name isEqualToString:#"repeat"]) {
SKTransition *reveal = [SKTransition fadeWithDuration:2.0 ];
MyScene *newScene = [[MyScene 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:#"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 (noImpulse == 1) {
if (started == 1) {
mover.physicsBody.restitution = 0.0;
mover.physicsBody.velocity = CGVectorMake(0, 0);
[mover.physicsBody applyImpulse:CGVectorMake(0, 15)];
}
}
}
PAUSEDMENU METHOD:
-(void)pausedMenu
{
self.scene.paused = YES;
mover.scene.paused = YES;
[repeatButton removeFromParent];
[homeButton removeFromParent];
[menuBackground removeFromParent];
menuBackground = [SKSpriteNode spriteNodeWithColor:[SKColor colorWithWhite:0.0 alpha:0.3] size:CGSizeMake(self.frame.size.width*2, self.frame.size.height*2)];
[self addChild:menuBackground];
[pauseButton removeFromParent];
startButton = [SKSpriteNode spriteNodeWithImageNamed:#"staart"];
startButton.name = #"start";
startButton.position = CGPointMake(20, self.size.height-20);
startButton.size = CGSizeMake(40, 40);
[self addChild:startButton];
repeatButton = [SKSpriteNode spriteNodeWithImageNamed:#"repeat"];
repeatButton.name = #"repeat";
repeatButton.position = CGPointMake(self.size.width/2+30, self.size.height/2);
repeatButton.size = CGSizeMake(repeatButton.size.width/2, repeatButton.size.height/2);
[self addChild:repeatButton];
homeButton = [SKSpriteNode spriteNodeWithImageNamed:#"home"];
homeButton.name = #"home";
homeButton.position = CGPointMake(self.size.width/2-30, self.size.height/2);
homeButton.size = CGSizeMake(homeButton.size.width/2, homeButton.size.height/2);
[self addChild:homeButton];
}
Check out
https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKView/Reference/Reference.html#//apple_ref/occ/instp/SKView/paused
If you pause your SKView that contains the game, all the state will be truly paused and you will be able to resume it after the pause menu has been dismissed. You'll have to present the pause menu in another SKView then the one that your game is contained in.

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 - How to add objects with specific distance

in my game I am adding enemies to the scene every second , I need to describe an specific distance between pervious object that my main character move through these enemy , here is my codes that add enemy to the scene :
- (void)createEnemy {
int GoOrNot = [self getRandomNumberBetween:0 to:1];
if(GoOrNot == 1){
int randomEnemy = [self getRandomNumberBetween:0 to:1];
if(randomEnemy == 0)
enemy = [[SKSpriteNode alloc]initWithImageNamed:#"car.png"];
else
enemy = [[SKSpriteNode alloc]initWithImageNamed:#"block.png"];
int xPostion = [self placeRandomObject] ;
int yPostion = 1150;
enemy.position = CGPointMake(xPostion, yPostion);
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy.size];
enemy.name = #"enemy";
enemy.physicsBody.categoryBitMask = enemyCategory;
enemy.physicsBody.contactTestBitMask = carCategory;
enemy.physicsBody.collisionBitMask = 0;
enemy.physicsBody.dynamic = YES;
enemy.physicsBody.usesPreciseCollisionDetection = YES;
[self addChild:enemy];
SKAction *wait = [SKAction waitForDuration:.20];
SKAction *move = [SKAction moveToY:self.scene.frame.origin.y-10 duration:enemySpeed];
SKAction *remove = [SKAction removeFromParent];
SKAction *runAction = [SKAction sequence:#[wait,move , remove]];
[enemy runAction:[SKAction repeatActionForever:runAction]];
}
}
adding enemy :
- (void)addEnemies {
SKAction *wait = [SKAction waitForDuration:.55];
SKAction *callEnemies = [SKAction runBlock:^{ [self createEnemy];}];
updateEnimies = [SKAction sequence:#[wait,callEnemies]];
[self runAction:[SKAction repeatActionForever:updateEnimies] withKey:#"addEnemy"];
}
You can create new variable to hold the last position of the previous enemy.
CGPoint positionOfLastEnemy;
when you create new enemy assign the position of created enemy to this variable:
positionOfLastEnemy = CGPointMake(xPostion, yPostion);
if you need to update the enemy position you can do it in your collision detection method.
After that just use the positionOfLastEnemy variable to calculate the distance you need.
The other way is to add variable to hold the last enemy
SKSpriteNode *lastEnemy;
in your createEnemy method save reference to your created enemy.
lastEnemy = enemy;
And if you need the distance of the last enemy just use
lastEnemy.position

Resources