objective c - adding CCPhysicsSprite to CCLayer cocos2d -
i'm trying reorganize helloworld project in cocos2d our needs. thing did - made class, inherent ccphysicssprite
, wanted add cclayer
(helloworldlayer
). goes wrong. according debugger instance created, can't see in ios emulator. need , explanations.
helloworldlayer.h
@interface helloworldlayer : cclayer <gkachievementviewcontrollerdelegate, gkleaderboardviewcontrollerdelegate> { cctexture2d *spritetexture_; // weak ref b2world* world_; // strong ref glesdebugdraw *m_debugdraw; // strong ref }
helloworldlayer.mm (only changed me functions:)
-(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 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]; } }
and creating unit: (header , mm file:)
@interface bloodrobotunit : ccphysicssprite { b2body *body_; b2world *owner_; } -(id) initwithowner:(b2world*)owner at:(cgpoint)pt;
mm:
-(id) initwithowner:(b2world*)owner at:(cgpoint)pt { if(self = [super initwithfile:@"blocks.png" rect:cgrectmake(0, 0, 32, 32)]) { owner_ = owner; //create body @ position b2bodydef bodydef; bodydef.type = b2_dynamicbody; bodydef.position.set(pt.x/ptm_ratio, pt.y/ptm_ratio); body_ = owner->createbody(&bodydef); // define box shape our dynamic body. b2polygonshape dynamicbox; dynamicbox.setasbox(.5f, .5f);//these mid points our 1m box // define dynamic body fixture. b2fixturedef fixturedef; fixturedef.shape = &dynamicbox; fixturedef.density = 1.0f; fixturedef.friction = 0.3f; body_->createfixture(&fixturedef); [self setb2body: body_]; [self setposition:pt]; return (self); } return nil; }
where mistake? appreciated
the trick in setting position self debugger showed texture @ position inf:inf
code change make work following:
in mm file of creating ccphysicssprite iheriter following:
[self setb2body: body_]; self.ptmratio = ptm_ratio; //[self setposition:cgpointmake(pt.x/ptm_ratio, pt.y/ptm_ratio)];
that - need set body position , set ptmratio (thanx @giorashc). setting sprite texture not necessary.
Comments
Post a Comment