iOS Endless Runner Lag SpriteKit - ios

I have an iOS endless runner game where the path appears from the top and scrolls down.
Here's how I do it: 2 sprites are created and when the first one disappears from the screen, I load another sprite from an image and run the same logic.
The problem: There's a small lag right after the first sprite disappears and the new one appears.
Edit: This problem only occurs when running on iPhone 5. iPhone 4 works perfectly !
Anyone have any ideas? or has run into this problem before and solved it?
if (CurrentObstacle.frame.origin.y < -self.frame.size.height + 10) {
// clear old obstacle
CurrentObstacle = nil;
CurrentObstacle = nextObstacle;
currentObstacleImage = nextObstacleImage; // for pixel processing stuff
[self generateObstacle];
}
- (void)generateObstacle{
// genrate random image name
int i = rand()%10+1;
NSString *imageName = [NSString stringWithFormat:#"ob%i", i];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if(screenSize.height == 480)
{
//Load 3.5 size
imageName = [imageName stringByAppendingString:#"small"];
}
imageName = [imageName stringByAppendingString:#".png"];
// create obstacle
nextObstacle = [SKSpriteNode spriteNodeWithImageNamed:imageName];
nextObstacleImage = [imageDictionary objectForKey:imageName];
nextObstacle.size = self.size;
nextObstacle.position = CGPointMake(self.view.center.x, 1.48*nextObstacle.size.height);
// set speed to be the same as the other obstacles
nextObstacle.speed = CurrentObstacle.speed;
// show obstacle
[self addChild:nextObstacle];
// move obstacle
SKAction *moveAction = [SKAction moveByX:0 y:-2*self.size.height-1 duration:8];
[nextObstacle runAction:moveAction];
}

Ok, solved this problem by removing the #2x images. Apparently processing big images was the cause of the lag.
Hope this helps someone!

You shouldn't load the image each time you want to add an obstacle. You should do a methode like this that you would call at "initWithSize"
//MyScene.h
#interface MyScene : SKScene{
SKTexture * textureObstacle1;
SKTexture * textureObstacle2;
//...//
}
-(id)initWithSize;
-(void)LoadSprites;
#end
//MyScene.m
#implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self LoadSprites];
}
return self;
}
-(void)LoadSprites{
textureObstacle1=[[SKTexture alloc] initWithImageNamed:#"obstacle1.png"];
//...//
}
- (void)generateObstacle{
//...//
nextObstacle = [SKSpriteNode alloc]init];
[nextObstacle setTexture:textureObstacle1];
}
#end

Related

Objective-C. How is your first SKScene initialised?

This method sets up the initial scene and is called as soon as the application opens:
#import "GameScene.h"
#import "WarScene.h"
#import "Math.h"
#implementation GameScene
{
SKNode *map;
float oldY;
float oldX;
BOOL buttonClicked;
int thisButton;
float oldMapPosition;
float midScreenX;
float midScreenY;
int separation;
float sendY;
BOOL interacting;
CGVector speed;
float oldPosition;
float newPosition;
BOOL isReleased;
int iterations;
float diff;
BOOL comeToStop;
BOOL actualStop;
BOOL clicked;
}
int numberOfLevels = 20;
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
map = [SKNode node];
separation = [UIScreen mainScreen].bounds.size.height;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
//landscape mode
midScreenX = ([[UIScreen mainScreen]bounds].size.width>[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
midScreenY = ([[UIScreen mainScreen]bounds].size.width<[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
}
else
{
//portrait mode
midScreenX = [[UIScreen mainScreen]bounds].size.width/2;
midScreenY = [[UIScreen mainScreen]bounds].size.height/2;
}
NSLog(#"Initial Width: %f Height: %f", midScreenX*2, midScreenY*2);
map.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: CGSizeMake([UIScreen mainScreen].bounds.size.width, separation*(numberOfLevels+1))];
map.physicsBody.affectedByGravity = NO;
map.physicsBody.allowsRotation = NO;
clicked = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
self.backgroundColor = [SKColor blackColor];
for (int i = 0; i<numberOfLevels; i += 1) {
SKLabelNode *title;
{
title = [SKLabelNode labelNodeWithFontNamed:#"Cochin"];
title.text = [NSString stringWithFormat: #"Level %d", 1+i];
title.fontSize = 60;
title.position = CGPointMake(CGRectGetMidX(self.frame),
((i+1)*separation));
title.fontColor = [UIColor whiteColor];
title.name = [NSString stringWithFormat: #"Level %d", 1+i];
[map addChild:title];
}
}
[self addChild:map];
}
This works exactly as I intended it to, however, when I call this from another class:
-(void)continueMethod:(UIButton*)button{
NSLog(#"InitwithSize to GameScene Width: %f Height: %f", midScreenX*2, midScreenY*2);
GameScene *map = [[GameScene alloc] initWithSize: CGSizeMake(midScreenX*2 ,midScreenY*2)];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
SKView * skView = (SKView *)self.view;
UIView *viewToRemove = [self.view viewWithTag:3];
[viewToRemove removeFromSuperview];
[skView presentScene:map transition:reveal];
}
The scene which should be set up doesn't appear as I intended it to, or indeed how it used to look the last time that it was initialised. The separation variable appears bigger, the text appears bigger and everything is wider. I've verified that the UIScreen that I have initialised is exactly the same throughout.
This led me to question how the initial scene of a SKSprite application comes to be and whether it is different to "initWithSize:" however, looking through all the code that comes in the game template I can't ever see my first scene get called. The closest that I can find to initWithSize: is the following from the GameViewController-
GameScene *scene = [GameScene unarchiveFromFile:#"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;
Firstly, is this what is initialising my initial scene? Secondly is it in anyway different to initWithSize: ? And finally if the answer to the previous tow questions is yes, should it be used instead of initWithsize: ?
Yes, this is the initialization of the initial scene! What you are seeing in the View Controller is a slightly different initialization. You are using the sks file, first of all, and initWithCoder: is being called when the view controller unarchives it. However, this isn't very different from initWithSize if you specify all of the same dimensions and properties.
That being said, the scene's scaleMode in your initialization and the view controller's scaleMode is different. As you can see, the view controller specifies:
scene.scaleMode = SKSceneScaleModeAspectFill;
The AspectFill scale mode according to Apple:
The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible but may require letterboxing in the view.
When you initialize your scene, on the other hand, you leave the default scaleMode, which happens to be SKSceneScaleModeFill. This maps each axis of the scene to the view, so you get a distorted scene. What you need to do is simply state:
map.scaleMode = SKSceneScaleModeAspectFill;
This will use the same scale mode as in the original scene and the distortion that you see now will be gone.
Hope this helps, let me know if you have questions.

Using CCScrollView in Cocos2d V3

I'm wondering if anyone can help me with this. I want to add 4 CCScrollViews, that will scroll horizontally, on a CCNode. The CCNode, positioned and held on the device in portrait mode, will fill the entire screen with a normalized contentSize set to cover the entire screen.
So in essence the scroll views will pile on top of each other with the 4th being at the bottom of the screen and the 1st being at the top. Now, I have managed to add the 4 CCScrollViews but only one responds to touches. The others are flat an unmovable. It's almost as though the last CCScrollView added to the node is overlaying the other three and it is the only thing responding to touch requests. All 4 CCScrollViews have their delegate property set to self.
I'm someone coming at Cocos2d with a fair bit of UIKit experience. So, i'm trying to apply my UIScrollView mode of thinking to all of this. Having had a good Google about and coming up with little, I'm wondering if SO can help. I've even been considering winding UIScrollView into Cocos2d.
I guess my main issues here are two-fold. One, I have an issue with touch response. Two, I have an issue with the paging aspect and control of content-size. Via trial and error, I'm sort of getting along but if someone could perhaps write up a bit of a best-practive guide to CCScrollView implementation, specifically where one does not set the contentSize of the CCNode or CCspriteFrame that's added to larger contentView does not fill the entire width of the screen.
Thanks in advance.
#import "CCToolKit.h"
#import "GameShip.h"
typedef enum ShipPart
{
ShipPartHead,
ShipPartBody,
ShipPartWings,
ShipPartBoosters
} ShipPart;
#implementation GameShip
-(instancetype)init {
if (self = [super init]) {
[self addBackground];
[self addScrollTo:ShipPartHead forQtyOfParts:3];
[self addScrollTo:ShipPartBody forQtyOfParts:4];
[self addScrollTo:ShipPartWings forQtyOfParts:3];
[self addScrollTo:ShipPartBoosters forQtyOfParts:3];
}
return self;
}
-(void)addBackground {
CCSprite *bg = [CCSprite spriteWithImageNamed:kGameMainBackGround];
bg.positionType = CCPositionTypeNormalized;
bg.position = ccp(0.5f,0.5f);
[self addChild:bg];
}
-(void)addScrollTo:(ShipPart)shipPart forQtyOfParts:(int)partQty {
NSString *imageFileNameSegment;
switch (shipPart) {
case ShipPartHead:
imageFileNameSegment = #"head";
break;
case ShipPartBody:
imageFileNameSegment = #"body";
break;
case ShipPartWings:
imageFileNameSegment = #"wings";
break;
case ShipPartBoosters:
imageFileNameSegment = #"boosters";
break;
default:
break;
}
CCNode *scrollViewContents = [CCNode node];
scrollViewContents.contentSizeType = CCSizeTypeNormalized;
scrollViewContents.contentSize = CGSizeMake(partQty * 0.65, 0.25f);
NSLog(#"scrollView,height %f", scrollViewContents.boundingBox.size.height);
for (int i = 1; i <= partQty; i++) {
NSString *imageFileName = [NSString stringWithFormat:#"%#%d.png", imageFileNameSegment, i];
CCSprite *shipPartSprite = [CCSprite spriteWithImageNamed:imageFileName];
shipPartSprite.positionType = CCPositionTypeNormalized;
shipPartSprite.position = ccp((i + 0.5f) / partQty, 0.5f);
[scrollViewContents addChild:shipPartSprite];
}
CCScrollView *scrollView = [[CCScrollView alloc] initWithContentNode:scrollViewContents];
scrollView.pagingEnabled = YES;
scrollView.horizontalScrollEnabled = YES;
scrollView.verticalScrollEnabled = NO;
scrollView.color = [CCColor redColor];
scrollView.contentSize = CGSizeMake(0.5f, 0.25f);
scrollView.positionType = CCPositionTypeNormalized;
scrollView.position = ccp(-1.0f, ((shipPart * 0.25f) -0.1f));
scrollView.delegate = self;
//scrollView.description = [NSString stringWithFormat:#"%d", shipPart];
[self addChild:scrollView];
//[scrollView setHorizontalPage:1];
}

SpriteKit change Image after swipe gesture

I am trying to code a game. I got an object, that can jump and slide.
I want to hold the animation of 'run' while jumping, but while sliding, I want to change the image. My problem: the image won't show off , if I just change the image to 'slide7'.
Nothing happens.
The slide animation should appear only for about 4 seconds, than go again into the run animation. Any suggestions?
My Code :
-(void)Mensch{
SKTexture * MenschTexture1 = [SKTexture textureWithImageNamed:#"Mensch1"];
MenschTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture * MenschTexture2 = [SKTexture textureWithImageNamed:#"Mensch2"];
MenschTexture2.filteringMode = SKTextureFilteringNearest;
SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:#[MenschTexture1, MenschTexture2] timePerFrame:0.4]];
Mensch = [SKSpriteNode spriteNodeWithTexture:MenschTexture1];
Mensch.size = CGSizeMake(45, 45);
Mensch.position = CGPointMake(self.frame.size.width / 5, Boden.position.y + 73);
Mensch.zPosition = 2;
Mensch.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Mensch.size];
Mensch.physicsBody.dynamic = YES;
Mensch.physicsBody.allowsRotation = NO;
Mensch.physicsBody.usesPreciseCollisionDetection = YES;
Mensch.physicsBody.restitution = 0;
Mensch.physicsBody.velocity = CGVectorMake(0, 0);
[Mensch runAction:Run];
[self addChild:Mensch];
}
- (void)didMoveToView:(SKView *)view
{
UISwipeGestureRecognizer *recognizerDown = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(handleSwipeDown:)];
recognizerDown.direction = UISwipeGestureRecognizerDirectionDown;
[[self view] addGestureRecognizer:recognizerDown];
}
-(void)handleSwipeDown:(UISwipeGestureRecognizer *)sender
{
[Mensch removeAllActions];
Mensch = [SKSpriteNode spriteNodeWithImageNamed:#"slide7.png"];
NSLog(#"Slide");
}
You are replacing the Mensch object. More specifically, you are replacing the pointer you have to the object, but that doesn't stop it from being displayed in the scene.
You can either:
Replace the SKTexture inside the sprite, which should be displayed immediately. This means Mensch.texture = [SKTexture textureWithImageNamed:#"slide7.png"];
Replace the Sprite. This entails removing the current sprite ([sprite removeFromParent]) and adding the new one ([self addChild:newSprite]).
I think you want the first one, since you don't have to recreate the Mensch object.
Might I add that you are performing a lot of Mensch-specific logic in this object? It would be better (from an Object Oriented standpoint) to move this creation code to an SKSpriteNode subclass Mensch.
#interface Mensch : SKSpriteNode
- (void) displayRunAnimation;
- (void) displaySlideAnimation;
#end
#implementation : Mensch
- (instancetype) init {
self = [super init];
if (self) {
// your initialization code here, including physics body setup
[self displayRunAnimation];
}
return self;
}
- (void) displayRunAnimation {
SKTexture * MenschTexture1 = [SKTexture textureWithImageNamed:#"Mensch1"];
MenschTexture1.filteringMode = SKTextureFilteringNearest;
SKTexture * MenschTexture2 = [SKTexture textureWithImageNamed:#"Mensch2"];
MenschTexture2.filteringMode = SKTextureFilteringNearest;
SKAction * Run = [SKAction repeatActionForever:[SKAction animateWithTextures:#[MenschTexture1, MenschTexture2] timePerFrame:0.4]];
// if you plan to call this often, you want to cache this SKAction, since creating it over and over is a waste of resources
[self runAction:Run];
}
- (void) displaySlideAnimation {
[self removeAllActions];
self.texture = [SKTexture textureWithImageNamed:#"slide7.png"];
NSLog(#"Slide");
// re-start the runAnimation after a time interval
[self performSelector:#selector(displayRunAnimation) withObject:nil afterDelay:4.0];
}

Deallocate SKScene after transition to another SKScene in SpriteKit

I have a view controller that has three skscenes as children.
When I transition from one to another, the old skscene doesn't get deallocated.
I want it to get deallocated as if it was never there.
Example:
When I first load the app, only 1 skscene is visible (say it takes up 100mb memory), then I transition to another (100mb more), and then the third (300mb memory).
I would end up with 300mb memory and I want to have 100 at all times.
How can I achieve this?
My view controller:
//
// ViewController.m
// Paddle Jumper
//
// Created by Chance Daniel on 1/18/14.
// Copyright (c) 2014 Max Hudson. All rights reserved.
//
#import "Flurry.h"
#import "ViewController.h"
#import "startViewController.h"
#implementation ViewController{
BOOL sceneSetUp;
}
- (void)viewWillLayoutSubviews
{
if(!sceneSetUp){
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([[defaults objectForKey:#"firstTime"] intValue] != 1){
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"firstTime"];
[defaults setObject:#"ggr" forKey:#"skinSelected"];
[defaults setObject:[NSNumber numberWithInt:2] forKey:#"ggrOwned"];
[defaults setObject:[NSNumber numberWithInt:5000] forKey:#"gona"];
[defaults setObject:[NSNumber numberWithInt:1500] forKey:#"points"];
[defaults setObject:[NSNumber numberWithInt:7] forKey:#"livesLeftValue"];
[defaults setObject:[NSNumber numberWithInt:3] forKey:#"shieldsLeftValue"];
[defaults setObject:[NSNumber numberWithInt:2] forKey:#"lvlTwoLeftValue"];
[defaults setObject:[NSNumber numberWithInt:0] forKey:#"lvlThreeLeftValue"];
}
if(![defaults objectForKey:#"tut_game1"]){
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"tut_game1"];
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"tut_store"];
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"tut_daily"];
}
[defaults synchronize];
// Create and configure the scene.
SKScene * startScene = [StartViewController sceneWithSize:skView.bounds.size];
startScene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:startScene];
//[skView presentScene:scene];
sceneSetUp = YES;
}
}
-(void) switchScene{
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
return UIInterfaceOrientationMaskAll;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#end
An SKScene that won't release: //
// SettingsSKScene.m
// Paddle Jumper
//
// Created by Max Hudson on 3/15/14.
// Copyright (c) 2014 Max Hudson. All rights reserved.
//
#import "SettingsSKScene.h"
#import "gameViewController.h"
#import "StoreScene.h"
#interface SettingsSKScene (){
SKSpriteNode *bg;
SKSpriteNode *masterOverlay;
SKSpriteNode *wbox;
SKSpriteNode *gamecenter;
SKSpriteNode *max;
SKSpriteNode *chance;
SKSpriteNode *bryce;
SKSpriteNode *home;
SKSpriteNode *play;
SKSpriteNode *chance_link;
SKSpriteNode *max_link;
SKSpriteNode *bryce_link;
SKSpriteNode *fbButton;
SKSpriteNode *fbToggleYes;
SKSpriteNode *fbToggleNo;
SKLabelNode *story1;
SKLabelNode *story2;
SKLabelNode *story3;
SKLabelNode *chance_name;
SKLabelNode *max_name;
SKLabelNode *bryce_name;
SKLabelNode *chance_role;
SKLabelNode *max_role;
SKLabelNode *bryce_role;
SKLabelNode *chance_handle;
SKLabelNode *max_handle;
SKLabelNode *bryce_handle;
SKTexture *bg_texture;
SKTexture *gamecenter_texture;
SKTexture *wbox_texture;
SKTexture *max_texture;
SKTexture *chance_texture;
SKTexture *bryce_texture;
SKTexture *home_texture;
SKTexture *play_texture;
SKTexture *fb_texture;
SKTexture *toggle_yes_texture;
SKTexture *toggle_no_texture;
}
#end
#implementation SettingsSKScene
-(id) initWithSize:(CGSize)size{
if(self = [super initWithSize:size]){
masterOverlay = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:self.frame.size];
masterOverlay.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addChild:masterOverlay];
bg_texture = [SKTexture textureWithImageNamed:#"settings_bg"];
wbox_texture = [SKTexture textureWithImageNamed:#"white_rect_bg"];
gamecenter_texture = [SKTexture textureWithImageNamed:#"gc"];
max_texture = [SKTexture textureWithImageNamed:#"max"];
chance_texture = [SKTexture textureWithImageNamed:#"chance"];
bryce_texture = [SKTexture textureWithImageNamed:#"bryce"];
home_texture = [SKTexture textureWithImageNamed:#"home_light"];
play_texture = [SKTexture textureWithImageNamed:#"play_light"];
fb_texture = [SKTexture textureWithImageNamed:#"fb_light"];
toggle_yes_texture = [SKTexture textureWithImageNamed:#"toggle_yes"];
toggle_no_texture = [SKTexture textureWithImageNamed:#"toggle_no"];
NSArray *to_preload = #[bg_texture, wbox_texture, gamecenter_texture, max_texture, chance_texture, bryce_texture, home_texture, play_texture, fb_texture, toggle_yes_texture, toggle_no_texture];
[SKTexture preloadTextures:to_preload withCompletionHandler:^{
[self fadeRemove:masterOverlay];
[self initialize];
}];
}
return self;
}
-(void) fadeRemove: (SKNode *) node{
double duration = arc4random() % 10;
duration *= 0.05;
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.1+duration];
SKAction *remove = [SKAction runBlock:^{
[node removeFromParent];
}];
[node runAction:[SKAction sequence:#[fadeOut, remove]]];
}
-(void) initialize{
bg = [SKSpriteNode spriteNodeWithTexture:bg_texture];
bg.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
bg.zPosition = 0;
wbox = [SKSpriteNode spriteNodeWithTexture:wbox_texture];
wbox.position = CGPointMake(self.frame.size.width/2, 70);
wbox.alpha = 0.8;
wbox.zPosition = 2;
gamecenter = [SKSpriteNode spriteNodeWithTexture:gamecenter_texture];
gamecenter.position = CGPointMake(self.frame.size.width/2 + 100, self.frame.size.height/2-2);
gamecenter.name = #"gc";
gamecenter.zPosition = 2;
fbButton = [SKSpriteNode spriteNodeWithTexture:fb_texture];
fbButton.size = CGSizeMake(36, 36);
fbButton.position = CGPointMake(self.frame.size.width/2 - 115, self.frame.size.height/2-2);
fbButton.name = #"fb";
fbButton.zPosition = 2;
fbToggleNo = [SKSpriteNode spriteNodeWithTexture:toggle_no_texture];
fbToggleNo.position = CGPointMake(self.frame.size.width/2 - 50, self.frame.size.height/2-2);
fbToggleNo.size = CGSizeMake(fbToggleNo.size.width/3, fbToggleNo.size.height/3);
fbToggleNo.name = #"fbno";
fbToggleNo.zPosition = 2;
fbToggleYes = [SKSpriteNode spriteNodeWithTexture:toggle_yes_texture];
fbToggleYes.position = CGPointMake(self.frame.size.width/2 - 70, self.frame.size.height/2-2);
fbToggleYes.name = #"fbyes";
fbToggleYes.zPosition = 2;
int hpBuffer = 40;
int hpZ = 2;
home = [SKSpriteNode spriteNodeWithTexture:home_texture];
home.position = CGPointMake(hpBuffer, self.frame.size.height - hpBuffer);
home.zPosition = hpZ;
home.name = #"home";
play = [SKSpriteNode spriteNodeWithTexture:play_texture];
play.position = CGPointMake(self.frame.size.width - hpBuffer, self.frame.size.height - hpBuffer);
play.zPosition = hpZ;
play.name = #"play";
[self addChild:bg];
[self addChild:wbox];
[self addChild:gamecenter];
[self addChild:home];
[self addChild:play];
[self addChild:fbButton];
[self addChild:fbToggleNo];
[self addCredits];
[self addStory];
}
-(void) addCredits{
/* images */
int cmbZ = wbox.zPosition + 1;
int cmbY = wbox.position.y;
int cmbXUnit = 132;
int cmbX = self.frame.size.width/2 - (3*cmbXUnit)/2 + 20;
int cmbWidth = 40;
chance = [SKSpriteNode spriteNodeWithTexture:chance_texture];
max = [SKSpriteNode spriteNodeWithTexture:max_texture];
bryce = [SKSpriteNode spriteNodeWithTexture:bryce_texture];
chance.zPosition = cmbZ;
max.zPosition = cmbZ;
bryce.zPosition = cmbZ;
chance.position = CGPointMake(cmbX+cmbXUnit*0, cmbY+3);
max.position = CGPointMake(cmbX+cmbXUnit*1 + 10, cmbY);
bryce.position = CGPointMake(cmbX+cmbXUnit*2 + 10, cmbY+5);
chance.size = CGSizeMake(cmbWidth, (chance.size.height/chance.size.width)*cmbWidth);
max.size = CGSizeMake(cmbWidth, (max.size.height/max.size.width)*cmbWidth);
bryce.size = CGSizeMake(cmbWidth, (bryce.size.height/bryce.size.width)*cmbWidth);
[self addChild:chance];
[self addChild:max];
[self addChild:bryce];
/* names */
int cmb_nameXUnit = 30;
int cmb_nameY = wbox.position.y - 10;
int cmb_nameFontSize = 18;
chance_name = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
max_name = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
bryce_name = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
chance_name.text = #"Chance Daniel";
max_name.text = #"Max Hudson";
bryce_name.text = #"Bryce Daniel";
chance_name.fontColor = [SKColor blackColor];
max_name.fontColor = [SKColor blackColor];
bryce_name.fontColor = [SKColor blackColor];
chance_name.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
max_name.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
bryce_name.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
chance_name.position = CGPointMake(chance.position.x + cmb_nameXUnit, cmb_nameY);
max_name.position = CGPointMake(max.position.x + cmb_nameXUnit, cmb_nameY);
bryce_name.position = CGPointMake(bryce.position.x + cmb_nameXUnit, cmb_nameY);
chance_name.fontSize = cmb_nameFontSize;
max_name.fontSize = cmb_nameFontSize;
bryce_name.fontSize = cmb_nameFontSize;
chance_name.zPosition = cmbZ;
max_name.zPosition = cmbZ;
bryce_name.zPosition = cmbZ;
[self addChild:chance_name];
[self addChild:max_name];
[self addChild:bryce_name];
/* roles */
int cmb_roleY = wbox.position.y - 25;
int cmb_roleFontSize = 11;
chance_role = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
max_role = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
bryce_role = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
chance_role.text = #"Programmer";
max_role.text = #"Programmer";
bryce_role.text = #"Graphic Designer";
chance_role.fontColor = [SKColor darkGrayColor];
max_role.fontColor = [SKColor darkGrayColor];
bryce_role.fontColor = [SKColor darkGrayColor];
chance_role.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
max_role.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
bryce_role.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
chance_role.position = CGPointMake(chance.position.x + cmb_nameXUnit + 19, cmb_roleY);
max_role.position = CGPointMake(max.position.x + cmb_nameXUnit + 12, cmb_roleY);
bryce_role.position = CGPointMake(bryce.position.x + cmb_nameXUnit + 6, cmb_roleY);
chance_role.fontSize = cmb_roleFontSize;
max_role.fontSize = cmb_roleFontSize;
bryce_role.fontSize = cmb_roleFontSize;
chance_role.zPosition = cmbZ;
max_role.zPosition = cmbZ;
bryce_role.zPosition = cmbZ;
[self addChild:chance_role];
[self addChild:max_role];
[self addChild:bryce_role];
/* twitter handles */
int cmb_handY = wbox.position.y - 40;
int cmb_handFontSize = 10;
chance_handle = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
max_handle = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
bryce_handle = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
chance_handle.text = #"#ChanceOfThat";
max_handle.text = #"#max_hud";
bryce_handle.text = #"#BryceOfLife";
chance_handle.fontColor = [SKColor darkGrayColor];
max_handle.fontColor = [SKColor darkGrayColor];
bryce_handle.fontColor = [SKColor darkGrayColor];
chance_handle.position = CGPointMake(chance.position.x, cmb_handY);
max_handle.position = CGPointMake(max.position.x, cmb_handY);
bryce_handle.position = CGPointMake(bryce.position.x, cmb_handY);
chance_handle.fontSize = cmb_handFontSize;
max_handle.fontSize = cmb_handFontSize;
bryce_handle.fontSize = cmb_handFontSize;
chance_handle.zPosition = cmbZ;
max_handle.zPosition = cmbZ;
bryce_handle.zPosition = cmbZ;
[self addChild:chance_handle];
[self addChild:max_handle];
[self addChild:bryce_handle];
/* touchzones */
CGSize cmdL_size = CGSizeMake(120, 70);
SKColor *cmdL_color = [SKColor clearColor];
int cmdLZ = 3;
int cmdLX = cmbX + 40;
chance_link = [SKSpriteNode spriteNodeWithColor:cmdL_color size:cmdL_size];
max_link = [SKSpriteNode spriteNodeWithColor:cmdL_color size:cmdL_size];
bryce_link = [SKSpriteNode spriteNodeWithColor:cmdL_color size:cmdL_size];
chance_link.position = CGPointMake(cmdLX+cmbXUnit*0, cmbY);
max_link.position = CGPointMake(cmdLX+cmbXUnit*1 + 10, cmbY);
bryce_link.position = CGPointMake(cmdLX+cmbXUnit*2 + 10, cmbY);
chance_link.zPosition = cmdLZ;
max_link.zPosition = cmdLZ;
bryce_link.zPosition = cmdLZ;
chance_link.name = #"c_handle";
max_link.name = #"m_handle";
bryce_link.name = #"b_handle";
[self addChild:chance_link];
[self addChild:max_link];
[self addChild:bryce_link];
}
-(void) addStory{
int stX = self.frame.size.width/2;
int stZ = 2;
SKColor *stColor = [SKColor whiteColor];
story1 = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
story2 = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
story3 = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
story1.text = #"Gon";
story2.text = #"Help Gon-Gon and his friends get";
story3.text = #"Back to the time they came from!";
story1.fontColor = stColor;
story2.fontColor = stColor;
story3.fontColor = stColor;
story1.zPosition = stZ;
story2.zPosition = stZ;
story3.zPosition = stZ;
story1.position = CGPointMake(stX, self.frame.size.height - 55);
story1.fontSize = 50;
story2.position = CGPointMake(stX, self.frame.size.height - 95);
story2.fontSize = 30;
story3.position = CGPointMake(stX, self.frame.size.height - 120);
story3.fontSize = 20;
[self addChild:story1];
[self addChild:story2];
[self addChild:story3];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touchUI = [touches anyObject];
CGPoint touchPoint = [touchUI locationInNode:self];
SKNode *touchNode = [self nodeAtPoint:touchPoint];
if([touchNode.name isEqualToString:#"home"]){
StartViewController *svc = [[StartViewController alloc] initWithSize:self.size];
SKTransition *fade = [SKTransition fadeWithColor :[SKColor blackColor] duration:0.4];
[self.view presentScene:svc transition:fade];
}
if([touchNode.name isEqualToString:#"play"]){
gameViewController *gvc = [[gameViewController alloc] initWithSize:self.size];
SKTransition *fade = [SKTransition fadeWithColor :[SKColor blackColor] duration:0.4];
[self.view presentScene:gvc transition:fade];
}
if([touchNode.name isEqualToString:#"gc"]){
NSDictionary * dict = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithBool:1], #"showGC", nil];
[[NSNotificationCenter defaultCenter]postNotificationName:#"kNotificationUpdateBannerView" object:self userInfo:dict];
}
if([touchNode.name isEqualToString:#"c_handle"]){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=ChanceOfThat"]];
int worked = [[UIApplication sharedApplication] openURL:urlApp];
if(!worked){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"https://twitter.com/#!/ChanceOfThat"]];
[[UIApplication sharedApplication] openURL:urlApp];
}
}
if([touchNode.name isEqualToString:#"m_handle"]){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=max_hud"]];
int worked = [[UIApplication sharedApplication] openURL:urlApp];
if(!worked){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"https://twitter.com/#!/max_hud"]];
[[UIApplication sharedApplication] openURL:urlApp];
}
}
if([touchNode.name isEqualToString:#"b_handle"]){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=BryceOfLife"]];
int worked = [[UIApplication sharedApplication] openURL:urlApp];
if(!worked){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"https://twitter.com/#!/BryceOfLife"]];
[[UIApplication sharedApplication] openURL:urlApp];
}
}
}
#end
A similar problem was faced by the person who asked this question.
When asked whether he was able to solve it, they said:
Yes, I did, there wasn't anything I could do about it from the scene
or Sprite Kit for that matter, I simply needed to remove the scene and
the view containing it completely from the parent view, cut all its
bonds to the other parts of the system, in order for the memory to be
deallocated as well.
You should use separate views for each of your scene and transition between these views. You can follow these steps to make it look natural:
1 - At the point you want to transition from one scene to the other, take a snapshot of the scene using the following code:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then, add this image as a subview of the SKView of the current scene, and remove the scene.
2 - Initialise the new scene on another view, and transition between the two views using UIView animation.
3 - Remove the first view from it's superview.
I am just starting out with SpriteKit in Swift and I had the same problem: My intro scene was playing some music that I wanted to stop after transitioning to the main menu scene, and I figured the deinit would be a good place to put the AVAudioPlayer.stop() call in, but deinit was never being called.
After looking around I learned that it may be because of some strong references to the scene, so in my GameViewController:UIViewController subclass I changed this code:
let intro = IntroScene(size: skView.bounds.size)
skView.presentScene(intro)
to
skView.presentScene(IntroScene(size: skView.bounds.size))
and in the intro scene, that I wanted to be deallocated, I changed
let mainMenu = MainMenuScene(size: self.size)
let crossFade = SKTransition.crossFadeWithDuration(1)
self.scene.view.presentScene(mainMenu, transition: crossFade)
to
self.scene.view.presentScene(MainMenuScene(size: self.size),
transition: SKTransition.crossFadeWithDuration(1))
and it worked! After the transition was complete the deinit method got called.
I assume that the outgoing scene was not being deinitialized because there were variables holding references to it.
December 2019/Swift 5
Update:
My layout:
I have a single view controller that contains 2 SKViews which each of them have their own unique SKScene that are presented at the same time. One SKView & its SKScene is the main overworld where the player character is rendered, controlled, NPC's rendered, camera tracking, the whole shebang etc., & the other SKView & its SKScene display the mini map of the overworld.
You can imagine there are also quite a number of SKSpriteNode's & lot of them ALWAYS have some kind of SKAction/animation running non-stop (swaying trees for instance). My SKScenes even contain their own arrays pointing at specific groups of SKSpriteNodes, such as, character nodes, building nodes, tree nodes. This is for quick access & convenience purposes.
Plus, I have a few singletons that either contain an array of SKTextures, or character models, etc.. They are kept around as an optimization for quick data access rather than reading from disc/accessing storage every time I need something.
There are even UIKit elements used for the game UI inside the view controller.
Many other objects, such as, my models that contain data on characters, buildings, the entire game session all have some kind of delegates pointing at someone. On top of all of this the codebase is massive.
After observing memory in the debug session I found out the sure-fire way to make sure nothing is retained is to absolutely make sure the following:
Memory handling:
Pass in nil for scene presentation & set that skview's property/pointer to nil as well
Remove all view controller primary view subviews
Absolutely set every delegate you have to nil !!!!!!!!!!!!!!
Remove all observers IF you happen to have any
Anywhere you have an array/dictionary or pointer to some kind of object YOU created, set it to nil/empty it
CRITICAL: put all of the above into a single function/method & make sure to call it right before changing view controllers!
*NOTE: IF you have 1 view controller for the entire app (congrats on squeezing everything - youz overlord squeezer), then do NOT nil everything & use caution. BUT the previous scene still needs to be set to nil during presentation.
So something like this if you're jumping between view controllers:
/// Remove all pointers to any data, nodes & views.
fileprivate func cleanUp() {
guard self.skView != nil else { return }
// Session was passing data updates to this view controller; time to nil it
self.gameSessionModel.delegate = nil
self.skView.presentScene(nil)
self.skViewMiniMap.presentScene(nil)
self.skView = nil
self.skViewMiniMap = nil
for subview in self.view.subviews {
subview.removeFromSuperview()
}
}
/// Take the user back to the main menu module/view controller.
fileprivate func handleMenuButton() {
// First, clean up everything
self.cleanUp()
// Then go to the other view controller
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainViewController")
self.show(view, sender: .none)
}
Call handleMenuButton() function or whatever function you use to present another view controller, but make sure the cleanUp() function is called within it!
*NOTE: Classes such as 'gameSessionModel' are entirely my own custom classes. Xcode will throw an error on such things (unless you magically have the same one...) so delete such things. They are only used as example code in case you have delegates pointing at the current view controller.
If you're only presenting different SKScene's with a single SKView, then your cleanUp() function can end up being "lighter" as such:
/// Remove all pointers to any data, nodes & views.
fileprivate func cleanUp() {
self.skView.presentScene(nil)
// Previous scene was set to nil, its deinit called. Now onto the new scene:
let gameScene = self.setupGameScene(id: "perhapsYouUseStringIDsToDisntiguishBetweenScenes?")
self.skView.presentScene(gameScene)
}
*NOTE: Do NOT forget to use the SKScene's deinit method to remove anything you won't need. It's a practice I use all the time for all my classes to untangle anything I might have missed.
There's nothing wrong with SKScene or SKView, as long as I can see. Make sure the scene instance is not strongly reference anywhere else, especially inside a block. Blocks are highly probable to be ignored.
More about weak reference inside a block: https://stackoverflow.com/a/17105368/571489
As far as I see, you do have a block strongly referencing the scene instance:
[SKTexture preloadTextures:to_preload withCompletionHandler:^{
[self fadeRemove:masterOverlay];
[self initialize];
}];
EDIT: My previous idea about an NSTimer was irrelevant
To make sure this is an issue isolated to this scene, override the dealloc methods of all scenes you might have (including this one) like this:
-(void)dealloc {
NSLog(#"Dealloc <scene name>");
}
Look at your other scene transitions, see if they deallocate properly. Find the differences between these scenes. This will help you see if it's an isolated issue or a bigger problem. Once you have the problem fixed be sure to comment out or remove the dealloc method as it is overriding the one that actually deallocates the memory. Hopefully this helps!

Endless Scrolling Background in SpriteKit

I am attempting to make a side scrolling game using Apple's SpriteKit. When wanting to make a endless scrolling background I came across this answer.
After implementing the solution it does appear to work although it drops my FPS significantly. This is probably due to the fact that the images positions are being recalculated on every frame.
I feel like it would be much better if I could use one or more SKAction calls to take care of this animation for me but I'm not certain how to implement it.
Thoughts?
The code I have so far in my scene class (this only animates the background across the screen once though)
- (void)addBackgroundTileAtPoint:(CGPoint)point {
SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:#"background"];
bg.anchorPoint = CGPointZero;
bg.position = point;
bg.name = #"background";
bg.zPosition = -99;
[self addChild:bg];
SKAction *sequence = [SKAction sequence:#[
[SKAction moveByX:-(bg.size.width * 2) y:0 duration:10],
[SKAction removeFromParent]
]];
[bg runAction: sequence];
}
I did a small component called SKScrollingNode for that particular need in my last open source project : SprityBird.
FPS was not an issue even with 3 or 4 layers (for parallax), but you may need to try it yourself.
To use it you just have to add it like any other node and giving it a scrollingSpeed likeso :
back = [SKScrollingNode scrollingNodeWithImageNamed:#"back" inContainerWidth:WIDTH(self)];
[back setScrollingSpeed:BACK_SCROLLING_SPEED];
[self addChild:back];
SKScrollingNode.h
#interface SKScrollingNode : SKSpriteNode
#property (nonatomic) CGFloat scrollingSpeed;
+ (id) scrollingNodeWithImageNamed:(NSString *)name inContainerWidth:(float) width;
- (void) update:(NSTimeInterval)currentTime;
#end
SKScrollingNode.m
#implementation SKScrollingNode
+ (id) scrollingNodeWithImageNamed:(NSString *)name inContainerWidth:(float) width
{
UIImage * image = [UIImage imageNamed:name];
SKScrollingNode * realNode = [SKScrollingNode spriteNodeWithColor:[UIColor clearColor] size:CGSizeMake(width, image.size.height)];
realNode.scrollingSpeed = 1;
float total = 0;
while(total<(width + image.size.width)){
SKSpriteNode * child = [SKSpriteNode spriteNodeWithImageNamed:name ];
[child setAnchorPoint:CGPointZero];
[child setPosition:CGPointMake(total, 0)];
[realNode addChild:child];
total+=child.size.width;
}
return realNode;
}
- (void) update:(NSTimeInterval)currentTime
{
[self.children enumerateObjectsUsingBlock:^(SKSpriteNode * child, NSUInteger idx, BOOL *stop) {
child.position = CGPointMake(child.position.x-self.scrollingSpeed, child.position.y);
if (child.position.x <= -child.size.width){
float delta = child.position.x+child.size.width;
child.position = CGPointMake(child.size.width*(self.children.count-1)+delta, child.position.y);
}
}];
}
#end
I've been working on a library for an infinite tile scroller, you can easily use it to create a scrolling background. Take a look at it:
RPTileScroller
I made an effort to make it the most efficient possible, but I am not a game developer. I tried it with my iPhone 5 with random colors tiles of 10x10 pixels, and is running on a solid 60 fps.

Resources