• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

一个基于cocos2d-x3.0和Box2d的demo小程序

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
小程序输入框初始赋值和双向绑定发布时间:2022-07-18
下一篇:
zencart随机获取一张产品图片及价格小程序发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap