How can I make the motion smoother? - memory

I'am programming my first game for Ipad. I have a little problem on an animation. In this game I have a ball bouncing around the screen. I move the ball in this way
CGRect frameRect = ball.frame;
frameRect.origin.x += ballMovement.x;
frameRect.origin.y += ballMovement.y;
ball.frame=frameRect;
The ball moves but the animation is sometimes not very smooth... The strange thing is that while I was testing my app on my ipad I discovered that if I close the app and reopen it from the multitasking bar the ball moves way way better! The animation is smoother and faster...
Can somebody explain why is happening this thing?
Thank you!! Daniel from Italy

If reopening the application makes it smooth, then most likely something has been cached the second time that was not cached the first time. There's nothing in the 4 lines of code you posted that could explain the lack of smoothness. Maybe posting more of the code would help.

Related

how can we Add Parallax Scrolling in Cocos 2D?

how can we Add Parallax Scrolling in Cocos 2D version 3 (IOS) . I have explored many tutorials but nothing has helped me. i have tried following code but its not working . can you please list down steps to achieve Parallax Scrolling in Cocos2D game. i ma newbie in game Development . your help will be highly appreciated. Thanks
CCParallaxNode *voidNode = [CCParallaxNode node];
[voidNode addChild:bg4 z:1 parallaxRatio:ccp(3.5f, 3.5f) positionOffset:ccp(3.5f, 3.5f)];
[voidNode addChild:_spacedust1 z:2 parallaxRatio:ccp(3.5f, 3.5f) positionOffset:ccp(3.5, 3.5)];
[self addChild:voidNode];
You just move the voidNode. All children that you add to voidNode will be moved relative to voidNode.
So for example the foreground should be moved exactly like the voidNode then you make a parallaxRatio of ccp(1.f, 1.f).
Then the background for example should be moved slower than the foreground you make a parallaxRatio of ccp(0.6f, 0.6f).

iAd Banner is messing up scene in SpriteKit

I think I've looked through all the previous posts that are similar to this problem and couldn't find a solution so I hope someone can help me.
I've got a very simple game using SpriteKit. The game is in portrait mode only. When I view the game in the simulator, the ads appear perfect but they are on top of my content and have also shifted the top content off the screen so you can barely see the scores. I'm not sure if I need to do something in the ViewController or the scene itself that has the ads running, but I think the ViewController is the correct place.
I'm hoping someone else had this problem and figured it out. I saw some people say that using:
scene.scaleMode = SKSceneScaleModeAspectFill;
or
scene.scaleMode = SKSceneScaleModeAspectFit;
changed their views to work. I'm using "fill" but I tried "fit" and neither seemed to do anything. Ideally the top of the banner is the new bottom of my screen and I can get everything to fit in the new smaller size but I'm kinda of stuck at the moment. Thanks for any help you can provide!
I had the same issue and instead of using AspectFill or AspectFit
use this.
scene.scaleMode = .Fill
This option didn't cover up as much content as the others.

Temporary disable physics from CCSprite

I'm currently discovering Cocos2D in combination with SpriteBuilder and I'm making stuff bounce around.
It's quite fun :)
What I'm trying to figure out is the following :
- On SpriteBuilder, I create a CCSprite, and I enable physics for this one.
- I can invoke it in XCode, and do whatever I want with it. Plus, the sprite is reacting accordingly to the physics settings applied to it. Great.
Now, what I would like to do is to disable the physics from this sprite in the code. I don't find a method in the Cocos2D API that would do the trick.
Can you help me out ?
Thank you for your time and help ;)
I write the correct answer then:
sprite.physicsBody.sensor = TRUE;

iOS7 dirty rect drawing weirdness

I have been developing a cad-like drawing app for iOS, which makes careful use of dirty rect clipping to make for a smooth drawing experience - it has worked quite well for months. Now, I know iOS doesn't automatically clip dirty rects, but CoreGraphics will. For reference, here's what the preamble of my main view's drawRect function looks like (leaving out the actual drawing mechanism)
- (void)drawRect:(CGRect)dirtyRect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
if ( !CGRectIsNull(dirtyRect))
{
CGContextClipToRect( context, dirtyRect );
}
CGContextConcatCTM( context, self.viewTransform );
.... drawing here, all done by CG* vector and bitmap functionsa
CGContextRestoreGState( context );
}
Now, this code worked beautifully for a long time, iOS6, early iOS7 builds, etc. Smooth as butter. But just recently, I've been seeing a VERY odd set of behaviors, and I'm curious if anybody here has had similar experiences.
First, I trigger redraws the normal way, setNeedsDisplay when I need to redraw the whole view, and setNeedsDisplayInRect passing a dirty rect for just the small bit that needs redrawing. Weirdly, recently when I call setNeedsDisplayInRect the rect I pass is ignored, and instead drawRect receives a rect matching the view's bounds and not the intended subrect. I only noticed this when testers reported dramatically bad drawing performance compared to previous builds - I've been using the simulator too much recently and hadn't noticed :/
I suspected this might be the result of some kind of multiple-dirty-rect union, so I overloaded setNeedsDisplay and setNeedsDisplayInRect, tracing when they got called, but that let nowhere. So then I decided to track my dirty rect manually as an iVar in the my rendering view class. Here's where the second oddity shows up: when I clip to the now correct dirty rect, the contents outside the dirty rect are cleared.
They should NOT be cleared. I have explicitly set self.clearsContextBeforeDrawing = NO, so I don't understand why the behavior I'm seeing is happening, particularly when it wasn't happening in the past.
Now, I'm probably going to do a git-bisect style of debugging to see if I can find a version way back when which didn't have this bug. But I'm hoping somebody here with CoreGraphics experience can lend me some conceptual support. This has me totally baffled.
P.S. I haven't touched the drawing code for my app in a long time since I've spent the last few months developing the app around the drawing tools. This is why I suspect a change to iOS7's drawing pipeline may play some kind of role here.
I think you need to include this line in your code inside drawRect method :-
[super drawRect:dirtyRect];
For anybody who stumbles across this in the future when searching for help with iOS dirty rects, I want to say I solved this, and it was my bug, not apple's. I spent a morning doing a git bisect to narrow down when the trouble showed up, and found that when I did some refactoring back in early october, I introduced some change notifications to my model which were indirectly causing a setNeedsDisplay which overwrote the setNeedsDisplayInRect my touch handling code was laboriously computing.
So - not an apple bug!
drawRect:dirtyRect is for mac. For ios you should use drawRect:rect. It should be called with setNeedsDisplay.

touchesBegan only fired within 768x768 pixel rectangle

I'm seeing weird behaviour with my iPad app on both simulator (5.1 and 6.0) and my device (iPad2 running iOS6.0). Whichever orientation the app starts in, touchesBegan is only fired for new touches in the region (0,0) - (767,767) - in portrait mode the bottom of the screen doesn't respond, in landscape the right side.
However, touchesMoved & touchesEnded work on the whole display - if I touch and drag to the 'broken' region, the touch release event is received.
I further noted that while on the iOS 6 simulator my app looks right even though it doesn't work right, on the iOS 5.1 simulator the rendering was also a bit messed up initially too:
The whole display should be grey and that box with sliders in the bottom-right corner. Again, 768 seems the magic value here. Rotating a couple of times fixes this render issue but not the input issue... start in portrait mode and rotate 180 degrees and it now looks right but the top stops responding rather than the bottom.
There is a 3rd-partly library being used here, so my main question is what might cause this kind of problem, what should be I be looking for? I'm also curious as to why the two simulators give different results
If you're touchesBegan, touchesMoved, and touchesEnded all are looking for a location within your self.view, then you might not be in the correct frame area for the touches to be received. I just had a similar problem and I explicitly changed the size of the frame using:
self.view.frame = CGRectMake(0,0,1024,1536);
since my game is two screens tall. After readjusting the frame size, touchesBegan was working fine. Hopefully this helps!

Resources