Cannot get sprite to show in Box2d

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: Select all

-(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: Select all

-(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: Select all

-(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
Hi,

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

bodyDef.userData = yoursprite;

Regards,
Steve

Original thread: https://box2d.org/forum/viewtopic.php?t=7311