Cocos2D

2 thoughts on “Cocos2D

  1. shaun

    Great tutorials, i’ve learnt a lot from the site. Keep it up.
    I am having an issue with some code I wrote.
    I have written a code to display using CCLog the exact position of a sprite when a mousejoint moving it is released. Below is the Sprite.mm class and the ccTouchesEnded method (which is in the HelloWorldLayer.mm class). The output is constantly giving me x: 0.00 and y: 0.00, I am not sure what I’ve done wrong.

    Sprite.mm:

    -(id)addSprite:(CCLayer *)parentLayer
    inWorld:(b2World *)world
    {
    PhysicsSprite *aSprite = [PhysicsSprite spriteWithFile:@”spriteIm.png”];

    aSprite.tag = 1;
    [parentLayer addChild:aSprite];

    b2BodyDef spriteBodyDef;
    spriteBodyDef.userData = aSprite;
    spriteBodyDef.type = b2_dynamicBody;
    CGSize s = [CCDirector sharedDirector].winSize;
    spriteBodyDef.position = [Convert toMeters:ccp(s.width * 0.25,s.height-400)];
    b2FixtureDef fixtureDef;
    fixtureDef.density = 0.01;
    b2CircleShape circleShape;
    circleShape.m_radius = aSprite.contentSize.width/2 / PTM_RATIO;
    fixtureDef.shape = &circleShape;

    spriteBody = world->CreateBody( &spriteBodyDef );
    spriteFixture = spriteBody->CreateFixture( &fixtureDef );

    [aSprite setPhysicsBody:spriteBody];

    return aSprite;
    }

    ccTouchesEnded:

    – (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {

    if (mouseJoint)
    {
    for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
    if (b->GetUserData() != NULL) {
    CCSprite *mySprite = (CCSprite *)b->GetUserData();
    if (mySprite.tag == 1) {
    CGPoint spritePosition = mySprite.position;
    CCLOG(@”the sprite position is x:%0.2f, y:%0.2f”, spritePosition.x, spritePosition.y);
    }
    }
    }

    world->DestroyJoint(mouseJoint);
    mouseJoint = NULL;
    }
    }

    I have a feeling that the issue is with the way I am accessing the tag, not really sure. Please help. Thanks in advance

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>