0
\$\begingroup\$

I have an object that I am resizing from bottom to top.

private void setStickSize() {
    if (MyInputProcessor.isTap) {
        stickSprite.setSize(stickSprite.getWidth(),stickSprite.getHeight()+500.0f); 
       stickSprite.setY(MathUtils.clamp(stickSprite.getY(),0,700));   
       // MyInputProcessor.isTap = false;
       }

Here the sprite is growing out of the screen after calling this in update().I used clamp() to stop this sticksprite within the screen.But no effects on the code. Why clamp() is not working here? How can I restrict the sprite resizing effectively within the screen?

I am drawing the sprite like this.

    private void drawStick() {   
     stickSprite.setPosition(stick.getX(),stick.getY());

     batch.draw(
            stickSprite.getTexture(),stick.getX(),stick.getY(),
             stickSprite.getWidth()/2 ,stickSprite.getHeight()/2,
             stickSprite.getWidth(),                                                  
             stickSprite.getHeight()*0.01f,                                                  
             stickSprite.getScaleX(),stickSprite.getScaleY(), 0,                  
            stickSprite.getRegionX(),stickSprite.getRegionY(),  
            stickSprite.getRegionWidth(), stickSprite.getRegionHeight(),
             false,false);
        }

Entire gameScreen:

public class GameScreen implements Screen{

// ---objects
private SwatterGame game;
private AssetManager assetManager;
private final ScoreController scoreController;
private ObjectFactory objectFactory;
private Stick stick;
Sprite stickSprite;
// ----constructs
private ShapeRenderer shapeRenderer;
private SpriteBatch batch;
// ----Textures--
Texture bgTexture,platformTexture,stickTexture;


public GameScreen(SwatterGame game) {
    this.game = game;
    assetManager = game.getAssetManager();
    scoreController = game.getScoreController();
}

@Override
public void show() {
    loadAssets();
    game.viewPort.apply();
    game.camera.position.set(GameConstants.WORLD_WIDTH / 2, GameConstants.WORLD_HEIGHT / 2, 0);
    game.camera.update();
    batch = new SpriteBatch();
    shapeRenderer = new ShapeRenderer();

    MyInputProcessor myInputProcessor = new MyInputProcessor();
    InputMultiplexer im = myInputProcessor.returnInput();
    Gdx.input.setInputProcessor(im);
    initObjects();
   game.camera.zoom =3f; game.camera.update();
}

@Override
public void render(float delta) {
    clearScreen();
    batchDraw();
    update(delta);
    drawDebug();
}



private void loadAssets() {

    bgTexture = assetManager.get(Assets.background);
    stickTexture = new Texture(Gdx.files.internal("rect3.png"));
    stickSprite = new Sprite(stickTexture);
}

private void initObjects() {
    objectFactory=new ObjectFactory();
    player=objectFactory.createPlayer();
    stick=objectFactory.createStick();

}
@Override
public void resize(int width, int height) {
    game.viewPort.update(width, height);
    game.camera.position.set(game.camera.viewportWidth / 2, game.camera.viewportHeight / 2, 0);

}

private void drawDebug() {
    shapeRenderer.setProjectionMatrix(game.camera.projection);
    shapeRenderer.setTransformMatrix(game.camera.view);

    // player
    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.setColor(1, 0, 1, 1);
//  shapeRenderer.rotate(0.f, 0.f,1.0f, 45.f);
    rect(shapeRenderer, player.getCollisionRectangle());
    // stick
    rect(shapeRenderer, stick.getCollisionRectangle());
    shapeRenderer.end();
}


//shape rendering
private void rect(ShapeRenderer renderer, Rectangle rectangle) {
    shapeRenderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
}

private void circle(ShapeRenderer renderer, Circle circle) {
    shapeRenderer.circle(circle.x, circle.y, circle.radius, 100);
}

private void update(float delta) {
    player.update(delta);
    stick.update(delta);
    setStickSize();
}
private void batchDraw() {
    batch.setProjectionMatrix(game.camera.combined);
    batch.begin();
    //drawing background
    batch.draw(bgTexture,GameConstants.BG_X,GameConstants.BG_Y);
    batch.draw(platformTexture, GameConstants.PLATFORM_X,GameConstants.PLATFORM_Y);
    //drawBottom();

    drawStick();
    batch.end();

}


private void setStickSize() {
    float newHeight=stickSprite.getHeight() + 500.0f;
        if (MyInputProcessor.isTap) {
        stickSprite.setSize(stickSprite.getWidth(), newHeight);
      //    MyInputProcessor.isTap= true;

        }
        }

private void drawStick() {   
     stickSprite.setPosition(stick.getX(),stick.getY());
   //  stickSprite.setSize(stickSprite.getWidth(),stickSprite.getHeight());
     stickSprite.draw(batch);
        }

private void clearScreen() {
    Gdx.gl.glClearColor(Color.GRAY.r, Color.GRAY.g, Color.GRAY.b, Color.GRAY.a);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}

@Override
public void dispose() {
    // TODO Auto-generated method stub

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

EDIT:

// Set a fix y for you sprite
sprite.setY(fixY);

// Set a max height for your sprite
float maxHeight = Gdx.graphics.getHeight() - sprite.getY(); // Here you say that you want the max height of the sprite to be the difference between screen height and sprite.getY() so it will be in the screen

// Compute your new height
float newHeight = /* Compute new height here */;

// Set your height
sprite.setHeight(MathUtils.clamp(newHeight, 0, maxHeight));

This way :

You fix a Y for your sprite. Then compute the max height of your sprite, depending of his Y position and the screen height. For example :

 sprite.getY() = 80
 Gdx.graphics.getHeight() = 480
 maxHeight = 480 - 80 = 400

Then compute the new height of your sprite. Finally set this with the clamp you compute before.

I already said that before, but you should draw your sprite with sprite.draw(batch) so it really takes it's attributes to be displayed. Here you do some weird things in the batch.draw


Clamp works like this :

You put your value, then min and max, so if your value is superior to max it will return max and if your value is inferior to min it will return min. Here you're setting y by a clamp with min = 0 and max = 700. What you want to do is a clamp between 0 and max = 700 - sprite.getHeight().

In fact, your set Y here will not say anything if you are at Y = 700. But if you're screen height is <= to 700 you won't see it. Because origin is at bot left corner.

If you want your sprite to stay IN screen, you should not clamp on Y, but on setHeight().

What you want is that sprite.getY() + sprite.getHeight() < Gdx.graphics.getHeight()

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I tried.But my height values are going in larger numbers like 15000,20000 etc while resizing.How can I clamp on Height value then? \$\endgroup\$ – Niranjana Feb 13 '17 at 9:53
  • \$\begingroup\$ This object already has an height of 48. \$\endgroup\$ – Niranjana Feb 13 '17 at 10:01
  • \$\begingroup\$ Thanks for the edited answer.I used batch.draw() because with sprite.draw(batch),I cant get the gradually increasing effect.It was simply drawing the resized image at once.I dont know where I did something wrong in the code.Now I am completely confused:( \$\endgroup\$ – Niranjana Feb 13 '17 at 11:27
  • \$\begingroup\$ In your stick set size, do what i said in my update and it should work :) \$\endgroup\$ – Sebastien Servouze Feb 13 '17 at 12:37
  • \$\begingroup\$ is there any setHeight() method available for sprite? \$\endgroup\$ – Niranjana Feb 13 '17 at 12:56

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.