Skip to content

Commit

Permalink
Hero 类 bug修复
Browse files Browse the repository at this point in the history
- 修复了 Hero 侧面撞到墙会被弹回来的bug
- 添加了 Hero 的碰撞 Mask
- 设定 Terrain 的弹性为 0
  • Loading branch information
XingMo committed Aug 27, 2018
1 parent 0312e1d commit 527d9ae
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
17 changes: 16 additions & 1 deletion Classes/Hero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,24 @@ bool Hero::init(std::string type) {
void Hero::initPhysicsBody() {
auto body = PhysicsBody::createBox(this->getContentSize());
body->getShape(0)->setRestitution(0.0f);
body->getShape(0)->setFriction(100.0f);
body->getShape(0)->setFriction(0.0f);
body->setCategoryBitmask(2);
body->setContactTestBitmask(1);
body->setCollisionBitmask(1);
body->setRotationEnable(false);
this->setPhysicsBody(body);

EventListenerPhysicsContact* evListener = EventListenerPhysicsContact::create();
evListener->onContactBegin = [this] (PhysicsContact& contact) { return true;};
evListener->onContactPreSolve = [this] (PhysicsContact& contact, PhysicsContactPreSolve& presolve) {
this->actWalk(false, this->m_iWalking);
return true;
};
evListener->onContactSeperate = [this](PhysicsContact& contact){
//this->m_iWall = 0;
this->actWalk(true, this->m_iWalking);
};
tDirector->getEventDispatcher()->addEventListenerWithSceneGraphPriority(evListener, this);
}

bool Hero::initSprite(std::string name) {
Expand Down
11 changes: 9 additions & 2 deletions Classes/Level1Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ bool Level1Scene::init()
ground->initSprite("HelloWorld.png");
ground->setPosition(Director::getInstance()->getVisibleSize().width/2 , 0);
ground->initPhysicsBody();
this->addChild(ground,0,0);
this->addChild(ground);

auto ground2 = Terrain::create();
ground2->initSprite("HelloWorld.png");
ground2->setPosition(ground->getPositionX() + ground->getContentSize().width ,
ground->getPositionY() + ground->getContentSize().height);
ground2->initPhysicsBody();
this->addChild(ground2);

auto hero = Hero::create("Hero");
hero->initSprite("YanMo.png");
Expand All @@ -40,7 +47,7 @@ bool Level1Scene::init()
JoyStick* rocker = JoyStick::create();
rocker->setPosition(Point::ZERO);
rocker->setHero(hero);
addChild(rocker, 99, TAG_ROCKER);
addChild(rocker, 99);

auto spriteBoom = Sprite::create("CloseSelected.png");
auto menuItemBoom = MenuItemSprite::create(spriteBoom, spriteBoom, [=](Ref *){
Expand Down
1 change: 1 addition & 0 deletions Classes/Terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void Terrain::initPhysicsBody() {
auto body = PhysicsBody::createEdgeBox(this->getContentSize());
body->setGravityEnable(false); // Terrain cant afforded by gravity
body->setDynamic(false);
body->getShape(0)->setRestitution(0);
body->setCategoryBitmask(1);
body->setCollisionBitmask(15);
body->setContactTestBitmask(0);
Expand Down

0 comments on commit 527d9ae

Please sign in to comment.