문제

I need the following logic.

1. box2d object is flying top of the screen.
2. If make long mouse click, the flying object come down and hit ground.
3. If release the mouse click, then the object move to top.

I did first point using

 body_->SetGravityScale(0);
 body_->SetLinearVelocity(velocity);

How will i move top to bottom and again bottom to top?

Thanks in advance.

도움이 되었습니까?

해결책 2

I'm am going to assume that you actually have something on the ground (like a ground plane) for the body to hit.

First, to detect the long mouse click, and assuming you are using cocos2d-x, you are going to need a way to detect the long mouse click.

For that, you will either need to key off the touch events in your scene or create a class that handles long touches. For that, here are a couple of options:

  1. Use the CCLongPressGestureRecognizer found here.
  2. Use the TapDragPinchInput class found here (with full demonstration of using it in MainScene.cpp).

From what you want, I would favor the second, since it gives a call when the finger goes down and then comes up.

For the rest of it, you can set the velocity based on the mouse events. Or apply forces to the bodies to make them move.

See the demos here and here for information on doing that.

다른 팁

You do the followings:

 bool mainscene::ccTouchBegan(cocos2d::CCTouch *ptouch, cocos2d::CCEvent *pEvent)
 {
      this->schedule(schedule_selector(HelloWorld::longpresscoding));
 }

 void mainscene::ccTouchEnded(cocos2d::CCTouch *ptouch, cocos2d::CCEvent *pEvent) 
 {
      this->unschedule(schedule_selector(HelloWorld::longpresscoding));
 }

 void mainscene::longpresscoding()
 {
     CCLOG("LONG Press");
 }

It will help you.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top