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?
Related
I am having a difficult time getting a Swipe Gesture Recognizer to work on my app. Here is the Hierarchy of it all.
App's root view is a UINavigationController which has class ViewController as visible for the first view seen. I have a UIButton which will fire a movie that loops until I tap it twice which will have the navigation controller push a new ViewController that I have made called PUPPETS1 onto the screen. This VC has its own xib. The xib has a UIImageView. What I want to have happen is to start playing a movie once I swipe up on the screen, but that never happens, and the console never shows my NSLog from the 2nd VC's method.
- (void)loopVideo {
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"warpspeed" withExtension:#"mov"];
UIView *patternView = [[UIView alloc] initWithFrame:self.view.bounds];
patternView.backgroundColor = [UIColor blackColor];
[self.moviePlayer2.backgroundView addSubview:patternView];
self.moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayer2 setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer2.controlStyle = MPMovieControlStyleNone;
self.moviePlayer2.scalingMode = MPMovieScalingModeAspectFill;
self.moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayer2 setAllowsAirPlay:YES];
self.moviePlayer2.view.frame = self.view.frame;
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(puppetOne)];
tapGesture.numberOfTapsRequired = 2;
tapGesture.numberOfTouchesRequired = 1;
UIView *aView = [[UIView alloc] initWithFrame:self.moviePlayer2.backgroundView.bounds];
[aView addGestureRecognizer:tapGesture];
[self.view.window addSubview:aView];
[self.view addSubview:self.moviePlayer2.view];
self.moviePlayer2.repeatMode = MPMovieRepeatModeOne;
[self.moviePlayer2 play];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
In the 2nd VC, the PUPPETS1 one:
- (void)viewDidLoad {
[super viewWillAppear:YES];
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(playPuppets)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
UIView *aView2 = [[UIView alloc] initWithFrame:self.view.bounds];
[aView2 addGestureRecognizer:swipeRec];
[self.view addSubview:aView2];
// Do any additional setup after loading the view from its nib.
}
-(void)playPuppets {
NSLog(#"PLAYING");
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"SundayPuppets" withExtension:#"m4v"];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
The frame of the view to which you're attaching the gr depends on it's parent view's bounds, and those aren't initialized yet in viewDidLoad.
Move the setup to after layout is complete (and tweak it to run just once, on the first layout change), i.e.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
UIView *swipeView = [self.view viewWithTag:999];
// only do this if we haven't done it already
if (!swipeView) {
// now that self.view.bounds is initialized...
swipeView = [[UIView alloc] initWithFrame:self.view.bounds];
swipeView.tag = 999;
// the rest of your OP setup code is fine, and goes here
UISwipeGestureRecognizer * swipeRec = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(playPuppets)];
swipeRec.direction = UISwipeGestureRecognizerDirectionUp;
[swipeView addGestureRecognizer:swipeRec];
[self.view addSubview:swipeView];
NSLog(#"%#", swipeView);
}
}
EDIT The code above attached the gesture recognizing view correctly, providing a fix in the second view controller. It turns out that the other problem was the previous view controller was not properly removing the (deprecated) MPMoviePlayer, resulting in touches not functioning on the pushed vc. The entire reworked ViewController.m can be found in the chat linked below, but the fix for the touches issue was here...
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.moviePlayer stop];
[self.moviePlayer.view removeFromSuperview];
self.moviePlayer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
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]];
}
I'm trying to integrate "admobs" into an scene. I'm trying to do that by doing it viewWillLayoutSubviews in the viewController. I'm trying it out in a blank scene with 0 nodes, so there is nothing that can cause problems in the scene.
The problem is that the ad is not showing in my scene. I'm getting following log message:
To get test ads on this device, call: request.testDevices = #[ GAD_SIMULATOR_ID ];
but i am creating the request in createRequest method.
This is my code:
-(void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
if (!skView.scene) {
self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape];
self.bannerView.adUnitID = #"ca-app-pub-AdId";
self.bannerView.rootViewController = self;
self.bannerView.delegate = self;
self.bannerView.center = CGPointMake(skView.bounds.size.width / 2, skView.bounds.size.height - (bannerView_.frame.size.height / 2));
[self.view addSubview:self.bannerView];
[self.bannerView loadRequest:[GADRequest request]];
// Create and configure the scene.
SKScene * scene = [Menu sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:scene];
}
}
-(GADRequest *)createRequest {
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:GAD_SIMULATOR_ID, nil];
return request;
}
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
NSLog(#"Ad Reveived");
[UIView animateWithDuration:1.0 animations:^{
adView.frame = CGRectMake(0.0, 0.0, adView.frame.size.width, adView.frame.size.height);
}];
}
You need to replace the following code;
[self.bannerView loadRequest:[GADRequest request]];
with:
GADRequest *r = [[GADRequest alloc] init];
r.testing = YES;
[self.bannerView loadRequest:r];
Hope this works for you.
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;
}
}
I am interested to write my custom view, so I created the following xib file:
This is the definition file:
- (void)_baseInit {
NSLog(#"Unseen View loaded");
[self addSubview:[self activityIndicator]];
[self activityIndicator].alpha = 1.0;
[self activityIndicator].frame = CGRectMake(round(([self imageView].frame.size.width - 25) / 2),
round(([self imageView].frame.size.height - 25) / 2), 25, 25);
[self activityIndicator].hidesWhenStopped = YES;
[self showIndicator];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[self imageView].userInteractionEnabled = YES;
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[[self imageView] addGestureRecognizer:panRecognizer];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self _baseInit];
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder
{
if ((self = [super initWithCoder:coder])) {
[self _baseInit];
}
return self;
}
I tried to hook it up in my story board:
And I have my MainViewController call this during viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
self.unseenView = [[[NSBundle mainBundle] loadNibNamed:#"UnseenView" owner:self options:nil] objectAtIndex:0];
self.unseenView.delegate = self;
Unfortunately, nothing is showing up in my simulator literally nothing, not even the text labels.
However I am seeing the following log messages:
2013-02-20 17:37:58.929 Giordano.iPhone[66857:c07] Unseen View loaded
2013-02-20 17:37:58.934 Giordano.iPhone[66857:c07] Unseen View loaded
What am I doing wrong?
It looks like you are preparing your view correctly, but you are not adding it to the view hierarchy. in the viewDidLoad code you need to add this line:
[self.view addSubview:unseenView];