0
\$\begingroup\$

I have a sprite like this.

enter image description here

I want to draw this sprite in such a way that sprite is growing from bottom to top, using sprite batch on tap.for that,I have written the following code:

  if (MyInputProcessor.isTap)
{
Sprite stickSprite = new Sprite(stickTexture);
stickSprite.setPosition(stick.getX(),stick.getY());
for(float n=0;n<100;n++)
{
     float i = 0.01f * n;
    batch.draw(
    stickSprite.getTexture(),
     stick.getX(),         
     stick.getY(),    
         stickSprite.getWidth() / 2,
         stickSprite.getHeight() / 2,            

         stickSprite.getWidth(),                                                   
         stickSprite.getHeight()*i,  //multiplying with height                                            
         stickSprite.getScaleX(),       
         stickSprite.getScaleY(),       
        0 ,                                    
        stickSprite.getRegionX(),      
        stickSprite.getRegionY(),      

        stickSprite.getRegionWidth(),
        stickSprite.getRegionHeight(), 
        false,
        false); 


 }
MyInputProcessor.isTap = false;  

}

Here the i value increments but not taking effect in the code to scale. Whats wrong with the code?

I tried this also:

   private void drawStick() { 

    Sprite stickSprite = new Sprite(stickTexture);

    if (MyInputProcessor.isTap)
    {
       stickSprite.setPosition(stick.getX(),stick.getY());
       for(float n=0;n<=100;n++)
       {
        float i = 0.01f * n;
        stickSprite.setSize(stickSprite.getWidth(),stickSprite.getHeight()+i);  
        }
    }
  stickSprite.draw(batch);

But rendering full sprite only.

\$\endgroup\$
0
\$\begingroup\$

EDIT: You need to separe sprite's initialization and display

  1. Make your sprite ONE time in your create method for example
  2. Update your sprite's height on tap
  3. Draw your sprite in your render method

Your code won't do what you want because you make a new sprite at every tap. You want to have a single sprite which you resize in height at every tap.

You should have something like this

// On tap
stickSprite.setSize(stickSprite.getWidth(), stickSprite.getHeight() + addValue);

This way you could draw your sprite easier like that

// On draw
stickSprite.draw(batch);

The setSize will resize your texture to the size you tell him. It will do it perfectly for your unicolor texture.

Hope it helps

\$\endgroup\$
  • \$\begingroup\$ Thanks for your answer!I tried this answer(included in the question now).Still it renders full sprite only.Please help me to find out whats wrong in it. \$\endgroup\$ – Niranjana Feb 10 '17 at 10:18
  • \$\begingroup\$ It's because you initialize your sprite at the same moment you draw it, so each tap, you'll have a NEW sprite that will erase the old one, i updated my answer to make it more clear \$\endgroup\$ – Sebastien Servouze Feb 10 '17 at 11:01

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.