Using SpriteKit template, Swipe gesture lag between Scenes - ios

I'm building an app using Spritekit, I use UISwipeGestureRecognizer to swipe between scenes.
Now, my app is going to have a lot of scenes based on the same code, and the swipe gesture works well, except once I add a third or fourth scene I get a lot of swipe lag.
I'm not sure if it's because I'm using too many scenes or if it's because each scene uses a background image.
The swipe works but it suffers from a significant lag.
Am I the only one getting this issue? Has anyone found a work around for this kind of lag?
Any help would be appreciated.
Here's an example of the code used for 1 scene.
Thank you
#import "Ep1Slide1.h"
#import "Ep1Slide2.h"
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.backgroundColor = [SKColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
CGImageRef backgroundCGImage = [UIImage imageNamed:#"backgroundImage1"].CGImage;
SKTexture *backgroundTexture = [SKTexture textureWithCGImage:backgroundCGImage];
SKSpriteNode *someNode = [SKSpriteNode spriteNodeWithTexture:backgroundTexture];
someNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[someNode setScale:0.5f];
[self addChild:someNode];
}
return self;
}
- (void)didMoveToView:(SKView *)view{
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action: #selector(leftFlip:)];
[leftSwipe setDirection:UISwipeGestureRecognizerDirectionLeft];
[self.view addGestureRecognizer:leftSwipe];
}
- (void)leftFlip:(id)sender{
SKView * skView = (SKView *)self.view;
SKScene * scene = [Ep1Slide2 sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:1.2]];
}

Related

transition between two view controllers Sprite Kit

Good day!
I do have a problem with performSegueWithIdentifier: in my game.
There are two view controllers "MainMenu" and "GameViewContoller".
My game starts with MainMenu like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Configure the view.
SKView *skView = (SKView *)self.view;
skView.multipleTouchEnabled = NO;
// Create and configure the scene.
self.scene = [Menu sceneWithSize:skView.bounds.size];
self.scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:self.scene];
UILabel *startButton = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMidX(self.view.frame), CGRectGetMidY(self.view.frame), 100, 40)];
startButton.text = #"Start";
startButton.font = [UIFont fontWithName:#"out" size:20];
// startButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
startButton.textColor = [UIColor yellowColor];
startButton.userInteractionEnabled = YES;
// startButton.fontSize = 20;
// startButton.name = #"Start";
[self.view addSubview: startButton];
UITapGestureRecognizer *startTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(start)];
[startButton addGestureRecognizer: startTap];
// Do any additional setup after loading the view.
}
-(void) start{
UIViewController *vc = self.view.window.rootViewController;
[vc performSegueWithIdentifier:#"Game" sender:nil];
}
so my game is shutting down when [vc performSegueWithIdentifier:#"Game" sender:nil];
my segues configured correct, there is a segue name "Game". Game is running if is not in test mode from Xcode if so i have an error and second controller is not presented.
Any ideas?

SpriteKit problems with SKScene

I have performance problems using iOS Sprite kit.
I did the main scene of the game and every thing run nice and smoothly, then I inserted a splash windows in the beginning and when I change the scene to the main scene the game seems laggy with frame drop, but fps, cpu and memory parameters are Ok.
I also have a game over screen and the behaviour is the same, when the game return to the main scene looks very laggy.
I have tried with [self removeFromParents] and removing children, and nothing happens.
Code:
#implementation Splash
- (id) initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
SKSpriteNode *sprite= [SKSpriteNode spriteNodeWithImageNamed:#"main"];
sprite.position = CGPointMake(160,284);
[self addChild:sprite];
}
return self;
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Transition back to the Game
SKScene *myScene = [[MyScene alloc] initWithSize:self.size];
SKTransition *reveal = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:myScene transition:reveal];
[self removeAllChildren];
[self removeAllActions];
[self removeFromParent];
}
#end

FPS drop down when enabling iAd

My spriteKit game work fine at 60fps but when enabling iAd it drops to 15, sometimes 10 or even lower.. and it's a pain, when I die sometimes it takes a while to reset the scene.
I've ran the analyzer on x-code 6 and I get a lot of low importance warnings:
but also this message:
I've added iAd in the implementation of my viewController.m file like this:
#implementation XYZViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//AUDIO BACKground
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:#"spaceprueba160kbps" withExtension:#"mp3"];
self.backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
self.backgroundMusicPlayer.numberOfLoops = -1;
[self.backgroundMusicPlayer prepareToPlay];
[self.backgroundMusicPlayer play];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
//iAd
ADBannerView* adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
// adView.delegate = self;
[adView setFrame:CGRectMake(0, 0, 1024, 768)]; // set to your screen dimensions
[adView setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:adView];
// self.canDisplayBannerAds = YES;
// Create and configure the scene.
SKScene * scene = [XYZWorld1 sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
I know I should un-comment that line -> // self.canDisplayBannerAds = YES;
but it work fine like this and if I un-comment this line of code, I get a screen without iAdd and with my main camera looking a bit upper than the main screen.
My game don't run in this file but in an instance of SKScene.
So my question is if anybody know how I can get back my 60 fps while keeping iAd on my screen.. ?

Using UIKit's UIScrollView with SpriteKit

I read two questions here about that, both with the same answer. The suggested solution was to create the UIScrollView object, and then add it to the parent view of the SKScene in the target scene's didMoveToView:, removing it in willMoveFromView:.
And that is the target scene code I implemented:
#interface MQSSMakerScene ()
#property BOOL contentCreated;
#property MQSSMakerWorkspaceLayer *workspaceLayer;
#property MQSSMakerDockLayer *dockLayer;
#property UIScrollView *scrollView;
#end
#implementation MQSSMakerScene
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
CGSize layerSize = CGSizeMake(944, 140);
CGPoint layerPosition = CGPointMake(40, 768-(140+30));
CGRect viewFrame = CGRectMake(layerPosition.x, layerPosition.y, layerSize.width, layerSize.height);
_scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
_scrollView.contentSize = CGSizeMake(2000, 120);
_scrollView.scrollEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = YES;
_scrollView.backgroundColor = [UIColor brownColor];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
if(!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
[self.view addSubview:_scrollView]; // --> WHERE IT'S BEING ADDED
}
- (void)willMoveFromView:(SKView *)view
{
[super willMoveFromView:view];
[_scrollView removeFromSuperview]; // --> WHERE IT'S BEING REMOVED
}
- (void)createSceneContents
{
self.backgroundColor = [UIColor colorWithHue:.237 saturation:.56 brightness:.46 alpha:1];
self.scaleMode = SKSceneScaleModeAspectFill;
[self addChild:[self newMakerLayout]];
}
- (SKNode *)newMakerLayout
{
SKNode *mainLayout = [[SKNode alloc] init];
self.workspaceLayer = [[MQSSMakerWorkspaceLayer alloc] init];
self.dockLayer = [[MQSSMakerDockLayer alloc] init];
[mainLayout addChild:self.workspaceLayer];
[mainLayout addChild:self.dockLayer];
MQSSStoryObject *ob1 = [[MQSSStoryObject alloc] initWithImageNamed:#"maker-1.png"];
[self.workspaceLayer addChild:ob1];
return mainLayout;
}
#end
Please note that the user should go to this scene from the home scene when clicking on a button. The problem now is that once I add the line:
[self.view addSubview:_scrollView];
to add the UIScrollView object to this scene, the application no more goes to this scene and instead add the UIScrollView to the home scene I am transitioning from. It also doesn't remove it when transitioning to another scene. Once I comment out this line, everything works just as expected, clearly without presenting the UIScrollView.
I am stuck. Any help or advice is highly appreciated. Thanks!
I finally figured out what the problem was for me. I was using the -(void)willLayoutSubviews method of the viewController to ensure I had the correct bounds, but I had forgotten to see if my SKScene was already presented. That resulted in a new welcomeScene being presented each time the view was laid out on top of the old one.
-(void)willLayoutSubviews
{
if(!self.didPresentScene)
{
// Present scene here
....
self.didPresentScene = YES;
}
}

Collision Behavior not working

I am trying my hand at UIKit Dynamics and am trying to create a simple animation with a ball that bounces off of the screen boundaries.
As shown in the code below, when I add the gravity behavior the ball seems to go off the screen as expected. But when I add the collision behavior, nothing happens. The ball stays as is at the center of the screen where I have drawn it initially.
I also trie to add a push behavior with continuous push force option, but still the ball does not move.
#implementation TAMViewController
UIDynamicAnimator* animator;
UIGravityBehavior* gravity;
UICollisionBehavior* collision;
UIPushBehavior* push;
- (void)viewDidLoad
{
[super viewDidLoad];
TAMBouncyView *ballView = [[TAMBouncyView alloc] initWithFrame:CGRectMake(0,0,
self.view.frame.size.width,
self.view.frame.size.height)];
[self.view addSubview:ballView];
animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
gravity = [[UIGravityBehavior alloc] initWithItems:#[ballView]];
[animator addBehavior:gravity];
collision = [[UICollisionBehavior alloc] initWithItems:#[ballView]];
[collision setTranslatesReferenceBoundsIntoBoundary:YES];
[animator addBehavior:collision];
push = [[UIPushBehavior alloc] initWithItems:#[ballView]
mode:UIPushBehaviorModeContinuous];
[animator addBehavior:push];
// Do any additional setup after loading the view, typically from a nib.
}

Resources