一个基于cocos2d-x 3.0和Box2d的demo小程序
p圖demo小應用。想怎么p就怎么p
本文參考于http://blog.csdn.net/xiaominghimi/article/details/6776096和http://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; @endHelloLay.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 worldb2Vec2 gravity = b2Vec2(0.0f,-8.0f);_world = new b2World(gravity);//Create edges around the entire screenb2BodyDef groundBodyDef;groundBodyDef.position.Set(0, 0);b2Body* groundBody = _world->CreateBody(&groundBodyDef);b2EdgeShape groundEdge;b2FixtureDef boxShapeDef;boxShapeDef.shape = &groundEdge;//wall definitionsgroundEdge.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 shapeb2BodyDef 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
posted on 2017-05-28 20:19 mthoutai 閱讀(...) 評論(...) 編輯 收藏
轉載于:https://www.cnblogs.com/mthoutai/p/6916533.html
總結
以上是生活随笔為你收集整理的一个基于cocos2d-x 3.0和Box2d的demo小程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【原创】多dpi适配的新姿势
- 下一篇: 窗体显示类