読者です 読者をやめる 読者になる 読者になる

shanon's note

プログラミングとアニメがすき

cocos2dx 3.0 上下左右に壁を作った #cocos2dx

上下左右に壁を作りました。

    //床
    Sprite* floor = Sprite::create();
    floor->setPosition(visibleSize.width /2, 0);
    floor->setTextureRect(Rect(0, 0, 1136, 50));
    floor->setColor(Color3B(255, 255, 255));
    floor->setRotation(0.0f);
    
    PhysicsBody* pFloor = PhysicsBody::createBox(floor->getContentSize());
    pFloor->setDynamic(false);
    floor->setPhysicsBody(pFloor);
    layer->addChild(floor);
    
    //天井
    Sprite* roof = Sprite::create();
    roof->setPosition(visibleSize.width /2, visibleSize.height);
    roof->setTextureRect(Rect(0, 640, 1136, 0));
    
    PhysicsBody* pRoof = PhysicsBody::createBox(roof->getContentSize());
    pRoof->setDynamic(false);
    roof->setPhysicsBody(pRoof);
    layer->addChild(roof);
    
    //右壁
    Sprite* rightWall = Sprite::create();
    rightWall->setPosition(visibleSize.width, visibleSize.height /2);
    rightWall->setTextureRect(Rect(1136, 0, 50, 640));
    
    PhysicsBody* pRightWall = PhysicsBody::createBox(rightWall->getContentSize());
    pRightWall->setDynamic(false);
    rightWall->setPhysicsBody(pRightWall);
    layer->addChild(rightWall);
    
    //左壁
    Sprite* leftWall = Sprite::create();
    leftWall->setPosition(0, visibleSize.height /2);
    leftWall->setTextureRect(Rect(50, 0, 50, 640));
    
    PhysicsBody* pLeftWall = PhysicsBody::createBox(leftWall->getContentSize());
    pLeftWall->setDynamic(false);
    leftWall->setPhysicsBody(pLeftWall);
    layer->addChild(leftWall);

こんなんなります。
f:id:shanonim:20140911232314p:plain

(赤くなってるのは、デバッグ用のテクスチャ。↓)

scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

壁が低反発素材すぎるのでもうちょっとガツンと跳ね返ってくる感じに調整したい。