Box2D Forums

It is currently Thu May 23, 2013 10:27 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Jul 17, 2011 6:15 am 
Offline

Joined: Sun Jul 17, 2011 5:54 am
Posts: 1
Hello Community. :)
im hoping i can get some help here.
im tryin to get a dynamic sprite in my box 2d world, so i use vextex helper to draw the vertices.
I can get the body/ vertex to show but i cant just get the sprite to show.

gonna try to show some code but dont wanna flood so please ask if there's something else you need to see

myplayer.mm
To create the player

Code:
-(void) createPlayerInWorld:(b2World*)world
{
   CGSize screenSize = [[CCDirector sharedDirector] winSize];
   float randomOffset = CCRANDOM_0_1() * 10.0f - 5.0f;
   CGPoint startPos = CGPointMake(screenSize.width - 15 + randomOffset, 80);
   
   // Create a body definition and set it to be a dynamic body
   b2BodyDef bodyDef;
   bodyDef.type = b2_dynamicBody;
   bodyDef.position=[Helper toMeters:startPos];
   bodyDef.angularDamping = 10.9f;
   
   NSString* spriteFrameName = @"knife.png";
   CCSprite* tempSprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
   tempSprite.tag=1;
   
   b2PolygonShape shape;
   //float radiusInMeters = (tempSprite.contentSize.width / PTM_RATIO) * 0.5f;
   //shape.m_radius = radiusInMeters;
   //shape.SetAsBox(0.5f, 0.5f);
   if (tempSprite.tag == 1) {
      int num = 4;
      b2Vec2 verts[] = {
         b2Vec2(-14.2f / PTM_RATIO, 14.2f / PTM_RATIO),
         b2Vec2(-15.0f / PTM_RATIO, -13.6f / PTM_RATIO),
         b2Vec2(14.6f / PTM_RATIO, -14.7f / PTM_RATIO),
         b2Vec2(14.6f / PTM_RATIO, 15.1f / PTM_RATIO)
      };
      shape.Set(verts, num);
    }
   // Define the dynamic body fixture.
   b2FixtureDef fixtureDef;
   fixtureDef.shape = &shape;
   fixtureDef.density = 0.8f;
   fixtureDef.friction = 0.7f;
   fixtureDef.restitution = 0.3f;
   
   [super createBodyInWorld:world bodyDef:&bodyDef fixtureDef:&fixtureDef spriteFrameName:spriteFrameName];

}


setup.mm
Code:
-(id) initWorldWithWorld:(b2World*)world
{   
   if ((self=[super init]))
   {      float randomOffset = CCRANDOM_0_1() * 10.0f - 5.0f;
      CGSize screenSize = [[CCDirector sharedDirector] winSize];
      CGPoint startPos = CGPointMake(screenSize.width - 15 + randomOffset, 80);
      world_=world;
myplayer* Myplayer=[myplayer playerWithWorld:world];
   [self addChild:Myplayer z:-1];
      
      world_=NULL;
   }
   return self;
}


mainscene
Code:
-(id) init
{
   if ((self=[super init]))
   {   CGSize screenSize=[[CCDirector sharedDirector]winSize];
      GameSceneInstance=self;
      [self initBox2dWorld];
      [self enableBox2dDebugDrawing];
   
      [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"texture.plist"];
   
   
      /*CCSprite* batch=[CCSprite spriteWithFile:@"iphoneback2.png"];
      [self addChild:batch z:-2 tag:kTagbackgroud];
       */
      
      // batch node for all dynamic elements
      CCSprite* batch = [CCSprite spriteWithSpriteFrameName:@"knife.png"];
      [self addChild:batch z:-2 tag:kTagBatchNode];
      
      //Static elements
      GameSceneSetup* background=[GameSceneSetup setupWorldWithWorld:world];
      [self addChild:background z:-1];
   }
   return self;
}

Hope it wasnt to much of a mess to read.
so Mainscene is my...mainscene.
and myplayer is the set up for myplayer whose method i pass to gamesetup,like i said the body/ vertices show but just not the sprite.texture


Top
 Profile  
 
PostPosted: Wed Sep 14, 2011 4:30 am 
Offline

Joined: Fri Jun 10, 2011 12:48 pm
Posts: 10
Hi,

You will need to set up your userdata to your sprite.

bodyDef.userData = yoursprite;

Regards,
Steve


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group