I am trying to develop a simple soccer game including penalty kicks in which i have to animate a ball from player to the goal post...earlier i have been using simple animations using a timer to add to the axis of ball image so that it moves from 1 point to another..but i did not have the desired result as animations were not that smooth...So i was thinking of using a Game Engine...Since i am a New Programmer i have no idea about game engine and neither can i find any proper documentation regarding engines like box2d or chipmunks or sparrow..i was also thinking of using UIView animations instead of the earlier animations as i think that can achieve far better animations without scratching my head trying to work on a game engine....I am going no where with this so it would be really great if someone can put some light on this issue of mine???
-
5Here's **the** box2d **engine** manual: http://www.box2d.org/manual.html ...In what **ways** is it not proper **documentation** ? – FrustratedWithFormsDesigner Sep 08 '10 at 13:26
-
1If you don't use a game engine, you're going to have to implement what game engines do under the hood. That's going to be harder than using a game engine, unless you know what you're doing pretty well with graphics, input, etc. – LarsH Sep 09 '10 at 03:21
2 Answers
Use UIView animations, like in:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // or whatever time
object.center=CGPointMake(object.center.x+2, object.center.y+4);
// or whatever
[UIView commitAnimations];
You should also use an NSTimer with the same interval so that you can call animations smoothly.
NSTimer *timer=[NSTimer scheduledTimerWithTimeInterval:0.3 target: self
selector:@selector(animation) userInfo: nil repeats: YES];
Then, implement the method:
- (void)animation {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // or whatever time
object.center=CGPointMake(object.center.x+5, object.center.y+7);
// or whatever
[UIView commitAnimations];
}
This should do for ANY simple game.
-
NSTimers make a best effort, but are not exact, when the timer is fired, you should record the current time in a variable and read out the previous time. use these two times to get a delta.if you want to move something 5 frames every 0.3 seconds, you would use 5/0.3*delta to calculate the distance. – MCannon Nov 29 '10 at 23:43
-
Actually don't ever do that, because NSTimers, though they may be triggered late (we're talking about very few milliseconds here, if you're app is memory efficient), they'll fire precisely. Don't ever change the distance when using a repeating timer. – Nov 30 '10 at 14:32
As you tagged the question with cocos2d
, I guess you're using it, or planning to. Animating CCSprites
is easy as you can see e.g. in this game https://github.com/haqu/tweejump.
In your onEnter
implementation, just call
[self scheduleUpdate]
this will call routinely the update:
where you can do your drawing
- (void)update:(ccTime)dt {
ball_pos.x += ball_velocity.x * dt;
ball_pos.y += ball_velocity.y * dt;
ball_velocity.x += ball_acc.x * dt;
ball_velocity.y += ball_acc.y * dt;
//game logic goes here (collision, goal, ...)
ball.position = ball_position;
}
That'll handle smooth movement of the ball. ball_pos
, ball_velocity
and ball_acc
being vvCertex2F
.
You probably don't even have to deal with acceleration, and only give an impulse to the ball when someone hit it (i.e. bump the velocity).
You probably also want some damping to slow the ball down. you do it by reducing the velocity at every step

- 16,134
- 5
- 57
- 85