Showing posts with label Game in iOS. Show all posts
Showing posts with label Game in iOS. Show all posts

Wednesday, November 7, 2012

Remove all objects in cocos2d game world with box2d


You can remove all objects in cocos2d game world with box2d


for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
        if (b->GetUserData() != NULL) {
            CCSprite *sprite = (CCSprite *)b->GetUserData();
            [self removeChild:sprite cleanup:YES];
            _world->DestroyBody(b);
           
        }
    }

Box2D Joints


  1. Mouse Joint
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector]
        convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
    b2Vec2 locationWorld =
        b2Vec2(touchLocation.x/PTM_RATIO, touchLocation.y/PTM_RATIO);
    b2AABB aabb;
    b2Vec2 delta = b2Vec2(1.0/PTM_RATIO, 1.0/PTM_RATIO);
    aabb.lowerBound = locationWorld - delta;
    aabb.upperBound = locationWorld + delta;
    SimpleQueryCallback callback(locationWorld);
    world->QueryAABB(&callback, aabb);
if (callback.fixtureFound) {
        b2Body *body = callback.fixtureFound->GetBody();
        b2MouseJointDef mouseJointDef;
        mouseJointDef.bodyA = groundBody;
        mouseJointDef.bodyB = body;
        mouseJointDef.target = locationWorld;
        mouseJointDef.maxForce = 50 * body->GetMass();
        mouseJointDef.collideConnected = true;
        mouseJoint = (b2MouseJoint *)
            world->CreateJoint(&mouseJointDef);
        body->SetAwake(true);
return YES; }
    return TRUE;
}
-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
    CGPoint touchLocation = [touch locationInView:[touch view]];
    touchLocation = [[CCDirector sharedDirector]
        convertToGL:touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
    b2Vec2 locationWorld = b2Vec2(touchLocation.x/PTM_RATIO,
        touchLocation.y/PTM_RATIO);
    if (mouseJoint) {
        mouseJoint->SetTarget(locationWorld);
} }
-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
     if (mouseJoint) {
        world->DestroyJoint(mouseJoint);
        mouseJoint = NULL;
     }
}



        2. Revolute Joint  

    wheelL = [Box2DSprite spriteWithSpriteFrameName:@"Wheel.png"];
    wheelL.gameObjectType = kCartType;
    wheelLBody = [self createWheelWithSprite:wheelL
                                      offset:b2Vec2(-63.0/100.0, -48.0/100.0)];

   
    b2RevoluteJointDef revJointDef;
    revJointDef.Initialize(body, wheelLBody,
                           wheelLBody->GetWorldCenter());
   
    revJointDef.enableMotor = true;
    revJointDef.maxMotorTorque = 1000;
    revJointDef.motorSpeed = 0;


//restrict revolute joints to a certain angle range with Box2D


revJointDef.lowerAngle = CC_DEGREES_TO_RADIANS(-30);
revJointDef.upperAngle = CC_DEGREES_TO_RADIANS(60);
revJointDef.enableLimit = true;
 ///////////////////////
    wheelLJoint = (b2RevoluteJoint *) world->CreateJoint(&revJointDef);


        3. Prismatic Joint


A prismatic joint is a type of joint that restricts two bodies so they can move relative to each other only along a specified axis. 
 



   b2PrismaticJointDef prisJointDef;
    prisJointDef.Initialize(body, legsBody,
                            legsBody->GetWorldCenter(), axis);
    prisJointDef.enableLimit = true;
    prisJointDef.lowerTranslation = 0.0;
    prisJointDef.upperTranslation = 43.0/100.0;
    world->CreateJoint(&prisJointDef);



Sunday, August 19, 2012

Add Pop Up Message to the Game


#import "cocos2d.h"

@interface Scene4UILayer : CCLayer {
    CCLabelTTF *label;
}

- (BOOL)displayText:(NSString *)text
andOnCompleteCallTarget:(id)target selector:(SEL)selector;

@end

//////////////

#import "Scene4UILayer.h"

@implementation Scene4UILayer

- (id)init {   
    if ((self = [super init])) {       
        CGSize winSize = [CCDirector sharedDirector].winSize;
        label = [CCLabelTTF labelWithString:@"" fontName:@"Helvetica"
                                   fontSize:48.0];
        label.position = ccp(winSize.width/2, winSize.height/2);
        label.visible = NO;
        [self addChild:label];       
    }
    return self;   
}

- (BOOL)displayText:(NSString *)text
andOnCompleteCallTarget:(id)target selector:(SEL)selector {
    [label stopAllActions];
    [label setString:text];
    label.visible = YES;
    label.scale = 0.0;
    label.opacity = 255;
   
    CCScaleTo *scaleUp = [CCScaleTo actionWithDuration:0.5 scale:1.2];
    CCScaleTo *scaleBack =
    [CCScaleTo actionWithDuration:0.1 scale:1.0];
    CCDelayTime *delay = [CCDelayTime actionWithDuration:2.0];
    CCFadeOut *fade = [CCFadeOut actionWithDuration:0.5];
    CCHide *hide = [CCHide action];
    CCCallFuncN *onComplete =
    [CCCallFuncN actionWithTarget:target selector:selector];
    CCSequence *sequence = [CCSequence actions:scaleUp, scaleBack,
                            delay, fade, hide, onComplete, nil];
    [label runAction:sequence];   
    return TRUE;   
}

@end


Sunday, July 29, 2012

Adding Box2D Files to your Cocos2D Project


  1. First, download 'cocos2d-iphone-x.0.tar.gz' , and Open a Finder window and navigate to where you downloaded the Cocos2D source.
  2. Open the folder and navigate to external\Box2d\Box2D. 
  3. Open a second Finder window, navigate to your project, and navi-
    gate to libs. 
  4. Copy the Box2D folder to the libs folder. 








  • Adding the Box2D directory to your Xcode project   


  • You have to set "User Header Search Paths" as 'Project_Name/libs'



  • And also you have to add two more files :
    the ones responsible for drawing Box2D objects to the screen for debug purposes.


  navigate to your Cocos2D directory, and navigate to templates\cocos2d_box2d_app\Classes. Inside you’ll find the files GLES- Render.h and GLES-Render.mm. Select those two files and drag them to your new Box2D group in Xcode. Make sure Copy items into destination group’s folder (if needed) is checked, and click Finish.

  • After that  #import "Box2D.h", what ever file you want and mack sure rename the file with a '.mm' extension. Because you are importing the Box2D header file (which uses C++) in files that are set up to use only Objective-C. To instruct the compiler to allow both C++ and Objective-C, we can add '.mm' extension.