-1

I'm a beginner and started with cocos2d-x. I want to make a clone of the game -

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

It seemed really easy but turns out the logic I have been using at "Update function" (game loop) isn't working. This is my code -

#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());

}

The player movement by accelerometer is fine.
I'm sure, something is wrong in the gameloop. Any kind of help will be appreciated. Thanks in advance !

Also, I'm thinking to switch from cocos2d-x to unity. Will this decision be promising ?

Vineet
  • 47
  • 8
  • Can you be more precise where's the problem? – Makalele Nov 30 '15 at 22:56
  • The player moves on X-axis (left/right) very smoothly. But by using by update method, I want to make it (sprite) unstable. And the unstable movement has to be overcome by user input(accelerometer). I'm facing a problem there in update method. I'm not sure if I'm using the right logic. – Vineet Nov 30 '15 at 22:58
  • 1
    But what's the problem you see when you execute? Or in which way does not do the thing you want? What I see is that every update you change the x 50 times the width of the screen so the character for sure is going out of screen every update I think ;) – Alex G. G. Dec 01 '15 at 12:05
  • The sprite doesn't move automatically. Only moves on player's ontrol. The gameloop doesn't wtk. – Vineet Dec 01 '15 at 13:02
  • @Vineet Dude first of all before putting up your question here you should try to debug your code yourself. Second if your want to just switch to unity just because you can't make this loop work , then that's just illogical. Third you should learn to eleborate your actual issue and precisely define what are you looking for as your answer. In this case you should tell us , Is the update loop is being called, or your logic in the loop is wrong as this update function is called every frame and you are randomly updating its position in every frame so these changes may not be visible to you. – Jain Dec 02 '15 at 04:51
  • @jain - The scenario is - 1. I can move my sprite with accelerometer along x-axis. It works fine. 2. The problem is - If you play the above game - It's game loop makes the sprite unstable. Like it automatically keeps moving towards left or right. As a player, we have to avoid that unstable movement and control the sprite from getting out of screen or falling. The 2. point is my problem. I believe my game loop logic is incorrect. – Vineet Dec 02 '15 at 18:59

1 Answers1

1

The problem is that you are updating the location of the sprite in each frame abruptly, hence the sprite will not be visible at any position. You have to make sprite stay at some point for some time or update its position smoothly (like using MoveTo). Here is a sample. And you should not ask for the solution rather how to solve the problem.

 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())));

}  
Jain
  • 854
  • 5
  • 15
  • Jain, Thank you so much for showing interest and helping me out. And I'll keep in mind your tip regarding - asking for the solution. Though, I was not intending to do that. But your code worked, Though had to make a few changes. Thanks once again ! – Vineet Dec 07 '15 at 12:34
  • @Vineet That's why we all are here :) – Jain Dec 08 '15 at 04:32