博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个基于cocos2d-x 3.0和Box2d的demo小程序
阅读量:6817 次
发布时间:2019-06-26

本文共 4225 字,大约阅读时间需要 14 分钟。

本文參考于和。

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

首先。当然是下载和安装cocos2d-x 3.0了,网址:。

其次,下载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。

具体代码请从下载,具体的解释请參照。

为了方面某些懒童鞋。以下把类HelloLay的.h文件和.mm文件贴出来。

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文件

////  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

转载地址:http://babzl.baihongyu.com/

你可能感兴趣的文章
teacher forcing
查看>>
Linux命令小记
查看>>
基于ROS和beaglebone的串口通信方式,使用键盘控制移动机器人
查看>>
android.view.WindowLeaked的解决办法
查看>>
存储过程的笔记
查看>>
OpenCV学习(27) 直方图(4)
查看>>
深度学习原理与框架-Tensorflow基本操作-实现线性拟合
查看>>
[leetcode-168-Excel Sheet Column Title]
查看>>
SpringBoot和数据库连接
查看>>
二叉搜索树
查看>>
网页小技巧-360doc个人图书馆复制文字
查看>>
delete删除-some
查看>>
maven阿里云中央仓库
查看>>
15.12.14listbox列表框
查看>>
sql 行转列
查看>>
(转)Python新手写出漂亮的爬虫代码1——从html获取信息
查看>>
配置Nim的默认编译参数 release build并运行
查看>>
图片下载
查看>>
《构建之法》第四章读后感
查看>>
python os.path.dirname()
查看>>