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

  • Home
  • SEARCH
  • 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 6723141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:32:00+00:00 2026-05-26T09:32:00+00:00

I have created an array of enemies and when I add an enemy I

  • 0

I have created an array of enemies and when I add an enemy I add the appropriate box2d code, however I have found that none of my enemies can be touched, I am not sure what is causing this but from what I can tell it never returns a fixture.

I have tried setting the user data but then I do not get multiple items.

This is how I add my sprite etc

for (int i = 0; i < EnemyType_MAX; i++)
{
   CCArray* enemiesOfType = [enemies objectAtIndex:i];
   int numEnemiesOfType = [enemiesOfType capacity];

   for (int j = 0; j < numEnemiesOfType; j++)
   {
     EnemyEntity* enemy = [[EnemyEntity alloc]init:_gameScene enemyType:EnemyTypeBreadman];
     [batch addChild:enemy z:0 tag:i];
     [enemiesOfType addObject:enemy];
     [allEnemies addObject:enemy];

     b2BodyDef bodyDef;
     bodyDef.type = b2_dynamicBody;
     bodyDef.position.Set(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
     bodyDef.userData = self;
     b2Body *body = _gameScene.world->CreateBody(&bodyDef);

     b2CircleShape circle;
     circle.m_radius = 26.0/PTM_RATIO;

     // Define the dynamic body fixture.
     b2FixtureDef fixtureDef;
     fixtureDef.shape = &circle;
     fixtureDef.density = 1.0f;
     fixtureDef.friction = 0.3f;
     body->CreateFixture(&fixtureDef);
  }
}

I then use my touch handler to try and return what item has been touched

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    //NSLog(@"ccTouchesBegan %@", (_mouseJoint!= NULL) ? @"YES" : @"FALSE" );
    if (_gameScene.mouseJoint != NULL) return;

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    float move = 0.0f, x1, y1, z1;

    [_gameScene.camera centerX:&x1 centerY:&y1 centerZ:&z1];

    b2Vec2 locationWorld = b2Vec2((location.x+x1)/PTM_RATIO, (location.y+y1)/PTM_RATIO);
    NSLog(@"ccTouchesBegan %@",NSStringFromCGPoint(location));

    b2AABB aabb;
    aabb.lowerBound.Set(-1.0f+locationWorld.x, -1.0f+locationWorld.y);
    aabb.upperBound.Set(1.0f+locationWorld.x, 1.0f+locationWorld.y);
    b2Vec2 callPoint;
    callPoint.Set (locationWorld.x,locationWorld.y);
    QueryCallback callback(callPoint);
    _gameScene.world->QueryAABB(&callback, aabb);

    b2Body* nbody = NULL;
    if (callback.m_fixture)
    {
        nbody= callback.m_fixture->GetBody();
    }

    if (nbody)
    {
        b2BodyDef bodyDef;
        b2Body* groundBody = _gameScene.world->CreateBody(&bodyDef);

        b2MouseJointDef md;
        md.bodyA = groundBody;
        md.bodyB = nbody;
        md.target = locationWorld;

        #ifdef TARGET_FLOAT32_IS_FIXED
            md.maxForce = (nbody->GetMass() < 16.0)? (1000.0f * nbody->GetMass()) : f        loat32(16000.0);
        #else
            md.maxForce = 1000.0f * nbody->GetMass();
        #endif

        _gameScene.mouseJoint = (b2MouseJoint *)_gameScene.world->CreateJoint(&md);
        nbody->SetAwake(true);
    }
}
  • 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-05-26T09:32:01+00:00Added an answer on May 26, 2026 at 9:32 am

    In your init method, right after the if statement, is this in your code:

    if(self = [super init]){
         self.isTouchEnabled = YES;
    

    EDIT——————

    Instead of using ccArray, you should use this:
    CCSprite *_anArray[x];
    

    When I deal with sprites I always put them in a sprite array, I declared it in the header. You also have to do the @property(nonatomic, retain) NSMutableArray *arrowArray; in the .h file and in the .m @synthesize arrowArray = _arrowArray;
    Then I just added all my sprites into that array. Should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an array in php that prints this Array ( [mark] =>
I have an array created with this code: var widthRange = new Array(); widthRange[46]
I am currently trying to pass an array that I have created in Javascript
Explanation I have a multidimensional array that is iterated over to created a categorized
I have JSON array created having code like user_images.push({'href' : value.href, 'title' : value.title
I have created a suffix array using the Princeton implementation. However, my basic text
I have created a dynamic array of buttons that come from sqllite database. For
I have created a custom Array Adapter to bind a custom row that contains
I have created an array of custom objects in my processing code and then
I have created an array of doubles using the following code: double *points =

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.