使用box2d跳转精灵触摸

时间:2015-10-15 13:33:52

标签: c++ ios xcode cocos2d-x box2d

我是新的cocos2dx开发人员。我正在使用cocos2dx v3.7.1。现在我想在sprite上跳过精灵。我提到很多答案,但我很困惑。                 我在HelloWorld.cpp文件中实现以下代码

             Scene* HelloWorld::createScene()
            {

              auto scene = Scene::createWithPhysics();
                scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
                 //scene->getPhysicsWorld()->setGravity(Vec2(0,0.0));
                auto layer = HelloWorld::create();
                layer->SetPhysicsWorld( scene->getPhysicsWorld() );
                scene->addChild(layer);
                return scene;
            }

            // on "init" you need to initialize your instance
            bool HelloWorld::init()
            {
                //////////////////////////////
                // 1. super init first
                if ( !Layer::init() )
                {
                    return false;
                }
                auto dispatcher = Director::getInstance()->getEventDispatcher();
                auto touchListener = EventListenerTouchOneByOne::create();
                touchListener->setSwallowTouches(true);

                touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
                touchListener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
                touchListener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);

                dispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

         sprite3 = Sprite::create("kartoon.png");
            sprite3->setPosition(Point(40,40));
            sprite3->setAnchorPoint(Vec2(0,0));            spritibody=PhysicsBody::createBox(sprite3->getContentSize(),PhysicsMaterial(0,1,0));
                sprite3->setPhysicsBody( spritibody );
                spritibody->setDynamic(true);
               this->addChild(sprite3,4);
   this->schedule(SEL_SCHEDULE(&HelloWorld::chek),0.01f);
            return true;
           }
 void HelloWorld::chek()
   {

                if (sprite3->getPosition().y>109) {
                    yvel-=0.1;
                }
                else{

                    if(yvel!=6)
                    {
                        xvel=0;
                        yvel=0;
                    }
                }
                sprite3->setPosition(Vec2(sprite3->getPosition().x+xvel,sprite3->getPosition().y+yvel));

            }
    bool HelloWorld::onTouchBegan(Touch* touch, Event* event)
    {
        CCLOG("onTouchBegan x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
        auto target = static_cast<Sprite*>(event->getCurrentTarget());
        Point locationInNode = target->convertToNodeSpace(touch->getLocation());
        if(sprite3->boundingBox().containsPoint(locationInNode))
        {

            // spritibody->setVelocity(Vec2(2, 0));
         spritibody->applyImpulse(Vec2(0,40));
            sprite3->setPhysicsBody(spritibody);
           spritibody->setGravityEnable(true);
            }
      return true;

    }

当我点击精灵时,它的跳跃连续并且在某种类型的sprite3离开屏幕之后。但是我想在我点击sprite3时只跳一次。 请帮助我。

0 个答案:

没有答案