iPhone Development
In: Tutorials
20 Jan 2010Someone on the cocos2D-iphone forum needed a tutorial for a accelerometer pong/breakout paddle so here you go…
1.First make sure you have cocos2d 0.8.2 for this tutorial and create a new cocos2d iphone app, you can call it anything, I’ll call it PongPaddle
2.Delete the HelloWorldScene files
3.Create a new Objective-C file that subclasses a NSObject and name it PongPaddleScene.m and include the .h file aswell…
4.In PongPaddleAppDelegate.m change the #import “HelloWorldScene.h” to #import “PongPaddleScene.h” That line of code will be at the top of the file…
5.In PongPaddleAppDelegate.m change the line
[[Director sharedDirector] runWithScene: [HelloWorld scene]];
to
PongPaddleScene *pongPaddleScene = [PongPaddleScene node];
[[Director sharedDirector] runWithScene: pongPaddleScene];
6. Go to PongPaddleScene.h and add #import “Scene.h” and change
@interface PongPaddleScene : NSObject
to
@interface PongPaddleScene : Scene
7.Add:
![]()
to your project’s resources
8.In PongPaddleScene.m change the code to:
#import ”PongPaddleScene.h”
#import ”cocos2d.h”
#import ”Paddle.h”
@implementation PongPaddleScene
-(id)init{
self = [super init];
if (self != nil) {
Paddle *paddle = [Paddle node];
[self addChild:paddle];
}
return self;
}
@end
Notice: This code is for the next part
9.Now create another another file that subclasses an NSObject just like before except call it Paddle.m and include the .h file
10. Change Paddle.h to:
#import
#import ”Layer.h”
@interface Paddle : Layer {
}
@end
11.Now change Paddle.m to:
#import ”Paddle.h”
#import ”Sprite.h”
#import ”cocos2d.h”
@implementation Paddle
-(id)init{
self = [super init];
if (self != nil) {
Sprite *paddle = [Sprite spriteWithFile:@"paddle.png"];
paddle.position = ccp(240, 20);
[self addChild:paddle];
}
return self;
}
@end
Now the next part is actually adding the accelerometer which is pretty easy and will all be done in Paddle.m
12.Right under [self addChild:paddle]; add self.isAccelerometerEnabled = YES;
13.Now we add [[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0 / 30)]; Under That…
14. now right before the @end add this code:
#define kFilteringFactor 0.1
UIAccelerationValue rollingY;
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{
}
This is where we will move the paddle with the accelerometer
15. now inside of the code we just put add:
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 – kFilteringFactor));
float acelx = -(acceleration.y – rollingY);
float x = acelx*40;
if (self.position.x > 0 && self.position.x < 480) {
self.position = ccp(self.position.x+x, self.position.y);
}
if (self.position.x < 55) {
self.position = ccp(56, self.position.y);
}
if (self.position.x > 435) {
self.position = ccp(434, self.position.y);
}
if (self.position.x < 55 && x > 1) {
self.position = ccp(self.position.x+x, self.position.y);
}
if (self.position.x > 435 && x < 0) {
self.position = ccp(self.position.x+x, self.position.y);
}
16. Hit build and Go and make sure you have it hooked up to a real device for accelerometer!
Download the source by clicking here.
If you have any questions or comments please drop a comment!
We program websites, iphone/ipod touch/ipad apps.. Our blog is here to not only share our progress on our projects but to share our knowledge via tutorials.
1 Response to Pong/Breakout Paddle Cocos2D Tutorial
quick payday loans
February 11th, 2010 at 3:39 pm
I am completely impressed with the article I have just read. I wish the writer of web-geeks.com can continue to provide so much worthwhile information and unforgettable experience to web-geeks.com readers. There is not much to say except the following universal truth: 76.4% of all statistics are wrong I will be back.