Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8406801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:12:40+00:00 2026-06-09T23:12:40+00:00

I have written a code to display using CCLog the exact position of a

  • 0

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 sprite position is not updating, the output is constantly x: 0.00 and y: 0.00.

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

Please help. I’ve been at it for a few days.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T23:12:41+00:00Added an answer on June 9, 2026 at 11:12 pm

    I finally managed to get it working. I guess the x:0.00 and y:0.00 were because I was taking the position of the sprite not the body. The sprite is parented to the body so it was giving its position inside it’s parent, which is 0,0. This is how I understand it. Here are the changes I made to the ccTouchesEnded code.

    ccTouchesEnded:

    - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
    
    if (mouseJoint)
    {
        for(b2Body *b = mouseJoint->GetBodyB(); b; b=b->GetNext())    {
            if (b->GetUserData() != NULL)
            {
                CCSprite *mySprite = (CCSprite*)b->GetUserData();
    
                if (mySprite.tag == 1) {
                    mySprite.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
                    CCLOG(@"the sprite postion is x:%0.2f , y:%0.2f", mySprite.position.x, mySprite.position.y);
    
                }
            }
        }
    
        world->DestroyJoint(mouseJoint);
        mouseJoint = NULL;
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a simple code to display an image using Jsp XHTML template
I have written some code to display a YouTube video on my page but
I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
I have written some code that loads an XML document using an XmlDocument object
I am using InfoPath 2007 I have some code written in the form that
I am using ImageMagick for Perl use Image::Magick; I have written functional code to
I have written code to upload the files of different types using php, and
I have written code that opens 16 figures at once. Currently, they all open
I have written code in Java to access web cam,and to save image... I
I have written code to perform a function that could take a while to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.