Cocos2d-x Sprite flickers during scale animation sequence - c++

I have a grow / shrink animation sequence. But for some reason the sprite does not scale smoothly. I did change the animation interval and disabled depth test but it did not fix this.
What can be the reason behind this?
Does does have something to do with art asset that is used?
ScaleTo* grow = ScaleTo::create(1, 1.5);
ScaleTo* shrink = ScaleTo::create(1, 1);
Sequence* anim = Sequence::create(grow, shrink , NULL);
RepeatForever *rep = RepeatForever::create(anim);
station_image->runAction(rep);
Solved
I was able to fix this issue by enabling ‘CC_FIX_ARTIFACTS_BY_STRECHING_TEXEL’ in ccConfig.h

Related

Zooming in and out of Scene in Cocos 2D-X 3.2

I'm pretty new to Cocos 2D-X but have some decent background in C++. I have a sprite _rocket (tied to a Box2D body) that occasionally moves outside the visible view of my screen. I'd like for the view to automatically zoom out as the sprite approaches the edge of the screen, so that the sprite is always in the view. When the sprite returns to the original view frame, the view should scale back to its original size.
I was able to zoom out with the following code in the update function:
Size winSize = Director::getInstance()->getWinSize();
if ((_rocket->getPosition().x - _rocket->getContentSize().width/2 < 10.0) ||
(_rocket->getPosition().x + _rocket->getContentSize().width/2 > winSize.width - 10.0) ||
(_rocket->getPosition().y - _rocket->getContentSize().width/2 < 10.0) ||
(_rocket->getPosition().y + _rocket->getContentSize().width/2 > winSize.height - 10.0))
{
this->setScale(this->getScale()-0.005);
}
However, because winSize isn't updated, this essentially scales forever, until the sprite returns to the original view. I am not sure how to update winSize so that it can be used iteratively to find the screen's edge. There may also be a much easier way of approaching this.
i don't understand why the winSize should change.
if you mean the _rock's contentsize not change
you should use
auto size = _rocket->getBoundingBox().size;
They removed some useful camera functions in cocos2d-x 3.+
The workaround is to scale/move the layer containing the game instead of trying to move the camera.

Side scrolling game background

I am working in Cocos2d and xcode to create a side scrolling game.
I have an issue with my coding for adding a background to the levelscene.
I tried to implement a parallax background to no avail so have opted to just have a background the player will scroll across.
But at the moment the background follows the player across the screen, which frankly looks rubbish.
The code I have is
(void)setupParallax {
NSString *backgroundName = [self.tilemap propertyNamed:#"BackgroundImage"];
CCSprite *background = [CCSprite spriteWithFile:[AssetHelper getDeviceSpecificFileNameFor:[NSString stringWithFormat:#"background-noon.png]]];
background.anchorPoint = ccp(0,0);
background.position = CGPointMake(0, 0);
[self addChild:background z:0];
}
I know it must be something with either the position or anchorPoint but these values only change with reference to the screen they are loaded on.
Have you looked at this tutorial on doing parallax scrolling in cocos2d-x?
You an do parallax scrolling by hand...but it will probably easier, at least if you are just starting out, to do it using the CCParallaxNode and let the framework do the heavy lifting for you.

Qt 4.8.5 QGraphicsView::fitInView fails to fit exactly

I Have a problem trying to fit my content (stretch) of a scene into my QDeclarativeView. I load a QML file the common way. I overrode the showEvent and resizeEvent method with the code below:
QGraphicsItem* rootItem = this->scene()->items.at(0);
QRectF rootRect = rootItem->sceneBoundingRect(); // it gives me a QRectF(0,0,1920,1080)
this->fitInView(rootRect, Qt::IgnoreAspectRatio); // Aspect doesn't matters.
The problem is that it keeps showing a little white border (almost 4 pixels) around the content. I've tested in 1920x1080, 1920x1200 and 1440x900 and all those resolutions on my desktop shows the content with the same problem. Even out of fullscreen mode it keeps the little white border.
Just to make sure it was nothing from the content, I have set the view's background brush to black and the white border became black (in other words, the content is being scaled down too much to fit in view).
Subtracting values from rectangle hardcoding is not an option once it's varying the background portion depending on the content size. (It should adapt dynamically).
Any suggestions?
Just encountered the problem as well. Since the bug is not about to be solved, I'll post my partial solution. I subclassed QGraphicsView and added a method myFitInView(), that does the required scaling and centering automatically.
I guess if you need more performance you could also directly fill the the matrix, but I don't need this and therefore scale and center seperately.
Also, any previous view transformations are lost with this approach, but you could probably account for that as well, by getting the current matrix and modifying/multiplying it accordingly.
void MyGraphicsView::myFitInView(QRectF const &rect)
{
QRectF viewRect = frameRect();
double scaleX = viewRect.width() / rect.width();
double scaleY = viewRect.height() / rect.height();
QTransform trans;
trans.scale(scaleX, scaleY);
setTransform(trans, false);
centerOn(rect.width() / 2, rect.height() / 2);
}
seems like a bug in Qt: https://bugreports.qt-project.org/browse/QTBUG-11945.
my ugly workaround for that is
QRectF removeMargin11945(const QRectF& sceneRect, const QSize& viewerSize){
const int bugMargin = 2;
const double mx = sceneRect.width()/viewerSize.width()*bugMargin;
const double my = sceneRect.height()/viewerSize.height()*bugMargin;
return sceneRect.adjusted(mx, my, -mx, -my);
}
What makes it particularly ugly is that sometimes it is not required and things just work fine, be careful to distinguish those cases.

CCParallaxNode adding childs while scrolling

I'am using a CCParallaxNode to scroll 3 backgrounds along with Ray Wenderlich's category to move the backgrounds when they go out of the screen.
It is working just fine, my problem is that i want to add childs (enimies) on the fly, like every 5 seconds. Normally i would just add the enemies to the parent layer using a CCMoveTo action to animate him over the screen but I want my enimies to follow the foreground of the parallax layer.
I'am increasing the scroll speed slowly as the game progresses.
I can't seem to figure out the right offset when calling
CGFloat offset = self.gameBackground.position.x;
[self.gameBackground addChild:enimy z:5 parallaxRatio:ccp(0.1, 0.1) positionOffset:ccp(offset, 85)];
Can someone help me out with this?
edit:
I'am doing this to move the background:
- (void)update:(ccTime)delta
{
self.speed -= 0.5f;
CGPoint backgroundScrollVel = ccp(self.speed, 0);
self.gameBackground.position = ccpAdd(self.gameBackground.position, ccpMult(backgroundScrollVel, delta));
}
Thanks
Rays article: http://www.raywenderlich.com/3611/how-to-make-a-space-shooter-iphone-game
Final solution:
I ended up just adding the enimies to the CCLayer instead of the Parallax. To move the enimies in the same speed as the foremost layer child in Parallax i did the following:
in update:(ccTime)delta:
CGFloat parallaxRatio = 0.1f;
CGPoint backgroundScrollVel = ccp((self.backgroundSpeed * - 1) * parallaxRatio, 0);
for(WKEnimy *enemy in self.enimies)
{
enemy.position = ccpAdd(enemy.position, ccpMult(backgroundScrollVel, delta));
}
You could add your enemy CCSprites to your foreground CCLayer (instead of directly adding them to your CCParallaxNode). Besides, I wouldn't recommend using actions (such as CCMoveTo) for this particular case; you may update your sprites positions as your doing with your gameBackground, and 'manually' check wether or not they're off the screen.

screen size in cocos2d

i would like to change the screen size so that sprites will disappear before they reach the real screen edges.
BUT i still want my background to stay on all of the screen size.
Imagine a paper on my screen so i want to game to exist only on that paper, and around that paper there still will be some background.
so, how do i set my CCSprites to move in and out from that paper and slowly disappear when coming to its edges ?
my sprites are moves with : (i need to put some code to get published cause site "standard" )
id moveclouds1 = [CCMoveTo actionWithDuration:30 position:ccp(420,380)];
thanks.
you can use glscissor for that
simply subclass a CCLayer to make your "paper screen". Then add you sprites inside this layer.
on this layer override the visit method
- (void) visit
{
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(x,y, width, height); //here put the position and the size of the paper/screen
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
a sprite reaching the border of the paper/screen will be scissored off.
REMEMBER: glScissor will use PIXEL values not points, so it`s your job to use double values for retina display ( CC_CONTENT_SCALE_FACTOR() can come in handy )