How to move a sprite across the screen back-and-forth in Cocos2d-x ?

  • iam777
    Likes 1

    Problem Description

    This is a 2d game, which contains moving platforms that are spawned randomly along the X axis. I need to move them across the screen horizontally, but when they hit the edge, they will move in opposite direction. And, to make the game less predictable, I would like to start moving the sprite in the direction that is far away of the edge.

    How can I accomplish this effect in Cocos2d-x C++ ?

  • Frahaan admin
    Likes 1

    Hello

     

    Do something like this 

    MoveTo *platformAction = MoveTo::create( 2, Vec2( 0 + ( platformWidth / 2 ), platformPosY ) );
    
    MoveTo *platformAction2 = MoveTo::create( 2, Vec2( screenWidth - ( platformWidth / 2 ), platformPosY ) ); 
    
    
    Sequence *sequenceAction = Sequence::create( platformAction, platformAction2, NULL );
    RepeatForever *repeatForeverAction = RepeatForever::create( sequenceAction );
    platform->runAction( repeatForeverAction );
    

    When you get this working let us know and we can make it less predictable.

     

    Hope this helps

     

    Regards

    Frahaan Hussain – CEO of Sonar Systems

  • iam777
    Likes 1

    Dear Frahaan, I am so grateful with your help. I already tested the code, and it is working, but I am having a weird effect. My platform is increasing the speed by itself and then it desapears from the screen. It looks like the platform is been controlled by the DELTA time. How can I fix it?

    Thanks again.

     

  • Frahaan admin
    Likes 0

    Hello

     

    Send us a video of what is happening so we can get a better understanding.

     

    Regards 

    Frahaan Hussain - CEO of Sonar Systems 


    This reply has been verified.
  • iam777
    Likes 1

    I got it working, I just moved the code code above the delta function (see below), and now it is working as spected. So, how can we make it less predictable ?

    void GameScene::step(float dt)
  • Sonar Systems admin
    Likes 0

    Hello

    What sort of unpredictability did you have in mind?..


  • iam777
    Likes 0

    I would like to randomly pick one of the “Moving Platforms” and not make it to go all the way to the screen edge. Instead of that, go back again. Basically, the moving platform will behave in two different ways:

    1.- Move across the screen back-and-forth (you did it already :P )

    2.- Move partially back-and-forth but randomly.

    Thanks again in advance.

     

  • Sonar Systems admin
    Likes 1

    Hello

     

    Here is one possible way:

    (arc4random % end - start)) + start;

     

    Use the code above to generate a random number between a range a minus it from the screen edge and add it to the screen edge start so it doesn’t go all the way.

    Let us know how it goes.


  • iam777
    Likes 0

    Thanks, that works perfect!

  • Sonar Systems admin
    Likes 0

    Sweet

    Don’t forget, any questions you have, feel free to ask them.

     

     


Login to reply