環境
cocos2d-x version3.14.1 (3.8でも試しました
xcode8.2
iphone7
事象
親Node
∟ 子Sprite1
∟ 子Sprite2
∟ 子Sprite3
こんな感じの親子関係で、子Spriteにlistnerでタッチイベントをもたせ、TouchBegan,TouchMoved,TouchEndedでドラッグ&ドロップできるようにしており、
ドラッグ中に親Node->setPosition(touch->getLocation)で移動しています。
ですが、setPosition()で移動しても、Spriteの描画はちゃんと移動するのですが、
getBoundingBox()の座標が移動しません。
こちらsetPositionで移動する際に子SpriteのgetBoundingBoxの座標も変更されるようにしたいです。
また、BoundingBoxを変更するために、getBoundingBox().setRect(x,y,w,h)を指定しても、
変更されませんでした。
解決したい点としては、
子SpriteのgetBoundingBoxの座標を親NodeのsetPositionで一緒に移動できるように
もしくは、getBoundingBox().setRect()でちゃんと判定領域の指定が反映されるように
できればと思っております。
- ちなみに、普通に親子関係をつくらずSprite単体だと、setPositionでいどうすると、getBoundingBox()の座標も移動されました。
- また、移動後にlistnerでイベント登録をし直しましたが解決しませんでした。
- cocos2dxのversion3.8 version3.14.1で試しました。
- 親Sprite子Spriteでも試しました。
- 絶対や相対座標でも何度か試行錯誤したが変わりませんでした。
cpp
1// タッチ開始 2bool GamePlayFits::onTouchBegan2(cocos2d::Touch *touch, cocos2d::Event *event) { 3 4 5 6 auto target = (Sprite*)event->getCurrentTarget(); //target : ターゲットのスプライト 7 Rect targetBox = target->getBoundingBox(); //targetBox : タッチされたスプライトの領域 8 9 Point touchPoint = touch->getLocation(); 10 11 12 // そもそもBoundingBoxを使わず座標計算で当たり判定した方がいい? 13 if (isDrag == false && targetBox.containsPoint(touchPoint)) //touchPointがtargetBoxの中に含まれているか判定 14 { 15 isDrag = true; 16 17 log("hogeタップ!! %s : %s(%d) -------> %s", "ActorSprite", "onTouchBegan", __LINE__,target->getName().c_str()); 18 19 ValueVector nameParse = this->split(target->getName(), "_"); 20 log("nameParse: %s",nameParse.at(2).asString().c_str()); 21 22 string nodeNamePrefix = "Node_Block_"; // block 23 nodeNamePrefix += std::to_string(nameParse.at(2).asInt()); 24 25 selectSpriteName = nodeNamePrefix; 26 selectNodeAccess = (Sprite*)target->getParent(); 27 selectSpriteAccess = target; 28// selectSpriteBoundingBox = target->getBoundingBox(); 29 30 hitFlg = true; 31 32 33 } 34 35 return true; //falseだと、onTouchBeganでイベントが終了してonTouchEndedが呼ばれない 36} 37 38 39 40// ドラッグ中 41void SceneGamePlayFits::onTouchMoved2(cocos2d::Touch *touch, cocos2d::Event *event) { 42 43 44 if(hitFlg == true && isDrag == true){ 45 46 47 48 // タップしたスプライトと親nodeの座標を計算し、ドラッグ位置にスプライトがくるように 49 Point touchPoint = Vec2( 50 touch->getLocation().x - selectSpriteAccess->getPositionX(), 51 touch->getLocation().y - selectSpriteAccess->getPositionY() 52 ); 53 54 selectNodeAccess->setPosition(touchPoint); 55 56 57 58 59 // 親Nodeに紐づくSpriteの座標確認debug 60 for(auto spriteBlock:selectNodeAccess->getChildren()){ 61 62 // なぜかBoundingBoxの座標だけ、setPositionで移動しても変更されない 63 spriteBlock->getBoundingBox().setRect( 64 spriteBlock->getPositionX(), 65 spriteBlock->getPositionY(), 66 spriteBlock->getBoundingBox().getMaxX() - spriteBlock->getBoundingBox().getMinX(), 67 spriteBlock->getBoundingBox().getMaxY() - spriteBlock->getBoundingBox().getMinY() 68 ); 69 70 71 } 72 73 } 74 75 return; 76} 77
参考情報:
おそらく同じ事象、、cocos2dxの不具合なんでしょうか。。
http://www.cocoachina.com/bbs/read.php?tid-176727.html
あなたの回答
tips
プレビュー