1

我正在尝试根据我们的需要重新组织 cocos2d 中的 HelloWorld 项目。我所做的事情 - 制作了一个类,它是固有的CCPhysicsSprite并希望将其添加到CCLayer( HelloWorldLayer) 中。但是出了点问题。根据调试器,我的实例已创建,但我在 iOS 模拟器中看不到它。需要你的帮助和解释。

HelloWorldLayer.h

@interface HelloWorldLayer : CCLayer <GKAchievementViewControllerDelegate,     GKLeaderboardViewControllerDelegate>
{
    CCTexture2D *spriteTexture_;    // weak ref
    b2World* world_;                    // strong ref
    GLESDebugDraw *m_debugDraw;     // strong ref
}

HelloWorldLayer.mm(仅由我更改函数:)

-(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];
    }
}

并创建单元:(标题和mm文件:)

@interface BloodRobotUnit : CCPhysicsSprite
{

    b2Body *body_;
    b2World *owner_;
}

-(id) initWithOwner:(b2World*)owner at:(CGPoint)pt;

毫米:

-(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->CreateBody(&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 = &dynamicBox;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.3f;
        body_->CreateFixture(&fixtureDef);

        [self setB2Body: body_];
        [self setPosition:pt];
        return (self);

    }
return nil;
}

我的错误在哪里?任何帮助将不胜感激

4

1 回答 1

1

诀窍在于将位置设置为自我调试器显示我的纹理位于位置 inf:inf

使一切正常工作的代码更改如下:

在创建 CCPhysicsSprite iheriter 的 mm 文件中执行以下操作:

[self setB2Body: body_];
self.PTMRatio = PTM_RATIO;
//[self setPosition:CGPointMake(pt.x/PTM_RATIO, pt.y/PTM_RATIO)];

也就是说 - 您只需要设置身体位置并设置 PTMRatio (感谢@giorashc)。不需要设置精灵纹理。

于 2013-08-07T13:08:22.180 回答