Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #import "HelloWorldLayer.h"
- #define PTM_RATIO 32
- @implementation HelloWorldLayer
- +(CCScene *) scene{
- CCScene *scene = [CCScene node];
- HelloWorldLayer *layer = [HelloWorldLayer node];
- [scene addChild: layer];
- return scene;
- }
- -(id) init {
- if( (self=[super init])) {
- CGSize winSize = [CCDirector sharedDirector].winSize;
- self.isAccelerometerEnabled = YES;
- self.isTouchEnabled = YES;
- // Create sprite and add it to the layer
- _spear = [CCSprite spriteWithFile:@"SPEAR.png" rect:CGRectMake(0, 0, 100, 10)];
- _spear.position = ccp(100, 100);
- [self addChild:_spear];
- // Create a world
- b2Vec2 gravity = b2Vec2(0.0f, -20.0f);
- bool doSleep = true;
- _world = new b2World(gravity, doSleep);
- // Create edges around the entire screen
- b2BodyDef groundBodyDef;
- groundBodyDef.position.Set(0,0);
- b2Body *groundBody = _world->CreateBody(&groundBodyDef);
- b2PolygonShape groundBox;
- b2FixtureDef boxShapeDef;
- boxShapeDef.shape = &groundBox;
- groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
- groundBody->CreateFixture(&boxShapeDef);
- groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(0, winSize.height/PTM_RATIO));
- groundBody->CreateFixture(&boxShapeDef);
- groundBox.SetAsEdge(b2Vec2(0, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
- groundBody->CreateFixture(&boxShapeDef);
- groundBox.SetAsEdge(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO), b2Vec2(winSize.width/PTM_RATIO, 0));
- groundBody->CreateFixture(&boxShapeDef);
- // Create spear body and shape
- b2BodyDef spearBodyDef;
- spearBodyDef.type = b2_dynamicBody;
- spearBodyDef.position.Set(100/PTM_RATIO, 100/PTM_RATIO);
- spearBodyDef.userData = _spear;
- b2Body *_spearBody = _world->CreateBody(&spearBodyDef);
- b2PolygonShape spearShape;
- spearShape.SetAsBox(100/PTM_RATIO, 10/PTM_RATIO);
- b2FixtureDef spearShapeDef;
- spearShapeDef.shape = &spearShape;
- spearShapeDef.density = 1.0f;
- spearShapeDef.friction = 0.9f;
- spearShapeDef.restitution = 0.89f;
- b2Vec2 force;
- force.Set(_spearBody->GetLinearVelocity().x+5.0f, _spearBody->GetLinearVelocity().y+10.0f);
- _spearBody->SetLinearVelocity(force);
- [self schedule:@selector(tick:)];
- }
- return self;
- }
- - (void)tick:(ccTime) dt {
- _world->Step(dt, 10, 10);
- for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
- if (b->GetUserData() != NULL) {
- CCSprite *ballData = (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) dealloc {
- delete _world;
- _world = NULL;
- [super dealloc];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement