0
\$\begingroup\$

I was wondering how you can make a sprite jump to a certain direction in libGDX just like the brick does in the famous mobile game "Amazing Brick'

I want to make my sprite jump up a bit to the right when I tap the right part of the phone and make it jump up but to the left direction when I tap on the left part of my phone.

I am using libGDX on android studio and I am on a beginner level, trying to develop a unique game for android. Plz help. Thanks in advance. ^o^

\$\endgroup\$
  • \$\begingroup\$ I don't know 'Amazing Brick', so it's not as much famous as you think it is. Could you add a link to a video or something like that to show a bit more clearly what you have in mind? \$\endgroup\$ – Vaillancourt Jan 24 '16 at 18:36
  • \$\begingroup\$ youtu.be/gllQ1FNKff8 this is a gameplay of the amazing brick game. You can see how the brick move upwards to the left side or right side corresponding to whether the user tapped on the left or right part of the mobile screen. \$\endgroup\$ – user78123 Jan 25 '16 at 4:47
0
\$\begingroup\$

Short answer:

public void update()
{
    if (Gdx.input.justTouched())
    {
        //Touched left side of screen
        if (Gdx.input.getX() > Gdx.graphics.getWidth() / 2)
        {
            //give actor upforce and and a bit of force to the left
            someActor.setVelocity(new Vector2(-25, 100));

        }
        //touched right side of screen
        else 
        {
            //give actor upforce and a bit to the right
            someActor.setVelocity(new Vector2(25, 100));
        }
    }
}

The problem is this won't work on it's own. You would need a class for your sprite and give it appropriate down force as well. The amounts you use depend on the units you give in your game.

You could add some drag to the sideway movement by multiplying the X velocity by something like .95. For the downforce/gravity you could use need to subtract something each update so it will eventually fall down. You could use realistic numbers here or make something up and tweak it until it's right.

To move your sprite you use the velocity the actor has.

position.add(velocity);

Your best bet is to get familiar with the basic vector math. Start coding and if you run into unsolvable problems come back and ask something more specific.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your well elaborated answer. I am new to game development in LibGDX and your answer was very helpful. By the way, I played your game bounce and break and rated it five stars! You seem to have a lot of passion for game development. XOXO \$\endgroup\$ – user78123 Feb 17 '16 at 11:55
  • \$\begingroup\$ Thanks for playing and voting my game! For now I create games just because I love to do it and want to keep learning. I hope I can earn a living from it some day but that is very hard to do as a indie nowadays. But who knows, I'm not going to quit this hobby ever :D. Good luck with your game! \$\endgroup\$ – Madmenyo Feb 17 '16 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.