一个基于cocos2d-x 3.0和Box2d的demo小程序

时间:2023-03-09 02:15:54
一个基于cocos2d-x 3.0和Box2d的demo小程序

p图demo小应用。想怎么p就怎么p

本文參考于http://blog.****.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