1.Cocos2d-x的几个核心类可以参照这个:https://blog.csdn.net/blackzhangwei/article/details/82555495
一般在AppDelegate::applicationDidFinishLaunching()中会对Director进行配置,包括分辨率设置,帧数等。
2.触摸检测 我写过的代码,可以参照一下
auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(GameScene::onContactBegin, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this); bool GameScene::onContactBegin(const PhysicsContact & contact) { auto target = contact.getShapeA()->getBody()->getNode(); if (target->getTag() == STAR) { gameMap->moveNode(target); } else if (target->getTag() == NPC && target->getPositionY() + target->getContentSize().height / 2 < player->getPositionY()) { gameMap->moveNode(target); addScore(150); } else if (target->getTag() == NPC && target->getPositionY() + target->getContentSize().height / 2 >= player->getPositionY()) gameOver(); else if (target->getTag() == TOOL) { jumpTotal = 3; auto toolIcon = Sprite::create("accelerate_state.png"); toolIcon->setPosition(Point(visibleOrigin.x + 180, visibleOrigin.y + 50)); this->addChild(toolIcon, 2); target->removeFromParent(); addScore(300); } else { jumpTimes = 0; } //Turn back to running if (player->playerState == JUMP) { player->run(); } return true; }3.触摸检测,也就是点击的功能,例子如下:
auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan, this); touchListener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); bool GameScene::onTouchBegan(Touch * touch, Event * event) { auto touchPoint = touch->getLocation(); if (slideBtn->getBoundingBox().containsPoint(touchPoint)) { slideBtn->setTexture(slideBtnTextures.at(1)); player->slide(); } if (jumpTimes < jumpTotal && jumpBtn->getBoundingBox().containsPoint(touchPoint)) { if (isSound) SimpleAudioEngine::getInstance()->playEffect("jump.wav"); jumpBtn->setTexture(jumpBtnTextures.at(1)); player->jump(); jumpTimes++; } return true; } void GameScene::onTouchEnded(Touch * touch, Event * event) { auto touchPoint = touch->getLocation(); if (slideBtn->getBoundingBox().containsPoint(touchPoint)) { slideBtn->setTexture(slideBtnTextures.at(0)); player->run(); } if (jumpBtn->getBoundingBox().containsPoint(touchPoint)) { jumpBtn->setTexture(jumpBtnTextures.at(0)); } }