Cocos2d-Android - What is the difference between Sprite.getcontentsize , Sprite.getscale , Sprite.gettexture

StackOverflow https://stackoverflow.com/questions/21793870

Question

The Title says it all. I want to know what is the difference between Sprite.getcontentsize, Sprite.gettexture , Sprite.getscale. and How are they used. I couldn't find any resources following this question.

Was it helpful?

Solution

We have for example image with name "test.png" with width 100px and height 200px. Then:

CCSprite testImage = CCSprite.sprite("test.png");

// print original image size
System.out.println("" + testImage.getContentSize().width + ", " + testImage.getContentSize().height); // print: 100, 200

// print actual image size
System.out.println("" + testImage.getBoundingBox().size.width + ", " + testImage.getBoundingBox().size.height); // print: 100, 200

// this will change resolution of image to half, so it will be drawn 2x smaller then original size
testImage.setScale(0.5f); 

// print how many is image scaled
System.out.println("" + testImage.getScale()); // print: O.5

// print original image size
System.out.println("" + testImage.getContentSize().width + ", " + testImage.getContentSize().height); // print: 100, 200
// print actual image size
System.out.println("" + testImage.getBoundingBox().size.width + ", " + testImage.getBoundingBox().size.height); // print: 50, 100

testImage.gettexture()will probably return loaded texture/image, but I don't know, I have never needed to use this function.

OTHER TIPS

Well, as per my knowledge:

  1. Sprite.getContentSize() --> It will return the size of Sprite(i.e its width and height)

  2. Sprite.getScale() --> It will return the amount the resolution it shrinked (By default its value is 1.This method is used if you want to change its size at runtime)

    If it is >1 then the sprite size will be more and <1 will give smaller size

Note: Sprite.getContentSize() has no effect on the scaled sprite. It will give original sprite size only though it scaled above or below to normal.

  1. Sprite.getTexture() --> It will give image of that sprite.It is used if you want to crate another sprite with same texture (image) i.e.

    CCSprite newSprite = CCSprite.sprite(Sprite.getTexture()); 
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top