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.
Related
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.
I have several iOS puzzle apps, written with Objective-C using QuartzCore.
I would like to include a quick animation showing how to solve the puzzle. Non interactive is OK.What would be a small format to use? Gif-anim? I would like to use minimal coding (pretty much just a 'do not show again' UIButton) and have the entire thing done with graphics file/s. Anyone has any suggestions? Maybe using web-kit?Thanks!!
What I ended up doing is following the suggestion of Mahal.
I created a view with a simple background image, and four buttons: Back, Close, Next slide and Previous slide.
At the start, you can choose to see tutorial. Otherwise, it's available from the help screen.Sharing flowchart below...
What about a UIImageView and an array of PNGs?
UIImageView
#property(nonatomic, copy) NSArray *animationImages
#property(nonatomic) NSTimeInterval animationDuration
I really need some help, since iOS 7 I have had nothing but trouble with UISwitch.
The Problem
After moving the view up when the keyboard is displayed and restoring it to original position, I then turn any UISwitch on/off it will work OK, however it will "Bounce Down" during the animation. this only seems to happen to UISwitch's that have the .thumbTintColor attribute. by that I mean self.borders.thumbTintColor = [UIColor redColor]; when the switch is off and self.borders.thumbTintColor = [UIColor greenColor]; when switch is on.
For clarity everything works well until the moving of the view (I can't not do this as you are not able to see the text field).
I have searched everywhere (including the class reference) and I haven't found anything helpful, or anyone else with this problem.
UPDATE
I have tried setting the UISwitch thumTintColor various ways, everything works fine until I move the view up to compensate for the Keyboard. if I drag the switch either on my iPad or via simulator on and off slowly the "bounce down" occurs halfway and when the color changes from green to red or red to green depending on state, I think this is an actual IOS7 UISwitch Bug. I really do not want to use a third part switch plug in on my app, so not sure what to do now, one would of thought that changing the tint color on the thumb would not have been such a huge problem.
I have tried turning off the animation from on to off - off to on makes no difference.
I have created a new UISwitch via code not IB and the same problem exists.
created a new test app with only a switch and text box to test and the same thing occurs.
Questions
1) My thoughts are this is a genuine bug. How do i log it with apple (never done it before) so if someone can point me to the correct place i would be grateful.
2) I am now thinking of using Buttons as switches, but feel that is not the way to go, are there any practical reasons not to do so i.e problems submitting the app to the App store etc?
I had the similar issue and found a solution. It's a bit hacky but it does the job.
Just need to add a small delay between setting isOn property to On/Off and changing the color of the thumb.
switch.isOn = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.switch.thumbTintColor = .white
}
Answer 1: Logging a bug with apple can be done via the iOS Dev center (There's a link on the bottom of the page). You can also ask for technical support. Click on the "Developer Support Center" button, then go to "Technical Support (TSI)" (Please note you only have a limited amount that you're allowed to use per year, before you have to buy more).
Answer 2: I think that having two buttons would be a very bad User Interface decision. Apple may reject it because of that (but you never know). I would get the bug report in, and go from there.
Good Luck!
I found interesting solution may be it helps.
NSArray *array = [self.mySwitch subviewsWithClass:[UIImageView class]];
for (UIImageView *imageView in array) {
imageView.image = [UIImage imageNamed:#"Untitled-1.png"];
}
Hear I used method from UIView category
- (NSArray*)subviewsWithClass:(Class)class
{
NSMutableArray *array = [NSMutableArray array];
if ([self isKindOfClass:class]) {
[array addObject:self];
}
for (UIView *subview in self.subviews) {
[array addObjectsFromArray:[subview subviewsWithClass:class]];
}
return array;
}
Image size is {57, 43.5}. Circle radius is 15(with shadow)
I understand that it is not very good way to solve. But if you really need it helps.
Play with images little bit =)
Hopefully my question will make sense. Just started with Qt so bear with me.
I have an app in Objective-C (for iOS) and I'm trying to add a piece of code in Qt into it.
Most of the UI is in Objective-C (meaning I got UIViews, UIViewControllers, ...) and only a part of it should be handle in Qt.
To create a UI element in Qt, I guess I need to create something that inherits from QWidget.
But how would I set the parent of this QWidget to one of my UIView element?
I understand it would be easier to do everything in Qt, but that is just to much work so that is why I am hoping I can keep part of my UI in Objective-C and the other part in Qt.
Thanks for your help
--
Edit
Well I was following the example from here. Even if it is for Mac OS.
QWidget * qWidget = new QWidget();
qWidget->move(0, 0);
qWidget->setPalette(QPalette(Qt::red));
qWidget->setAutoFillBackground(true);
QVBoxLayout *layout = new QVBoxLayout();
QPushButton *pushButton = new QPushButton("An Embedded Qt Button!", qWidget);
pushButton->setAttribute(Qt::WA_LayoutUsesWidgetRect);
layout->addWidget(pushButton);
qWidget->setLayout(layout);
// Adjust Cocoa layouts
UIView *uiWidgetView = (__bridge UIView *)((void*)qWidget->winId()); // Fancy cast for ARC
// Add the widget
[self.view addSubview:uiWidgetView];
qWidget->show();
pushButton->show();
But it does not seem to work for iOS...
I have two messages:
- This plugin does not support propagateSizeHints()
Do not know how to fix that one
- QIOSBackingStore needs to have the same size as its window
This one is referenced here. Does not seem to be fixed yet.
Note: Adding a simple uiview (like a uilabel) to my current view works fine. Just this one that does not seem to render for some reason... :s
Create new project from opengl template via xcode4, you will see one color square moving.
I want to put two more small views to show the same thing. What I did is to add two
EAGLView *glView1;
EAGLView *glView2;
Then set up with property/systhesize etc, and same coding with self.view like these
[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];
if (!glView2) {
glView2 = [[EAGLView alloc] init];
}
[self.glView2 setContext:context2];
[self.glView2 setFramebuffer];
if (!glView1) {
glView1 = [[EAGLView alloc] init];
}
[self.glView1 setContext:context];
[self.glView1 setFramebuffer];
Do same thing for other place which self.view did. Also do the right thing to link within interface builder. (xcode4)
But the result show
I found that the last draw order in drawFrame will decide which window will show
[self.glView1 setFramebuffer];
[self.glView2 setFramebuffer]; // these will not show , but if put last, it will show
[(EAGLView *)self.view setFramebuffer]; // this will work
Does that make sense to explain my purpose and issues ?
Thanks very much for point out the root cause and help me go through this.
You need to read about OpenGL ES 2.0 ("OGLES2.0") . You shouldn't be using multiple EAGLView instances. You need only one, and you use OGLES2.0 commands to draw on it.
OpenGL is not easy to learn, and you will have to invest many hours of your time before you can achieve even simple renders with it. You should seek an external library like cocos2D to help you achieve what you want. Read the tutorials and documentation there carefully, and experiment. It will still take you a lot of time to achieve pleasing results, but far less than if you were using OGLES directly yourselfe