Node Body boundary problem

How To Make Flappy Bird
  • MoseHas
    Likes 0

    Problem Description

    Hi there. Got a new question again.

    In the tutorial “Game Scene Background” in Flappy Bird tutorial series, the demo showed me the perfect red boundary line just exactly at the edges of the display window. However, I wrote the same code and my rectangle flew up.

    This is the original one.

    This is the one I manually reset the vaue of the y axis.

     

    And the code as follows.

    #include "GameScene.h"
    
    USING_NS_CC;
    
    Scene* GameScene::createScene()
    {
        // 'scene' is an autorelease object
        auto scene = Scene::createWithPhysics();
    	scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
        
        // 'layer' is an autorelease object
    	auto layer = GameScene::create();
    	layer->SetPhysicsWorld(scene->getPhysicsWorld());
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool GameScene::init()
    {
        //////////////////////////////
        // 1. super init first
        if ( !Layer::init() )
        {
            return false;
        }
        
        Size visibleSize = Director::getInstance()->getVisibleSize();
        Vec2 origin = Director::getInstance()->getVisibleOrigin();
        
    	auto backgroundSprite = Sprite::create("background.jpg");
    	backgroundSprite->setPosition(visibleSize.width / 2 + origin.x, visibleSize.width / 2 + origin.y - 120);
    	backgroundSprite->setScale(0.93);
    	this->addChild(backgroundSprite);
    
    	auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3);
    
    	auto edgeNode = Node::create();
    	edgeNode->setPosition(visibleSize.width / 2 + origin.x, visibleSize.width / 2 + origin.y/* - 160*/);
    
    	edgeNode->setPhysicsBody(edgeBody);
    
    	this->addChild(edgeNode);
    
        return true;
    }

    I have to manually subtract 160 to make it perfectly fit in.

    And when I added the background image I had faced the same problem. My image flew up out of the window as well.

    In the beginning I thought that my image’s resolution might not be correctly fit, so I subtract 120 from the height of y.

    Don’t know what is going on right now. Hope it was not another stupid rookie mistake.

    Thanks. Wish good.

  • Sonar Systems admin
    Likes 0

    That’s crazy weird, could you show us the code in the AppDelegate.cpp please.

  • MoseHas
    Likes 0

    Sure. Here is my AppDelegate.cpp.

    #include "AppDelegate.h"
    #include "SplashScene.h"
    
    USING_NS_CC;
    
    AppDelegate::AppDelegate() {
    
    }
    
    AppDelegate::~AppDelegate() 
    {
    }
    
    //if you want a different context,just modify the value of glContextAttrs
    //it will takes effect on all platforms
    void AppDelegate::initGLContextAttrs()
    {
        //set OpenGL context attributions,now can only set six attributions:
        //red,green,blue,alpha,depth,stencil
        GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
    
        GLView::setGLContextAttrs(glContextAttrs);
    }
    
    // If you want to use packages manager to install more packages, 
    // don't modify or remove this function
    static int register_all_packages()
    {
        return 0; //flag for packages manager
    }
    
    bool AppDelegate::applicationDidFinishLaunching() {
        // initialize director
        auto director = Director::getInstance();
        auto glview = director->getOpenGLView();
        if(!glview) {
            glview = GLViewImpl::create("My Game");
            director->setOpenGLView(glview);
        }
    
        // turn on display FPS
        director->setDisplayStats(true);
    
        // set FPS. the default value is 1.0/60 if you don't call this
        director->setAnimationInterval(1.0 / 60);
    
        register_all_packages();
    
        // create a scene. it's an autorelease object
        auto scene = SplashScene::createScene();
    
        // run
        director->runWithScene(scene);
    
        return true;
    }
    
    // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
    void AppDelegate::applicationDidEnterBackground() {
        Director::getInstance()->stopAnimation();
    
        // if you use SimpleAudioEngine, it must be pause
        // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
    }
    
    // this function will be called when the app is active again
    void AppDelegate::applicationWillEnterForeground() {
        Director::getInstance()->startAnimation();
    
        // if you use SimpleAudioEngine, it must resume here
        // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
    }

    Actually I do not remember I have changed anything except replacing every HelloWorldScene by SplashScene (In tutorial #2: Creating The Scene about 2:50).

    And I have just finished watching Pipe Class Creation. Same problem showed up when I tried to create a Pipe sprite. I have only my topPipe and bottomPipe is gone! I assume it is the same problem as my NodeBody boundary.

     

    Thankks for your replying.

  • MoseHas
    Likes 0

    Ah… awkard things happened.

    I imported the sorce code you provided below the video and it did work correctly.

    Get so frustrated and still don’t know why.

  • Sonar Systems admin
    Likes 0

    so is it working and you are happy?

  • MoseHas
    Likes 0

    Working correctly but not so happy though.

    Wanna understand why strange things happened.

    Well, someday I will have to handle a program my own, so I try my best to prevent bugs.

    Well, thanks anyway.


    This reply has been verified.
  • Sonar Systems admin
    Likes 0

    That’s programming for you :D

Login to reply