一個基於cocos2d-x 3.0和Box2d的demo小程序

p圖demo小應用,想怎麼p就怎麼p

本文參考於http://blog.csdn.net/xiaominghimi/article/details/6776096http://www.cnblogs.com/liufan9/archive/2013/04/11/3012275.html

於上面基於cocos2d-x 2.0不一樣的地方,本本是基於cocos2d-x 3.0

首先,當然是下載和安裝cocos2d-x 3.0了,網址:http://www.cocos2d-iphone.org/download

其次,下載Box2d,網址:https://github.com/vegerjiang/Box2d

創建一個cocos2d的項目(怎麼創建這裏不重述了),加入Box2d庫(直接拖到Libraries目錄),在Build Settings->Search Paths->Head Search Paths中增加一項"$(SRCROOT)/$(PROJECT_NAME)/Libraries"。

如果能編譯運行成功,說明你已經建好了一個空的基於cocos2d-x 3.0和Box2d的ios項目了。

新建一個ooc類HelloLay,這裏需要注意亮點:

1.HelloLay必須繼承於CCLayout。

2.HelloLay.m改名爲HelloLay.mm。

具體代碼請從https://github.com/vegerjiang/testBox2d下載,詳細的解釋請參照http://www.cnblogs.com/liufan9/archive/2013/04/11/3012275.html

爲了方面某些懶童鞋,下面把類HelloLay的.h文件和.mm文件貼出來。

p圖demo小應用,想怎麼p就怎麼p


HelloLay.h文件

//
//  HelloLayer.h
//  testBox2d
//
//  Created by JiangHuifu on 14-5-28.
//  Copyright (c) 2014年 veger. All rights reserved.
//
#import "cocos2d.h"
#import "cocos2d-ui.h"

#import "CCLayout.h"


#define PTM_RATIO 32.0
@interface HelloLayer : CCLayout
+(id)scene;
@end

HelloLay.mm文件

p圖demo小應用,想怎麼p就怎麼p


//
//  HelloLayer.m
//  testBox2d
//
//  Created by JiangHuifu on 14-5-28.
//  Copyright (c) 2014年 veger. All rights reserved.
//

#import "HelloLayer.h"
#import "Box2D.h"
@interface HelloLayer(){
    b2World* _world;
    b2Body* _body;
    CCSprite* _ball;
}
@property(nonatomic,strong) CCSprite* ball;
@end
@implementation HelloLayer
@synthesize ball = _ball;
+(id)scene{
    CCScene* scene = [CCScene node];
    HelloLayer* layer = [HelloLayer node];
    [scene addChild:layer];
    return scene;
}
-(id)init{
    if (self = [super init]) {
        
        CGSize winSize = [[CCDirector sharedDirector] viewSize];
        
        //Create sprite and add it to the layout
        _ball = [CCSprite spriteWithImageNamed:@"ball.png"];
        _ball.scaleX = 52 / _ball.contentSize.width;
        _ball.scaleY = 52 / _ball.contentSize.height;
        _ball.position = ccp(100, 300);
        [self addChild:_ball];
        
        //Create a world
        b2Vec2 gravity = b2Vec2(0.0f,-8.0f);
        _world = new b2World(gravity);
        
        //Create edges around the entire screen
        b2BodyDef groundBodyDef;
        groundBodyDef.position.Set(0, 0);
        
        b2Body* groundBody = _world->CreateBody(&groundBodyDef);
        b2EdgeShape groundEdge;
        b2FixtureDef boxShapeDef;
        boxShapeDef.shape = &groundEdge;
        
        //wall definitions
        groundEdge.Set(b2Vec2(0, 0), b2Vec2(winSize.width/PTM_RATIO, 0));
        groundBody->CreateFixture(&boxShapeDef);
        groundEdge.Set(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
        groundBody->CreateFixture(&boxShapeDef);
        groundEdge.Set(b2Vec2(0,winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO,winSize.height/PTM_RATIO));
        groundBody->CreateFixture(&boxShapeDef);
        groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO,winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
        groundBody->CreateFixture(&boxShapeDef);
        
        //Create ball body and shape
        b2BodyDef ballBodyDef;
        ballBodyDef.type = b2_dynamicBody;
        ballBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
        ballBodyDef.userData = (__bridge void*)_ball;
        _body = _world->CreateBody(&ballBodyDef);
        
        b2CircleShape circle;
        circle.m_radius = 26.0/PTM_RATIO;
        
        b2FixtureDef ballShapeDef;
        ballShapeDef.shape = &circle;
        ballShapeDef.density = 1.0f;
        ballShapeDef.friction = 0.2f;
        ballShapeDef.restitution = 0.8f;
        _body->CreateFixture(&ballShapeDef);
        
        [self schedule:@selector(tick:) interval:0.017];
        
        [self schedule:@selector(kick) interval:5.0];
        
        
        self.userInteractionEnabled = YES;
    }
    return self;
}
-(void)tick:(CCTime) dt{
    _world->Step(dt, 10, 10);
    for (b2Body* b = _world->GetBodyList(); b; b=b->GetNext()) {
        CCSprite* ballData = (__bridge CCSprite*)b->GetUserData();
        ballData.position = ccp(b->GetPosition().x*PTM_RATIO,
                                b->GetPosition().y*PTM_RATIO);
        ballData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
    }
}
-(void)kick{
    b2Vec2 force = b2Vec2(30, 30);
    _body->ApplyLinearImpulse(force, _body->GetPosition());
}
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    b2Vec2 force = b2Vec2(-30,30);
    _body->ApplyLinearImpulse(force, _body->GetPosition());
}

-(void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
    NSLog(@"touchEnded");
}


-(void)dealloc{
    delete _world;
    _body = NULL;
    _world = NULL;
}
@end


發佈了18 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章