Skip to content
Browse files

Ball now respawn after score.

  • Loading branch information...
1 parent cac79e3 commit 30b2ef4a4180615c5b71291292551f86a923f4b9 @pjvds pjvds committed Apr 1, 2011
Showing with 44 additions and 4 deletions.
  1. +2 −0 Classes/Ball.h
  2. +26 −0 Classes/Ball.mm
  3. +16 −4 Classes/iPongLayer.mm
View
2 Classes/Ball.h
@@ -12,5 +12,7 @@
@property(readonly,assign) b2Fixture* Fixture;
-(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody: (b2Vec2) force;
+-(void) respawnLeft;
+-(void) respawnRight;
-(void)dealloc;
@end
View
26 Classes/Ball.mm
@@ -35,6 +35,32 @@ -(id) spawn: (CCLayer*) layer: (b2World*) world: (b2Body*) groundBody: (b2Vec2)
return self;
}
+-(void) reset {
+ CGSize winSize = [CCDirector sharedDirector].winSize;
+ float32 angle = Body->GetAngle();
+
+ Body->SetLinearDamping(0);
+ Body->SetAngularDamping(0);
+ Body->SetAngularVelocity(0);
+ Body->SetLinearVelocity(b2Vec2(0,0));
+
+ b2Vec2 center = b2Vec2(winSize.width/2/PTM_RATIO, winSize.height/2/PTM_RATIO);
+ Body->SetTransform(center, angle);
+}
+
+-(void) respawnLeft {
+ [self reset];
+
+ b2Vec2 force = b2Vec2(-10,10);
+ Body->ApplyLinearImpulse(force, Body->GetPosition());
+}
+
+-(void) respawnRight{
+ [self reset];
+
+ b2Vec2 force = b2Vec2(10,0);
+ Body->ApplyLinearImpulse(force, Body->GetPosition());
+}
-(void)dealloc{
[super dealloc];
View
20 Classes/iPongLayer.mm
@@ -61,21 +61,23 @@ - (void)setupWorld {
}
- (void)setupGroundBody {
+
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
_groundBody = _world->CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
+
b2FixtureDef groundBoxDef;
groundBoxDef.shape = &groundBox;
+
+ // top wall.
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
_bottomFixture = _groundBody->CreateFixture(&groundBoxDef);
- groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
- _groundBody->CreateFixture(&groundBoxDef);
+
+ // bottom wall.
groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
_groundBody->CreateFixture(&groundBoxDef);
- groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
- _groundBody->CreateFixture(&groundBoxDef);
}
- (void)spawnScoreboards{
@@ -113,6 +115,16 @@ - (void)tick:(ccTime) dt {
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
+
+ b2Vec2 ballPos = _ball.Body->GetPosition();
+ ballPos *= PTM_RATIO;
+
+
+ if(ballPos.x < 0){
+ [_ball respawnRight];
+ } else if(ballPos.x > winSize.width){
+ [_ball respawnLeft];
+ }
}
- (void)dealloc {

0 comments on commit 30b2ef4

Please sign in to comment.
Something went wrong with that request. Please try again.