iOS Sprite Kit - SKSpriteNode's .centerRect property not working
iOS Sprite Kit - SKSpriteNode's .centerRect property not working
I was going through the SpriteKit documentation by Apple and came across a really useful feature that I could use when programming my UI. The problem is I can't get it to work.
Please see this page and scroll down to "Resizing a Sprite" - Apple Docs
I have literally copied the image dimensions and used the same code incase I was doing something wrong. But I always end up with a stretched looking image rather than the correct "end caps" staying the same scale.
I am referring to this code:
SKSpriteNode *button = [SKSpriteNode spriteWithImageNamed:@"stretchable_button.png"]; button.centerRect = CGRectMake(12.0/28.0,12.0/28.0,4.0/28.0,4.0/28.0);
What am I doing wrong? Is there a step I have missed?
EDIT:
Here is the code I have been using. I stripped it of my button class and tried to use it with an SKSPriteNode but still the problem persists. I also changed the image just to make sure it wasnt that. The image im using is a 32x32 at normal size.
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"Button.png"]; [self addChild:button]; button.position = ccp(200, 200); button.size = CGSizeMake(128, 64); button.centerRect = CGRectMake(9/32, 9/32, 14/32, 14/32);
Answer by rickster for iOS Sprite Kit - SKSpriteNode's .centerRect property not working
9/32
is integer division, so the result passed to CGRectMake
is zero. Ditto the other three parameters. If you use floating point literals like the example you cite, you might get better results.
Answer by Tomoso for iOS Sprite Kit - SKSpriteNode's .centerRect property not working
Yes I noticed that after I checked the NSLOGs I was putting out and fixed it shortly after posting.
But according to this article the '.centerRect' property is broken and doesn't work.
(Also I searched the Apple Developer forums and found 3 posts citing the same problem. Apple hasn't fixed this issue yet.
Answer by rwr for iOS Sprite Kit - SKSpriteNode's .centerRect property not working
The .centerRect property works as documented if you adjust the sprites .scale property.
Try:
SKTexture *texture = [SKTexture textureWithImageNamed:@"Button.png"]; SKSpriteNode *button = [[SKSpriteNode alloc] initWithTexture:texture]; button.centerRect = CGRectMake(9/32, 9/32, 14/32, 14/32); [self addChild:button]; button.xScale = 128.0/texture.size.width; button.yScale = 64.0/texture.size.height;
Answer by Theis Egeberg for iOS Sprite Kit - SKSpriteNode's .centerRect property not working
Based on rwr's answer here is a working init method for a SKSpriteNode. I use this in my own game. Basically you make insets of 10px all around the the output image. And then call it like this:
[[HudBoxScalable alloc] initWithTexture:[atlas textureNamed:@"hud_box_9grid.png"] inset:10 size:CGSizeMake(300, 100) delegate:(id)clickedObject]; -(id) initWithTexture:(SKTexture *)texture inset:(float)inset size:(CGSize)size { if (self=[super initWithTexture:texture]) { self.centerRect = CGRectMake(inset/texture.size.width,inset/texture.size.height,(texture.size.width-inset*2)/texture.size.width,(texture.size.height-inset*2)/texture.size.height); self.xScale = size.width/texture.size.width; self.yScale = size.height/texture.size.height; } return self; }
Answer by Krekin for iOS Sprite Kit - SKSpriteNode's .centerRect property not working
Here's a refresh of how exactly this works. By the way, my image size width is 48 pixels and height is 52 pixels, but this doesn't matter at all. Any image can be used:
SKSpriteNode *button = [SKSpriteNode spriteNodeWithImageNamed:@"Button.png"]; //(x, y, width, height). First two values are the four corners of the image that you DON'T want touched/resized (They will just be moved). //The second two values represent how much of images width & height you want cut out & used as stretching material. Cut out happens from the center of the image. button.centerRect = CGRectMake(20/button1.frame.size.width, 20/button1.frame.size.height, 5/button1.frame.size.width, 15/button1.frame.size.height); button.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2); //Positions sprite in the middle of the screen. button.xScale = 4; //Resizes width (This is all I needed). //button.yScale = 2; //Resizes height (Commented out because I didn't need this. You can uncomment if the button needs to be higher). [self addChild:button];
Read the section called "Resizing a Sprite" in this document: https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html#//apple_ref/doc/uid/TP40013043-CH9-SW10
'Figure 2-4 A stretchable button texture' demonstrates how the (x, y, width, height) works.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment