简体   繁体   English

Cocos2d-x setScaleX()

[英]Cocos2d-x setScaleX()

So I was programming a game in cocos2d-x and I need one of the sprites to get wider for a certain amount of time, so I tried with the method setScaleX(). 因此,我正在用cocos2d-x编写游戏,并且我需要一个精灵来在一定时间内变宽,所以我尝试了setScaleX()方法。 The problem is that the content size of the sprite does not actually change, and since my collision system is based on the content size of the sprite, they do not properly work. 问题在于,子画面的内容大小实际上并未更改,并且由于我的碰撞系统基于子画面的内容大小,因此它们无法正常工作。 Here is the code I use for scaling: 这是我用于缩放的代码:

bar = Sprite::create( "Bar.png" );
CCLOG("Size: %f,%f.", bar->getContentSize().width, bar->getContentSize().height);
bar->setScaleX(1.5);
CCLOG("Size: %f,%f.", bar->getContentSize().width, bar->getContentSize().height);

The output is exactly the same on both cases. 两种情况下的输出完全相同。 Is there any way of fixing this? 有什么办法解决这个问题?

ContentSize represent the original texture size unless you set it using setContentSize method. ContentSize表示原始纹理大小,除非您使用setContentSize方法设置它。

You can either multiply size with scale factor or use boundingBox().size to know about current size of scaled sprite(if not rotated or skewed). 您可以将大小乘以比例因子,也可以使用boundingBox().size来了解缩放后的精灵的当前大小(如果未旋转或倾斜)。

cocos2d::Size size1 = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size1.width/768;
scaleY=size1.height/1024;

Sprite *sp1=Sprite::create("01.png");
    sp1->setScaleX(scaleX);
    sp1->setScaleY(scaleY);
    this->addChild(sp1);
Scale for Landscape:

cocos2d::Size size = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size.width/1024;
scaleY=size.height/768;

Scale for Portrait:

cocos2d::Size size = cocos2d::CCDirector::getInstance()->getWinSize();
scaleX=size.width/768;
scaleY=size.height/1024;

Sprite *sp=Sprite::create("01.png");
    sp->setScaleX(scaleX);
    sp->setScaleY(scaleY);
    this->addChild(sp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM