Why is manually moving a UIImageView slow? - iphone

I am trying to write a head-to-head iPhone Air Hockey (like) game. When the user touches the screen I move the "puck" by placing the UIImageView center where the touch center is.
Also I move the puck (another UIImageView) using a timer. This method is slow and shaky. I know I can get performance out of OpenGL ES but I do not want it to get that involved if I can help it.
I would also like to take advantage of the animation engine for special effects and such. How can I manually move the paddles and puck to a timer and get better performance. Or do I just have to embrace OpenGL ES for this?

Or do I just have to embrace the OpenGL?
No there is a middle ground: CoreAnimation.
Matt Gallagher has a demo game (for Mac) which uses Core Animation to display sprites. You will not see the same performance on iPhone as desktop but, your game is much simpler than his example.

Related

iPhone - Custom dynamic drawing using touch like in "Draw something" game OpenGL or Core Graphics?

I am trying to do dynamic drawing that draws a particle everywhere the finger is moving along the screen, exactly like in the game "Draw Something".
My intention is not to make a game, but I need this behavior for another app I am making.
Basically the behavior I am looking for is what the Apple sample code GLPaint does, simple drawing with the finger.
Now the question, I know that this can be done with OpenGL (GLPaint sample app does this), however I am new to OpenGL and I would like to use Core Graphics instead since it will only be 2D drawing.
Would it be possible to do this using Core Graphics only?, If so would performance be affected?. Some sample code for something like this using Core Graphics would be appreciated.
Is the best way to do this in Core Graphics to keep drawing a particle over and over again?.
Thank you,
Oscar
Have you considered using something like Cocos2D to achieve the OpenGL drawing? It's quite intuitive to pick up.
I'm sorry, I have no idea how easy/hard/performance hitting using CoreGraphics would be.

Why use Cocos over UI Kit for a 2D iPhone game?

I have been building a game in cocos for a while now I it has just occurred to me that it might just be a whole lot easier to use UIKit.
Could you please help me weight up the pros and cons, and share some experiences you have.
Here is my basic list of advantages for each. I feel I dont know enought about cocos to make a informed decision which is best.
Cocos
Game engine comes for free. Pause / resume callbacks every frame
Sprite animations
Uses open GL (is this true? is this an advantage?)
...Help me think of some more!
UI Kit
Far more Help / Documentation / Sample Code
Familiar for iPhone programmers
Easy to handel touch
Easy view animations
Easy to customise
Faster loading times? (is this true?)
Any feedback would be great!
EDIT: I have just seen a developer video (on an open GL game called Quest) where they used UI Kit for the UI elements, drawn over the top of the GL View. Is this possible / practical with cocos?
Cocos2d uses OpenGL for drawing its sprites, movements, and particles. Trying to use Quartz/Core Graphics (which is NOT accelerated!) is fundamentally much slower and power hungry.
Cocos2d is a sprite library with a scenegraph first and foremost. You would be duplicating effort to make a scenegraph using UIKit.
There are lots of nice freebies that work within Cocos which you would have to develop from the ground up.
So, in short: It draws way faster than CPU bound drawing calls, it provides 95% of what most sprite based games need without reinventing the same thing over and over, and is free.
Although UIKit is incredibly useful for gaming, Cocos2D offers a variety of easier ways to accomplish many common game-making operations. For instance, it has the ability to create easy scrolling-tile games (very common and very popular) using a simple few lines of code to import a "Tiled" map set. In terms of some of the benefits of UIKit over Cocos2D that you mentioned, they are very easy to accomplish in Cocos2D, as well.
Touch handling:
This is as easy as replacing the "touchesBegan" method of UIKit with "ccTouchesBegan" and setting self.isTouchEnabled = YES in the init method.
Animations:
This is so much easier in Cocos2d, mainly because frame rate and animation issues are solved. In UIKit, you cannot use CoreAnimation to move an object while still implementing collision detection. You would have to animate the object with an NSTimer. In Cocos2d, you can easily animate objects (called sprites) and still use collision detection and interaction with the object.
More Help/Documentation:
Cocos2D offers an incredible amount of documentation and, even more so, examples of code! If you can't find it there, there is a growing number of iPhone game developers who implement Cocos2D in their apps and are willing to help many problems.
If you are planning on developing games for iPhone, and not just general apps for productivity, music, etc., I highly suggest Cocos2D. Remember, you can always still find ways to combine this and UIKit for many operations! Those operations aren't completely lost!

iPhone game loop hybrid

I had an idea for an app that would require me to implement these little sprites that would require basic physics features like gravity, acceleration, etc... so naturally I looked into a game loop for the iPhone, there were plenty of threads on this, but what I am trying to understand is that I want to be able to create these sprites and have them behave naturally using a game loop, but still have full "standard" iPhone interaction, i.e. touches, pinching, swiping, core animation, core data, etc...
Is this just as simple as implementing a game loop on a background thread and updating sprites on that thread then doing the standard operations on the main UI thread (except of course for obvious background tasks)?
I would like some general overview of this type of thing if thats possible.
I wanted the same thing, but the iPhone SDK is all about async and callbacks and it's better to roll with the framework rather than fight it. If Cocos2d does not satisfy you, you could look into hooking into NSRunLoop.
With regards to CADisplayLink: I noticed a drop in performance (decreased triangle throughput) when using CADisplayLink with OpenGL ES (which you have stated you are not using so I will not elaborate further).
In Xcode try creating a iPhone OpenGL ES project. Just build and run with the default code. You should see a box moving up and down. This simple example should give you an insight into animating objects and how the OS version of the device matters. The OpenGL view is based off a normal view that you can attach gesture watchers to.
Check out one of the available game physics engines. Cocos2D is a good place to start: http://www.cocos2d-iphone.org/ -- free and lots of resouRces to get started with.

iPhone: Camera following player in cocos2d

I'm making an iPhone game in cocos2d.
I was wondering how I would make the camera / the view follow a specific sprite?
would I use the CCCamera class?
Yes, CCCamera would work. However, it has some drawbacks that make it undesirable for some uses. Moving the layers respectively all other objects relative to that sprite may be a better solution. It depends on the game.
First, read up what the different approaches and their drawbacks are, you can get a lot out of this cocos2d forum thread:
http://www.cocos2d-iphone.org/forum/topic/5363
It would be helpful if you could describe what your game is about and why you need the camera attached to that sprite.
For example, if you're thinking of a running game like Canabalt, i would not use the camera to scroll over the world, but instead scroll everything relative to the player (towards him) with the player sticking at about the same x coordinate while running. Perfect examples of games where you would not move the camera at all are the iCopter games, they are basically simplified versions of Canabalt. Notice that the player sprite always stays at the exact same x coordinate, and the game world just scrolls
Scrolling the camera itself in my opinion makes the most sense if you have a large game world that the player can traverse in all directions, and the number of objects are simply too numerous and also moving about in various directions, so updating their positions individually each frame would be both overkill and prone to errors. And since the game world is so huge, you would want to use the camera's position to limit what is drawn on screen.
use CCFollow actions
Like these :-
[self runAction:[CCFollow actionWithTarget:(u r hero) worldBoundary:CGRectMake(0,0,1050,350)]];
it will helps

iPhone development using sprites

I want to create an iPhone/iPod game. I want it to be 3d, but I want to use sprites instead of OpenGL to do this (I assume this would be easier since I don't know OpenGL).
I was thinking of simply layering sprites over top of each other and changing their size to give an illusion of 3d. It doesn't need to be too convincing since the game will be somewhat cartoony.
How can I use sprites as an alternative to OpenGL on the iPhone platform?
You can use CoreAnimation for this. Either using UIImageViews (or plain UIViews) or CALayers.
It's usually the best choice for some types of 2d games (board games, for example), since animation, rotation and scaling are really easy. Just keep in mind that if performance is your concern, OpenGL will always be better.
Depending on how much 3d, I'd recommend taking a look at cocos2d. It supports multiple layers, sprites, animations, etc, but is pretty straightforward to pick up & learn. (Much easier than OpenGL to me) The example code is very comprehensive.
http://code.google.com/p/cocos2d-iphone/
I have built a game using core animation with upto about 17 - 20 objects floating about the screen scaling and rotating and performance was fine on the iPhone (make sure you check regularly on the iPhone as the simulator doesnt simulate iPhone memory or CPU speed).
CoreAnimation is pretty simple and really powerful. Use PNG's for images and I don't think you will have to many issues. The real killer of this will be alpha's in your images, this is hard work for the iPhone. So the less transparency you have the better you app will go.
In addition to Marco's answer I want to add: Not using OpenGL may also tax the device battery a little more. As I understand it, OpenGL ES can be more efficient on a device power supply (if implemented properly). Of course, this depends on how much animation is going to be used with UIImageView, UIView or CALayers, etc.
I'm sure there is a tipping point.