I had an idea for a very simple little game I'd like to make. I have never made anything close to a game before so I am really, really new to this. For this game, I'd need to move around some PNG files on the screen - very basic animations.
"player" is an UIImageView. This does in fact move "player". Not quite the way I want it to but I guess I could get there. (Player jumps up 100 pixel in no time and then slides back down in the course of a second... I don't understand why he "jumps" up... but anyway...)
So, the question I have is: Which is the best approach to make around a couple of objects/images on the screen, detect whether they hit each other etc.... simple stuff. Do these UIView animations work for that kind of stuff?
If not, what do you recommend? What would I have to learn? (Because that's what I want, learn how to do it... it's just that I want to learn the right thing. )
Thanks for your time and any advise is appreciated.
Cheers,
Bob
__________________ We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.
If you are considering a game with collisions and or multiple animations and some physics, I would suggest using "cocos2d for iphone" it costs you nothing, and is a quick learn with another great community for support like this one. The latest release 0.99.3 includes many,many examples which you can modify to your hearts desire. Check it out cocos2d for iPhone I think you'll find it very useful!
Good Luck!
You can also find several helpful youtube video tutorials for cocos2d.
For animations of UIImageViews that needs action (ie to check for collisions etc), you are better to use NSTimer, instead of using CAnimation.
CAnimation is good for something not requiring action, like the clouds moving around, etc..
The thing with CAnimation is that, the object is translated immediately once you call the translation, but the OS "plays" the transition later. So when you do animation of translating an object from 0 to 50, the object is already at 50 when it is executed. Only the OS plays it for you. Whilst if u are using NSTimer, the object is translated by x amount each time the code is called.
In my first game, I use CAnimation only as it is just a mancala game that requires no action of objects (but just to animate). In my 2nd game (in progress now), i use a mixture of CAnimation and NSTimer, because I have stuffs all over the places and need to check for collision.
Hey Robert. I think that for most games UIView animations are not usually the best way to go, but for some simple gaes (usually puzzle games) it can work. But cocos2d is a great alternative
But anyway the reason why it "jumps" is just what roco guy (sorry i forgot ur name haha) said. The propert is changed immediately, but the system manages the animation. So when you start another animation immediately after the first one, it finishes that one so that it can start the next one. You could use NSTimers to achieve the animation, but i think using UIView's animation callback methods is a better bet. So when you start the animation, you have to give it a name. Ex: [UIView beginAnimations:@"Animation1" context:nil];
then set the delegate to ur app and then add the function you want to call. So the code would look something like this:
Code:
[UIView beginAnimations:@"MyID" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(methodName:finished:context:)];
- (void)methodName:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
if ([animationID isEqualToString:@”MyID”] && [finished boolValue]) {
[UIView beginAnimations:@”NextStepInMultiStep” context:(void *)];
// do another animation, might use the context in here
[UIView commitAnimations];
}
}
PS: thx to CS193P for the example code, I was too lazy to write it myself
Anyway you could do that and then check the animationID to know which animation ended and then start the one that needs to start after that
Thanks rocotilos and kapps11 for the info on CAnimations. I did use setAnimationDidStopSelector to make it do what I wanted it to do.
Thanks hm50 for pointing out cocos2d. I already expected that CAnimations are not the way to go. As soon as I have more time next week, I'll dive into cocos2d.
Thanks guys, your information was very helpful.
Cheers,
Bob
__________________ We are God’s middle children, according to Tyler Durden, with no special place in history and no special attention.