Collision between enemy characters and bullets shot - (Cocos2dx, C++, Android) -


my problem collision detection wrote enemy character objects , bullet objects.

the symtoms:

when 1 enemy character spawns , shoot bullets, enemy character destroyed normal. but, if 2 or more enemy characters spawn , shoot first enemy spawned nothing. last character spawned , shot destroyed without issue.

how tried fix it:

i decided give each enemy character unique id creating int variable , randomize each time enemy spawned. ensures me each enemy character object definately have unique identifier if shoot them, can somehow match unique id bullet.

however, dont know how work around that. code below deletes every enemy character associated randomized tags

my question:

how can set bullet objects hold every unique id of enemy characters, when bullet hits 1 (doesn't matter specific order) search compare unique ids has enemy character id has shot?

apologies if question unclear or if coding style horrible.

gamescene.cpp

/**  * @param enemy_spawn_frequency value 0.002, set make them spawn automatically every few seconds  */ void gamescene::updatescheduler() {     -> scheduleupdate();     -> schedule(schedule_selector(gamescene::spawnenemycharacter),      enemy_spawn_frequency * visiblesize.width); }  // ...  bool gamescene::ontouchbegan(cocos2d::touch* touch, cocos2d::event* event) {      // fire button     auto target = (sprite*)this -> getchildbytag(fire_button_tag);     rect rect   = target        -> getboundingbox();     point p     = touch         -> getlocation();      // initialize bullets     if (rect.containspoint(p)) {         gamescene::spawnbullets(player_tag, playercharacter->getpositionx(), playercharacter->getpositiony()+100, 0, reference_size_y);         return true;     }      // ...      return false; }  // ... somewhere below gamescene.cpp class  void gamescene::spawnbullets(int shooter_id, int x, int y, int p_x, int p_y) {      // load bullet sprites.      spriteframecache::getinstance() -> addspriteframeswithfile("bullets.plist");      // ...       auto bulletbody   = physicsbody::createbox(bullet_sprite -> getcontentsize());     bulletbody       -> setdynamic(true);     bulletbody       -> setgravityenable(false);     bulletbody       -> setcollisionbitmask(bullet_collision_bitmask);     bulletbody       -> setcontacttestbitmask(true);      bullet_sprite    -> setphysicsbody(bulletbody);     bullet_sprite    -> setposition(point( x, y));     bullet_sprite    -> setscale(0.2f);     bullet_sprite    -> settag(bullet_tag);                 -> addchild(bullet_sprite);      // animate bullet object shoots     bullet_obj.firebullet(bullet_sprite, p_x, p_y); }  // ... somewhere further down class define enemy characters spawned enemy objects in below method  void gamescene::spawnenemycharacter(float dt) {     int random_tag = rand();        // ensure each enemy character object has unique random id tag     enemycharacter_obj.spawnenemy(this, random_tag);     enemy_tags.push_back(random_tag);  // ... destroy enemy characters if hit bullet:  bool gamescene::oncontactbegin( cocos2d::physicscontact &contact ) {     physicsbody *a = contact.getshapea() -> getbody();     physicsbody *b = contact.getshapeb() -> getbody();      if (bullet_collision_bitmask == a->getcollisionbitmask() && enemy_collision_bitmask == b-> getcollisionbitmask() ||         bullet_collision_bitmask == b->getcollisionbitmask() && enemy_collision_bitmask == a-> getcollisionbitmask() ) {          // delete enemys random tag         (std::vector<int>::iterator itl = enemy_tags.begin(); itl != enemy_tags.end(); ++itl) {             enemycharacter_obj.destroyenemycharacter(this, *itl);         }             -> removechild(bullet_sprite);     }      return true; } 

bullet.cpp

void bullet::firebullet(sprite* bullet_sprite, int x, int y) {     auto bulletaction  = sequence::create( moveby::create(bulletspeed, point(x, y)), callfuncn::create( cc_callback_1(bullet::doremovefromparentandcleanup, this, true)), null);     bullet_sprite     -> runaction(bulletaction); } 

and enemy character class:

void enemycharacter::spawnenemy(cocos2d::layer* layer, int random_tag) {      // データ読み込み     spriteframecache::getinstance() -> addspriteframeswithfile("enemies.plist");      // 敵キャラクター     enemycharacter   = sprite::createwithspriteframename("enemy_1.png");     auto enemycharacterbody  = physicsbody::createbox(enemycharacter -> getcontentsize() / 5);      // ...      auto enemycharacterposition = (random * visiblesize.height ) + ( enemycharacter -> getcontentsize().height / 2);     enemycharacterbody         -> setdynamic(false);     enemycharacterbody         -> setcollisionbitmask(enemy_collision_bitmask);     enemycharacterbody         -> setcontacttestbitmask(true);     enemycharacter             -> setscale(0.3f);     enemycharacter             -> settag(random_tag);     enemycharacter             -> setphysicsbody(enemycharacterbody);     enemycharacter             -> setposition( point( ((random * visiblesize.width) + (enemycharacter->getcontentsize().width / 2)), visiblesize.height ) );     layer                 -> addchild(enemycharacter);      // ... }  // .. further down class   void enemycharacter::destroyenemycharacter(cocos2d::layer* layer, int randomized_tag) {     layer-> removechild(layer -> getchildbytag(randomized_tag)); } 

i ended using box2d in case, adding each object ccarray traverse them in update method. think question specific needs it's won't useful really


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -