bool SceneFlyGame::init() { Scene::initWithPhysics(); // 显示边框线 //this->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); // 返回按钮 Node * back = TBack::create(); addChild(back); back->setLocalZOrder(100); // 创建一个鸟 Sprite * bird = Sprite::create(); m_bird = bird; bird->setContentSize(Size(16, 16)); addChild(bird); bird->setPosition(winSize.width / 4, winSize.height / 2); bird->setScale(2, 1); Texture2D * texture = Director::getInstance()->getTextureCache()->addImage("flyFishRight.png"); Vector<SpriteFrame *> frames; for (int i = 0; i < 6; i++) { SpriteFrame * frame = SpriteFrame::createWithTexture(texture, Rect(i * 16, 0, 16, 16)); frames.pushBack(frame); } Animation * animation = Animation::createWithSpriteFrames(frames, .1f); Animate * animate = Animate::create(animation); bird->runAction(RepeatForever::create(animate)); // 给鸟加一个body Size size = bird->getBoundingBox().size; Point pts[] = { Point(-size.width / 2, -size.height / 2), //Point(-size.width / 2, size.height / 2), Point(-size.width / 2, 0), Point(0, size.height / 2), Point(size.width / 2, size.height / 2), Point(size.width / 2, -size.height / 2) }; PhysicsBody * body = PhysicsBody::createPolygon(pts, 5); bird->setPhysicsBody(body); body->setGroup(1); body->setContactTestBitmask(1); // 碰撞标志位 // 添加边框 { PhysicsBody * body = PhysicsBody::createEdgeBox(winSize, PhysicsMaterial(1.0f, 1.0f, 1.0f)); Sprite * sprite = Sprite::create(); addChild(sprite); sprite->setPhysicsBody(body); sprite->setPosition(winSize.width / 2, winSize.height / 2); body->setContactTestBitmask(1); } // 鸟上升 auto ev = EventListenerTouchOneByOne::create(); ev->onTouchBegan = [&](Touch*, Event *){ m_press = true; return true; }; ev->onTouchEnded = [&](Touch*, Event *){ m_press = false; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(ev, this); m_press = false; schedule(schedule_selector(SceneFlyGame::fly)); //碰撞检测 auto evCollision = EventListenerPhysicsContactWithGroup::create(1); evCollision->onContactBegin = [&](PhysicsContact & contact){ m_dead = true; // 去除连续的碰撞检测 m_bird->getPhysicsBody()->setGroup(0); return true; }; m_dead = false; _eventDispatcher->addEventListenerWithSceneGraphPriority(evCollision, this); schedule(schedule_selector(SceneFlyGame::addBlock), 0.5f); schedule(schedule_selector(SceneFlyGame::moveBlock)); return true; }