From d55cd37d8876816e77f141ae5f5f241aaecfe710 Mon Sep 17 00:00:00 2001 From: headrokoko Date: Sat, 3 Jan 2015 01:01:24 +0900 Subject: [PATCH] =?UTF-8?q?20150103=20=E8=A1=9D=E7=AA=81=E6=99=82=E3=81=AB?= =?UTF-8?q?=E3=82=B9=E3=83=97=E3=83=A9=E3=82=A4=E3=83=88=E3=82=92=E6=B6=88?= =?UTF-8?q?=E3=81=99=E3=81=93=E3=81=A8=E3=81=8C=E3=81=A7=E3=81=8D=E3=81=9F?= =?UTF-8?q?=E3=81=8CBody=E3=81=8C=E6=B6=88=E3=81=9B=E3=81=AA=E3=81=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Classes/HelloWorldScene.cpp | 44 +++++++++++++++++++++++++++++-------- Classes/HelloWorldScene.h | 2 +- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/Classes/HelloWorldScene.cpp b/Classes/HelloWorldScene.cpp index e7d7670..6b8fa75 100644 --- a/Classes/HelloWorldScene.cpp +++ b/Classes/HelloWorldScene.cpp @@ -87,6 +87,7 @@ void HelloWorld::CreateBomb(CCPoint point) { PhysiSprite* BombSprite = new PhysiSprite(); BombSprite->autorelease(); + BombSprite->setTag(TAG_BOMB); BombSprite->initWithFile("Bomb.png"); BombSprite->setPosition(point); this->addChild(BombSprite); @@ -120,17 +121,16 @@ void HelloWorld::CreateEnemy(float dt) { PhysiSprite* EnemySprite = new PhysiSprite(); EnemySprite->autorelease(); + EnemySprite->setTag(TAG_ENEMY); EnemySprite->initWithFile("Player.png"); - - - float posX = (float)(rand()%(95-10+1)+10) / 100 ; - + EnemySprite->setPosition(ccp(0,0)); this->addChild(EnemySprite); b2BodyDef EnemyBodyDef; EnemyBodyDef.type = b2_dynamicBody; + float posX = (float)(rand()%(95-10+1)+10) / 100 ; EnemyBodyDef.position.Set(ScreenSize.width * posX / PTM_RATIO, ScreenSize.height * 0.1f / PTM_RATIO); - EnemyBodyDef.userData = (void*)TAG_ENEMY; + EnemyBodyDef.userData = EnemySprite; enemyBody = _world->CreateBody(&EnemyBodyDef); @@ -188,23 +188,49 @@ void HelloWorld::CreatePlayer(CCPoint point) void HelloWorld::BeginContact(b2Contact* contact) { //接触したオブジェクトのuserdataを取得 - CCSprite* SpriteA = (CCSprite*)contact->GetFixtureA()->GetUserData(); - PhysiSprite* SpriteB = (PhysiSprite*)contact->GetFixtureB()->GetUserData(); + PhysiSprite* SpriteA = (PhysiSprite*)contact->GetFixtureA()->GetBody()->GetUserData(); + PhysiSprite* SpriteB = (PhysiSprite*)contact->GetFixtureB()->GetBody()->GetUserData(); + + //AとBのオブジェクトのタグを取得 + int TagA = SpriteA->getTag(); + int TagB = SpriteB->getTag(); //接触したオブジェクトのbodyを取得 b2Body* BodyA = contact->GetFixtureA()->GetBody(); b2Body* BodyB = contact->GetFixtureB()->GetBody(); + //タグがEnemyの場合削除 + if(TagA == TAG_ENEMY) + { + SpriteA->setVisible(false); + + } + else if(TagB == TAG_ENEMY) + { + SpriteB->setVisible(false); + } + + //タグがBombの場合削除 + if(TagA == TAG_BOMB) + { + SpriteA->setVisible(false); + + } + else if(TagB == TAG_BOMB) + { + SpriteB->setVisible(false); + } + //A側の非表示処理 //SpriteA->setVisible(false); //SpriteA->removeFromParentAndCleanup(true); - _world->DestroyBody(BodyA); + //_world->DestroyBody(BodyA); //B側の非表示処理 //SpriteB->setVisible(false); //SpriteB->removeFromParentAndCleanup(true); - _world->DestroyBody(BodyB); + //_world->DestroyBody(BodyB); } //物理初期化 diff --git a/Classes/HelloWorldScene.h b/Classes/HelloWorldScene.h index 075c3ea..e0bcb55 100644 --- a/Classes/HelloWorldScene.h +++ b/Classes/HelloWorldScene.h @@ -26,7 +26,7 @@ class HelloWorld : public cocos2d::CCLayer, public b2ContactListener virtual void CreateBackground(); //背景作成メソッド virtual void CreatePlayer(CCPoint point); //プレイヤー作成メソッド - void CreateEnemy(float dt); //Enemy作成メソッド + virtual void CreateEnemy(float dt); //Enemy作成メソッド virtual void CreateBomb(CCPoint point); //爆弾作成メソッド virtual void BeginContact(b2Contact* contact); //衝突判定メソッド virtual void draw(); //debugDraw作成メソッド