我正在尝试制作游戏的克隆只是为了练习。但我无法放置正确的游戏循环

I'm trying to make a clone of game just for practice. But I'm not able to put the right gameloop

我是初学者,从 cocos2d-x 开始。我想克隆游戏 -

https://play.google.com/store/apps/details?id=com.addictivegames.angryalcohol&hl=en

这看起来真的很简单,但事实证明我在 "Update function"(游戏循环)中使用的逻辑不起作用。 这是我的代码 -

#include "MainGameScene.h"
#include "MainMenuScene.h"
#include "GameOverScene.h"
#include "Constants.h"

USING_NS_CC;

Scene* MainGameScene::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer  = MainGameScene::create();
    auto layer2 = MainGameScene::create();


    // add layer as a child to scene
    scene->addChild(layer);
    scene->addChild(layer2); 

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool MainGameScene::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize  = Director::getInstance()->getVisibleSize();
    Vec2 origin        = Director::getInstance()->getVisibleOrigin();

     // this will now place the sprite in the middle of the viewport
sprite = Sprite::create("top.png");
sprite->setPosition(Point( visibleSize.width/2 + origin.x, visibleSize.height/3 + origin.y));
sprite->setAnchorPoint( Vec2( 0.5, 0 ));
this->addChild( sprite );

     //Accelerometer Setup here...
   Device::setAccelerometerEnabled(true);
   auto acclistener = EventListenerAcceleration::create(CC_CALLBACK_2(MainGameScene::onAcceleration, this));
   Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(acclistener, this);

   this->scheduleUpdate();

    return true;
}

void MainGameScene::onAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *event) {

    //Position constraints
    Size winSize         = Director::getInstance()->getWinSize(); 

    float rotX        = sprite->getRotation();
    float w           = winSize.width ;
    float h           = winSize.height;
    float center      = winSize.width/2 ;
     currentPos  = sprite->getPositionX();
    float   y         = sprite->getPositionY();
    float spriteWidth = sprite->getContentSize().width;
    //Filters for acceleration
    float sensitivity       = 0.3;
    float kFilteringFactor  = 0.4;



     //Accelerometer To control natural movement (GAME_LOOP_CONTROL)
      float accelX = ( acc->x * kFilteringFactor ) + ( accelX * (1.0 - kFilteringFactor ));

      rotX         = accelX - acc->x ; 
      currentPos   +=  acc->x * w * sensitivity;



    sprite->setPosition(currentPos, y);
    //sprite->setRotationX(rotX);


}


void MainGameScene::update( float dt ){

Size winSize      = Director::getInstance()->getWinSize(); 
 float w           = winSize.width ;
if (currentPos > winSize.width/2){ // IF on right side from middle

currentPos +=  50 * w ;

} else if (currentPos < winSize.width/2 ){ // IF on left side from middle

currentPos -=  50 * w  ;    

}   



sprite->setPosition(currentPos, sprite->getPositionY());

}

加速度计的玩家移动没问题。
我敢肯定,游戏循环中出了点问题。任何形式的帮助将不胜感激。提前致谢!

此外,我正在考虑从 cocos2d-x 切换到 unity。这个决定会有希望吗?

问题是你在每一帧突然更新精灵的位置,因此精灵在任何位置都不可见。你必须让精灵在某个点停留一段时间或平滑地更新它的位置(比如使用 MoveTo)。这是一个示例。你不应该问解决方案,而是如何解决问题。

 bool MainGameScene::init()
    {
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize  = Director::getInstance()->getVisibleSize();
    Vec2 origin        = Director::getInstance()->getVisibleOrigin();

     // this will now place the sprite in the middle of the viewport
sprite = Sprite::create("top.png");
sprite->setPosition(Point( visibleSize.width/2 + origin.x, visibleSize.height/3 + origin.y));
sprite->setAnchorPoint( Vec2( 0.5, 0 ));
this->addChild( sprite );

     //Accelerometer Setup here...
   Device::setAccelerometerEnabled(true);
   auto acclistener = EventListenerAcceleration::create(CC_CALLBACK_2(MainGameScene::onAcceleration, this));
   Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(acclistener, this);

   //this->scheduleUpdate();
schedule(schedule_selector(MainGameScene::gameUpdate),2);
    return true;
}
void MainGameScene::gameUpdate( float dt ){

Size winSize      = Director::getInstance()->getWinSize(); 
 float w           = winSize.width ;
if (currentPos > winSize.width/2){ // IF on right side from middle

currentPos +=  50 * w ;

} else if (currentPos < winSize.width/2 ){ // IF on left side from middle

currentPos -=  50 * w  ;    
float moveDuration=2;//Change this according to difficulty
        sprite->runAction(MoveTo::create(moveDuration, Vec2(currentPos, sprite->getPositionY())));

}