Note that there are some explanatory texts on larger screens.

plurals
  1. POadding CCPhysicsSprite to CCLayer cocos2d
    primarykey
    data
    text
    <p>I'm trying to reorganize HelloWorld project in cocos2d to our needs. The thing I did - made a class, which is inherent from <code>CCPhysicsSprite</code> and wanted to add it to <code>CCLayer</code> (<code>HelloWorldLayer</code>). But something goes wrong. According to debugger my instance is created, but I can't see it in the iOS emulator. Need your help and explanations.</p> <p>HelloWorldLayer.h</p> <pre><code>@interface HelloWorldLayer : CCLayer &lt;GKAchievementViewControllerDelegate, GKLeaderboardViewControllerDelegate&gt; { CCTexture2D *spriteTexture_; // weak ref b2World* world_; // strong ref GLESDebugDraw *m_debugDraw; // strong ref } </code></pre> <p>HelloWorldLayer.mm (only changed by me functions:)</p> <pre><code>-(id) init { if( (self=[super init])) { // enable events self.touchEnabled = YES; self.accelerometerEnabled = YES; CGSize s = [CCDirector sharedDirector].winSize; // init physics [self initPhysics]; // create reset button //[self createMenu]; //Set up sprite //#if 1 // // Use batch node. Faster // CCSpriteBatchNode *parent = [CCSpriteBatchNode batchNodeWithFile:@"blocks.png" capacity:100]; // spriteTexture_ = [parent texture]; //#else // // doesn't use batch node. Slower // spriteTexture_ = [[CCTextureCache sharedTextureCache] addImage:@"blocks.png"]; // CCNode *parent = [CCNode node]; //#endif // [self addChild:parent z:0 tag:kTagParentNode]; // // // [self addNewSpriteAtPosition:ccp(s.width/2, s.height/2)]; CCLabelTTF *label = [CCLabelTTF labelWithString:@"Tap screen" fontName:@"Marker Felt" fontSize:32]; [self addChild:label z:0]; [label setColor:ccc3(0,0,255)]; label.position = ccp( s.width/2, s.height-50); [self scheduleUpdate]; } return self; } -(void) addNewSpriteAtPosition:(CGPoint)p { CCLOG(@"Add sprite %0.2f x %02.f",p.x,p.y); if([self getChildByTag:kTagParentNode] == nil) { BloodRobotUnit *unit = [[BloodRobotUnit alloc] initWithOwner:world_ at:p]; [self addChild:unit z:0 tag:kTagParentNode]; } } </code></pre> <p>And Creating unit: (header and mm file:)</p> <pre><code>@interface BloodRobotUnit : CCPhysicsSprite { b2Body *body_; b2World *owner_; } -(id) initWithOwner:(b2World*)owner at:(CGPoint)pt; </code></pre> <p>mm:</p> <pre><code>-(id) initWithOwner:(b2World*)owner at:(CGPoint)pt { if(self = [super initWithFile:@"blocks.png" rect:CGRectMake(0, 0, 32, 32)]) { owner_ = owner; //create body at position b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(pt.x/PTM_RATIO, pt.y/PTM_RATIO); body_ = owner-&gt;CreateBody(&amp;bodyDef); // Define another box shape for our dynamic body. b2PolygonShape dynamicBox; dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box // Define the dynamic body fixture. b2FixtureDef fixtureDef; fixtureDef.shape = &amp;dynamicBox; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body_-&gt;CreateFixture(&amp;fixtureDef); [self setB2Body: body_]; [self setPosition:pt]; return (self); } return nil; } </code></pre> <p>Where is my mistake? Any help will be very appreciated</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload