I am a beginner developing an iOS game using sprite kit. I would like to have 'pop-up' menu's that display objects (skspritenodes) such as daily rewards or settings/pause menu. The settings pop-up for example should be able to be accessed on the main home screen or during the game. Currently I have one view controller and the home scene and play scene are two different SKScenes. For reusability (and a clean project, as well as learning) I would like to have these pop-ups be their own class, including handling touches. Currently, I have a class for the settings pop-up that returns an SKNode, and this sknode contains several skspritenodes (the buttons/images that correspond to a settings pop up including enabling/disabling sound etc). However, I have to duplicate the touches code in both my home scene and my play scene to interact with this. (I currently have, in each skscene's touchesbegan method, "if nodeSettings !=nil", check the name of the skspritenode that corresponds to the touch-location, then call a method in the setting-pop-up class passing the name of the skspritenode clicked to handle interactions with the pop-up).
For my own knowledge and also to solve this problem, I would like to use a class that can handle the touch logic on its own (so in my play or home skscene, the only thing I do is create the pop-up. Any interaction, including dismissal, I would like to have handled in the class. No using the skscene's touches methods). I have found one 'solution' to this:
LTPopUpReward *test = [[LTPopUpReward alloc] init];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:test];
CGFloat fltHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat fltWidth = [UIScreen mainScreen].bounds.size.width;
popover.popoverContentSize = CGSizeMake(fltWidth/2, fltHeight/2); //your custom size.
CGRect rectPopUpReward = CGRectMake(500, 500, fltWidth/2, fltHeight/2);
[popover presentPopoverFromRect:rectPopUpReward inView:self.view permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES];
and I like that you can dismiss the UIPopoverController by clicking outside of it, but this UIPopoverController only works on the iPad and doesn't work on the iPhone. I've seen this UIPopoverController for iphone not working? but was wondering if there is another way to solve my problem that Apple won't possibly disapprove of? For example, could I create a UIViewController, resize it and position it (i don't know how to do this) to simulate the pop-up?
Summarizing: my goal is to have a pop-up appear, but have all of its code (including touch) self contained within one class, and the objects on the pop-up are skspritenodes.
answering my own question: The solution I used to solve this problem is to use different SKViews. One SKView presents the main scene and another SKView presents the pop-up scene. You can adjust the size of the second SKView and even make its background transparent. Interacting with objects in this view will correctly call the touches methods in the class of the scene presented - That is this pop-up is dealt with when being created, and there is no other code dealing with it on the view-controller or main-scene's logic (all code, including creating objects and handling touch) are in the second skscene's code. The method below is called when I do "something" to call the 'pop-up'.
-(void)test4
{
skViewPopUp=nil;
scenePopUpReward=nil;
NSLog(#"test4 successfully fired");
CGFloat fltHeight = [UIScreen mainScreen].bounds.size.height;
CGFloat fltWidth = [UIScreen mainScreen].bounds.size.width;
CGRect rectPopUpReward = CGRectMake(0, 0, fltWidth/2, fltHeight/2);
skViewPopUp = [[SKView alloc] initWithFrame:rectPopUpReward];
[self.view addSubview:skViewPopUp];
skViewPopUp.allowsTransparency = YES;
scenePopUpReward = [[LTSceneStoreMain alloc] initWithSize:CGSizeMake(fltWidth/2, fltHeight/2)];
scenePopUpReward.backgroundColor = [UIColor clearColor];
[skViewPopUp presentScene:scenePopUpReward];
}
Related
I've done some searching but haven't found anything directly addressing my issue: I have an SKScene with several SKNodes each with SKSpriteNode objects for my game, and I am using a background UIImageView (there are some things I need to do with background that cannot be reasonable done with sprite kit - to my knowledge at least); the problem I'm having is that the UIImageView appears above everything and I can't see my game objects.
In my view controller (.m file) I have the following code to call my scene
#implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
SKView * skView = (SKView *)self.view;
SKScene * scene = [GameScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;
[skView presentScene:scene];
}
In the GameScene above, I have several SKNodes where all my game objects are children of (for example nodeHDU, nodePlay, nodePauseMenu...), and I am using a UIImageView as my background (I am switching between background scenes over time and am using some nice transitions such as UIViewAnimationOptionTransitionCrossDissolve; couldn't accomplish this with a SKSpriteNode as background without using multiple SKSpriteNodes and an intricate array of SKActions so I"m using UIImageView)
UIView *backgrounds = [[UIView alloc] init];
[self.view insertSubview:backgrounds belowSubview:self.view];
UIImageView *imageBackground = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)];
[backgrounds addSubview:imageBackground];
[backgrounds sendSubviewToBack:imageBackground];
[imageBackground setImage:[UIImage imageNamed:#"1-A.png"]];
imageBackground.alpha=1.0;
However, the ImageBackground above is the only thing I see when running the app in Xcode. The other objects from my game (children of other nodes) exist, and various NSLog statements are getting called, but they seem to appear behind the ImageBackground I have. The code above "belowSubview" and "sendSubviewToBack" don't help.
So How can I send the imageBackground to actually be the background?
Your problem is here that you are directly setting SKView as a controller view. To overcome from this issue don't make self.view a type of SKView. Do the following step in your storyboard or Xib :
Add an UIImageView on the controller's view first. Set the image as well.
Then add a SKView over the UIImageViewand set UIClearColor to the background colour.
So the complete hierarchy is like UIViewController->UIView->UIImageView->SKView
I have an UIScrollView with UIViews being added by lazy-loading algorithm. The UIViews contain previews of previous and next image, so at some point the actual views have to be used more than once (e.g. preview of the 2nd picture is NEXT now, but after 2 page scrolls it is PREVIOUS).
All the UIViews are stored in NSMutableArray and are added dynamically. I have the code, which checks, whether particular UIView is already added to the UIScrollView. If not, the code checks, is there needed UIView in NSMutableArray and if not, creates a new one...
The problem occurs when I try to get an UIView from NSMutableArray, change it's position on the screen and add to UIScrollView again. It just changes it's position and I see white squares where some UIViews have to be.
Is it possible to have 2 UIViews with pointer referred to one memory location or UIImage but with only one different parameter (position)? Is it good idea to copy already created UIView to NSMutable array twice to use with different position? (Win: creation process takes place only once, loss: amount of memory required is doubled).
From my point of view, I havent fount the good solution, but this worked for me - I just implemented copy method in UIView subclass, something like this:
- (ExhibitPreview *)copy {
ExhibitPreview *exhibitPreview =[[ExhibitPreview alloc] initWithImage:self.image];
exhibitPreview.number.text = self.number.text;
exhibitPreview.author.text = self.author.text;
exhibitPreview.title.text = self.title.text;
exhibitPreview.contentMode = UIViewContentModeScaleAspectFill;
exhibitPreview.clipsToBounds = YES;
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGRect frame = CGRectMake(0.0f, 0.0f, screenWidth/2, PREVIEW_HEIGHT);
exhibitPreview.frame = frame;
return exhibitPreview;
}
The first view is view itself and the second I create is a copy.
Say I have two UIViews UIViewWithGravity and UIViewWithoutGravity. UIViewWithGravity is added to the gravity behavior:
[self.gravity addItem:UIViewWithGravity];
UIViewWithoutGravity is not. At this point, UIViewWithGravity falls off the screen, UIViewWithoutGravity stays put. So far, so good.
Now, I tie the two together using a UIAttachmentBehavior. Something like:
- (void)attachViews {
self.attachment = [[UIAttachmentBehavior alloc] initWithItem:[self.UIViewWithGravity viewWithTag:GRAVITY_TAG]
attachedToItem:[self.UIViewWithoutGravity viewWithTag:NO_GRAVITY_TAG]];
[self.animator addBehavior:self.attachViews];
}
UIViewWithGravity now drags UIViewWithoutGravity off the screen.
How can I attach these views together such that UIViewWithGravity swings freely from UIViewWithoutGravity, but does not cause UIViewWithoutGravity to be pulled down?
You need to attach the view without gravity to something that doesn't move (a fixed anchor).
I trying to create a game, and following Apple's advice I am using multiple scenes.
You could consider the game to be a text heavy point and click adventure.
Therein lies the problem. Lots of text, having done a bit of a search, it seems the recommend way, or rather the current only way to do this is with a UITextView thus:
[self.view addSubview: textView];
This is all well and good if you only have one SKScene, as that is the scene being displayed by the current view/SKView.
My problem is, that when I add the text to my scene's *view, which isn't the first scene the app loaded (its the third, or higher), the app jumps back to the first scene it loaded (which is my menu scene) and happily displays the required text.
Anybody got any idea why? I have menu scene transitioning to scene one, to scene two (in which I wish to display the test).
Please don't say I need a view per scene if I want to display more than a handful of words per scene, that just doesn't really make sense, but perhaps neither does my usage of SpriteKit.
I am still some what stunned there is no SK equivalent of the UITextView.
Anyway, any help, pointers would be great, thank you.
Ok, here are the relevant parts of the code.... I think.
Controller:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Configure the view.
SKView *skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
skView.showsDrawCount = YES;
// Create and configure the scene.
SKScene *scene = [[GTMainMenu alloc] initWithSize:skView.bounds.size];
// Present the scene.
[skView presentScene:scene];
}
where GTMainMenu is a subclass of SKScene, and has a button (orange box) to an "act" (A subclass of GTScene, itself a subclass of SKScene), this will cause a transition to the act, which has another button to the first scene.
Only you never make it to the scene, as the viewDidLoad returns you to the main menu.
This is the viewDidLoad of the scene, which will cause it to "jump" back to the main menu:
- (void)didMoveToView:(SKView *)view
{
[super didMoveToView:view];
if (!self.contentCreated) {
[self createSceneContents];
self.contentCreated = YES;
}
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(self.size.width/2, self.size.height/2+20, 200, 40)];
textView.center = self.view.center;
textView.textColor = [UIColor blackColor];
textView.font = [UIFont systemFontOfSize:17.0];
textView.backgroundColor = [UIColor whiteColor];
textView.text = #"Where am I?";
[self.view addSubview:textView];
}
There is a git repo available here.
This is a striped down version of my project, removing everything that is unrelated to the issue at hand.
If you will excuse the code, my day job is Java, and I am struggling with certain concepts in Objective C at the moment.
Oh and sorry I managed to include the usersettings :S
Your view controller's viewWillLayoutSubviews method is not safeguarded against repeated execution. This method will not just run at app launch but every time the view rotates and resizes.
Add a check before creating/presenting a scene in that method:
if(self.view.scene == nil) { /* present scene */ }
Have you looked into SKLabelNode? I used it extensively in my SpriteKit game. If you need your SKLabelNode to do anything fancy (physics, etc.), you can just add it to a parent SKSpriteNode.
One concern over a universal app I currently developing.
I just finishing with functionality development and need to add eye candy (animations).
Now wondering in a single iPhone or iPad app I would just play and set frame, bounds, to move a View, layer etc with hardcoded values.
Now how would you handled the different device frame and bounds?
Meaning you want to move a UIImageView from outside the parent View bounds and make it come to a position. Handling differently while testing what device we running at?
Thank you.
You could add a method in your AppDelegate,
- (BOOL) ISIPAD {
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}
And refer to that each time you are making animations. If you then have some variables in your class with the width of the screen and so, you only need to check for this in either the viewDidLoad, or your init method. Then you could use the dynamic variables to do your animations. To place a button in the middle of the screen, you could do:
[UIView animateWithDuration:1.0 animations:^{
UIButton *yourButton;
yourButton.frame = (CGRect){.origin = CGPointMake(screenwidth/2.0f-yourButton.frame.size.width/2.0f, screenheight/2.0f - yourButton.frame.size.height/2.0f), .size = yourButton.frame.size};
}];
Considering the screenvariables are set in your init or viewDidLoad method, this will put your button in the middle of your screen.