I am in process of making a small game, where the main Gameplay Layer is being zoomed in and out based on a number of parameters. This is done by setting the .scale attribute to a fraction of 1. Works well.
I have a problem, however, that when calling [[CCDirector sharedDirector] winSize]; from any of the child nodes, I get a scaled window size, which kinda sucks 🙂 Is there any way around this apart from multiplying all my off-screen checks by the scale fraction plus one?
Here is an example
I am creating a CCLayer and placing Sprites on it. All the sprites have a static variable with the screen size during initialization, basically
static CGRect screenRect; //stored as a static variable for performance reasons
...
-(id)init{
...
// make sure to initialize the screen rect only once
if (CGRectIsEmpty(screenRect)){
CGSize screenSize = [[CCDirector sharedDirector] winSize];
screenRect = CGRectMake(0, 0, screenSize.width, screenSize.height);
CCLOG(@"Screen Size during init: %f, %f", screenSize.width, screenSize.height);
}
}
I then have a check when the sprite is moving around whether it is off the screen, and if so, I take it automatically to the opposite side:
if(newPosition.x > screenRect.size.width){
newPosition.x = 0;
}else if(newPosition.x < 0){
newPosition.x = screenRect.size.width;
}
if(newPosition.y > screenRect.size.height){
newPosition.y = 0;
}else if(newPosition.y < 0){
newPosition.y = screenRect.size.height;
}
All works very well and as expected. However, if I change the scale property of the parent CCLayer then offscreen positioning “scales” together with the layer, which is kinda weird. So if I set the CCLayer.scale = 0.5f; then it appears as though the off-screen boundaries have moved “inside” the phone screen as well. I want to avoid this. How?
After talking to quite a few developers, the off-bounds check need to take scaling into account, i.e.