How do I mix UIKit and SpriteKit? - ios

I am making a game in Swift. I have used both UIKit and SpriteKit, but never in the same app. I was wondering how to leverage the power of both of them (They are going to be a lot of menus). If you knew a tutorial I could use, or could tell me, I would be very appreciative. I also would like to know if there is a shortcut for this in xcode (e.g. storyboards)

I would highly recommend you to stick with SpriteKit and to create menus etc with SKNodes. I know that it is really seductive to use UIKit in a game for buttons etc. but you've got many possibilities right in SpriteKit which replaces UIKit-elements.
For example you can use an SKLabelNode instead of an UILabel. Also you've got many more possibilities in SpriteKit to make your menu 'smooth' (For example with SKTransition or SKAction).
Also an example for a menubutton would be:
//Button
var playButton = SKSpriteNode(imageNamed: "yourImage.png")
playButton.position = CGPointMake(300, 300)
playButton.name = "playButton"
addChild(playButton)
Then in your touchesBegan method you can handle that button touch.
Also one very important point to only use SpriteKit is, that you can port a iOS game to OSX within a few hours.

Related

Including a UIKit mini-game into a SpriteKit project

So I've made a quiz app in UIKit, with a question being displayed, four answers to pick from, etc. But since this project includes multiple mini-games, the rest of the project is written in SpriteKit. Is it worth it to rewrite the quiz app in SpriteKit, or is there some way I could include this UIKit mini-game into a SpriteKit project.
I've already tried to work with two classes like this:
public class GameScene: SKScene {
public var orientation: UIInterfaceOrientation!
let QuizScene = QuizScene()
override public func didMove(to view: SKView) {
fireQuizScene.setUpBushfireQuiz()
}
}
But it doesn't seem to work. Any help would be greatly appreciated. Thanks!
Stick with SpriteKit for games, and UIKit for the quiz.
Apple decided to develop SpriteKit because UIKit just does not have the proper tools to create a game.
Before Sprite Kit, people were on there own creating context windows out of open gl frame works, and it was a real hassle.
UIKit has nice features that keep your app responsive across all devices thanks to their constraints, which SpriteKit does not have.
Sprite Kit and UI kit integrate nicely, so do not fear with having your games in SpriteKit.
Just have your quiz in UIKit, and pop up an SKView for sprite kit when it is game time.

SpriteKit on AppleWatch - How to present a scene?

I made a game for iOS using SpriteKit.
Now I wanna make also the version for watchOS.
I added a new target and then these files appear to me:
If I delete the sks file and then try to present a swift scene as I did for the iOS game the scene appears empty... So the only thing i could do is leave the scene.sks file as it was.
Can someone kindly explain to me how can to present a new scene.swift file in the interfaceController.swift without using the sks file?
Here is the InterfaceController.swift file that presents the sks scene, but I wanna present my own swift scene.
Best Regards.
exchange the GameScene(filenamed:) with `YourScene(size:).
Your scenes are a subclass of SKView which have their primary initializers... here is the link to the one I showed:
https://developer.apple.com/reference/spritekit/skscene/1520435-init
So (filenamed:) is one initializer (for .sks) and (size:) is another initializer, for not having .sks. These initializers are the exact same kind of initializers you use for all of your own classes.
This just isn't readily apparent, because when you make your own GameScene: SKScene you don't specify initializers (because they are inherited from SKScene that you just subclassed from)
the video below also covers most of the necessary things for SK on AW.. it's timestamped to explain more of what you're doing:
https://youtu.be/REv-w2rBsng?t=1173
the info in the above video is virtually nowhere else that I could find, so I highly recommend you watch it from begin to end.
NOTE:
(last I checked)
Your vanilla SKScenes won't work on the watch, because they lack certain functions (like touchesBegan) because you are no longer using an SKView to present your scene. So, you must use gesture recognizers..
Here explains how to get around that stuff:
TouchEvents in watchOS 3 SpriteKit?

Can I use multiple animations in 1 view ios swift

I am trying to create an aquarium style app where the fish and bubbles move once the app is opened using swift and xcode 6.2. The code I am using only allows me to use it once. If I try to use it more the once the animations dont work correctly, they become jumpy and if I try it 3 or more times the animations disappear completely from the app. Does anybody know what code I could use to achieve what I am after or how to alter the code I already have to achieve this. The app is a single view with up to 8 animations. Here is the code I have been using with the (func performBubbles1) and (imgBubbles1) being changed accordingly to match what image view is going to be animated and with what animation.
timer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: Selector("performBubbles1"), userInfo: nil, repeats: true)
}
func performBubbles1() {
counter++
if (counter==15) {
counter = 1
}
imgBubbles1.image = UIImage(named: "bubbles\(counter).png")
I am new to coding so apologise if this makes no sense or if this has been answered. I have not been able to find the answer I am after.
Thankyou
The approach you're using was not designed to be used that way. You don't want to use NSTimer to essentially act as a render loop. It isn't accurate enough. Additionally, UIKit isn't really designed to swap images like you are at a high frame rate for a variety of different reasons.
What you're actually looking for is SpriteKit. It is designed for making games, but will fit your use case very VERY well. Here is a great SpriteKit tutorial to get you started.
Sprite Kit Swift Tutorial for Beginners

iOS dynamic object creation

I've worked with Xcode and iOS on a few personal projects and have always used non-object-oriented designs for everything... just because I've been doing mostly learning/experimenting with things.
Now I've been trying to implement some object oriented design into one game I've made previously. The idea is, I have a space ship that can shoot bullets. In the past I basically added the UIImageView to the storyboard and then connected it to the .h file and from there did things on it like move it around or whatever (using CGPointMake).
The idea now is to make a method (and a whole other class soon) that will create a UIImageView programmatically, allocate it, add it to the superview etc... I've got this working so far, easy stuff, but the next part is a bit harder. Where do I store this local variable "bullet"? I've been saving it to an NSMutableArray and doing the following:
(actually here are the methods that I have)
-(void)movement {
for (int i = 0; i < [array1 count]; i ++) {
UIImageView *a = [array1 objectAtIndex:i];
a.center = CGPointMake(a.center.x + 2, a.center.y);
if (a.center.x > 500) {
[array1 removeObjectAtIndex:i];
[a removeFromSuperview];
}
}
}
-(void)getBullet {
UIImageView *bullet = [[UIImageView alloc] initWithFrame:CGRectMake(ship.center.x + 20, ship.center.y - 2, 15, 3)];
bullet.image = [UIImage imageNamed:#"bullet2.png"];
bullet.hidden = NO;
[self.view addSubview:bullet];
[array1 addObject:bullet];
}
(by the way, array1 is declared in the .h file)
and theres a timer that controls the movement method
timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:#selector(movement) userInfo:nil repeats:YES];
first question is: what is the correct way of doing this? Storing a bullet for example until it is removed from the superview, should I store it another way?
and another question is, when I remove a UIImageView from the superview, does that remove it from memory so its not using up system resources?
Thank you for the help!
(will update if I Think of other questions
UPDATE
I would like to point out that currently this is not functioning correctly. When I click on a "shoot" button that I have created (which basically calls the getBullet method) the bullet appears but does not move.. not sure why. This is part of why I'm asking this question as well.
I offer you a totally different solution. Instead of using the built in UIKit to perform graphics calculation and move things around the screen, try using an external framework other than UIKit. The UIKit framework is really good at its intended job, which is more oriented around a touch based user interface than game development. The following is pulled from the UIKit documentation from Apple.The UIKit framework provides the classes needed to construct and manage an application’s user interface for iOS. It provides an application object, event handling, drawing model, windows, views, and controls specifically designed for a touch screen interface.As you can see, UIKit was made for the purposes of slick user interfaces, not animating sprites (spaceships, bullets) around the screen.
The best alternative is to look down another path. The first thing to go to would be SpriteKit. Made by Apple and already included in Xcode, SpriteKit is the perfect tool for your job. Made specifically for game development, it has the ability to seamlessly animate many sprites on screen at a time (bullets in your case) and has a built in physics engine. It is very easy to learn, and has the potential to turn you idea into a great game.
The link to the SpriteKit documentation can be found here.

Temporary disable physics from CCSprite

I'm currently discovering Cocos2D in combination with SpriteBuilder and I'm making stuff bounce around.
It's quite fun :)
What I'm trying to figure out is the following :
- On SpriteBuilder, I create a CCSprite, and I enable physics for this one.
- I can invoke it in XCode, and do whatever I want with it. Plus, the sprite is reacting accordingly to the physics settings applied to it. Great.
Now, what I would like to do is to disable the physics from this sprite in the code. I don't find a method in the Cocos2D API that would do the trick.
Can you help me out ?
Thank you for your time and help ;)
I write the correct answer then:
sprite.physicsBody.sensor = TRUE;

Resources