I have made my game prototype for iPhone6+. I have a circular sprite which has dimensions 108x108. I am creating artwork for my prototype for the iPad. After doing my calculations (iPhone6+ width to iPad reduces by 7.25% and the height increases by 23.67% for 2048x1536), I arrived at a 102x133 sprite image for the iPad. This image is not a circle. The moment of the circle is dependent on the radius so I need to calculate it right for the physics. How should I calculate my radius to get a similar look and movement? Currently I am using the following:
radius = sprite.frame.size.width/2
sprite.physicsBody = SKPhysicsBody(circleOfRadius: radius)
The height and width of a sprite shouldnt get affected by the number of pixels on screen, it would only affect the viewable area of your game. If you're running into a case where it is being affected that way, you should think of simply changing the scale mode on the scene. Here's a good explanation of using scale modes -http://blog.infinitecortex.com/2014/01/spritekit-understanding-skscene-scalemode/
Related
My scene's size is view.bounds.size
I have a ball whose size is CGSize(width: frame.size.width/25, height: frame.size.width/25)
Originally, i had the ball speed at CGVector(dx: 25, dy: 25), but the ball went faster on smaller devices (SE) and slower on larger ones (8 Plus), which makes sense. So i changed the ball's x and y velocity to be frame.size.width * 0.067, which comes to 25 on an iPhone 7, which is the speed i like. Unfortunately, the ball still moved at different speeds on different devices. Then, I thought it must be that the ball has a different mass because the ball size is a percentage of the frame width. So i set the mass to be a constant, but still the ball moves at different speeds
so now i have no idea whats going on.
Any ideas?
I think you can use constant size for scene (for playing area) and render that constant size to any screens with some transformation. Therefore all logic of your application will interact with constant size on any devices.
If screen has other ratio, you can add decorative elements near edges of the screen.
I am making this basic space shooting game but I can't get the x-coordinates of enemies right. Sometimes, they go out of the screen - or remain half inside at the edges. How can I fix this permanently regardless of which iPhone the app runs on?
here is the code for my positioning: (note that egg is name of my enemy. it is function I made for calling it every single time)
func egg() {
var egg = SKSpriteNode(imageNamed: list[Int(arc4random_uniform(6))])
var min = self.size.width / 8
var max = self.size.width
var point = UInt32(max - min)
egg.position = CGPoint(x: CGFloat(arc4random_uniform(point)), y: self.size.height)
let action = SKAction.moveToY(-100, duration: 2)
let actionDone = SKAction.removeFromParent()
egg.runAction(SKAction.sequence([action, actionDone]))
}
There are several places where things can be going sideways.
• First, it looks like you're assuming the location of your scene's anchorPoint is at (0,0). If it's not (the default .sks file now has an anchorPoint at (0.5,0.5), for instance), that could explain why your enemies never show up. The first thing I would do is double-check the anchorPoint of my scene.
• If you're adding these eggs to some other node besides the scene itself, then those eggs will "inherit" whatever translation, rotation, and scale that their parent node has. In other words, if you add an egg at (20,20) to a node at (30,30), it will appear at (50,50). Make sure the "context" of the eggs is what you expect.
• Your "min" and "max" values seem a little odd. The "min" looks like it is intended to be "indented" by 1/8th of the screen size on the left, but you aren't indenting it on the right. Maybe that's what you intend, but if you intend them to have symmetrical behavior, then you'll want to back "max" off by an eighth, too. Also, you're not adding that eighth back into the x value when you're determining the position, so this could stick an egg at x=0. This would explain why your sprites are sometimes "half inside at the edge".
• The size of your scene is not necessarily the screen dimensions. This depends heavily on the "scaleMode" of your SKScene. Check the documentation for more information on this, but briefly, the scaleMode tells SpriteKit how to render a scene that doesn't match its view in size or aspect ratio. Does it stretch? Does it crop? Does it letterbox? If you run a square 400x400 scene on a 1024x768 screen device, it has to have some way of knowing what you mean. Does it draw that 400x400 in the middle of the screen and let stuff be seen "outside" that rect? Or does it scale it up to fill the screen, cropping off the top and bottom? Or does it scale it up to FIT the screen, allowing space above and below that is technically outside the scene's size? Or does it scale it up and squash the scene to fit exactly? If your scene isn't matching up exactly with your device's screen, this could explain why things are not playing nice and staying within visible bounds.
I was trying to set the size of a SKSpriteNode from a PNG image (OK with that, just a:
test = SKSpriteNode(imageNamed: "myImage")
But when it comes to resize it, I'm not sure if I should use test.setScale or test.size.
What's the difference and when should I use each of them?
setScale():
Sets the xScale and yScale properties of the node. These two parameters are the scaling factor that multiplies the width (for xScale) and the height (for yScale) of a node and its children.
Code sample:
spaceship.setScale(0.50) // decreased scale to it's half size
size():
Expresses the dimensions of the sprite (width and height), in points.
Code sample:
spaceship.size = CGSizeMake(100.0, 70.0) // Re-size the dimensions to these values
When do you use setScale and when do you use size?
Usually setScale is used to increase or decrease the sprite about the scale factor, look for example the SKAction.scaleTo used to make a zoom in or zoom out.
size is frequently used because you can express in points the exactly values you want to do to your sprite.
You should use size when you want to change the specific size of a SKSpriteNode without affecting the size of any children of that sprite node.
Scaling a sprite node not only changes the size (scale) of the node, it also scales all that sprite node's children appropriately (proportionally) as well.
If a sprite node has no children, then there's no practical difference between size and scale.
Note: I find it helpful to use size when it doesn't matter so that I know that a scale of 1.0 is always the default size - handy for animating using scaleTo instead of scaleBy and back again to default size.
If you are using a spriteNode, I would use .setScale, or .xScale or .yScale , because those are specifically for sprites. .size can also be used for the size of labels, and text views in UIKit.
Lets say you want the sprite to be 2x bigger:
theNode.xScale = 2.0
theNode.yScale = 2.0
Note that they have to be floats, not Integers
So to sum it up, .setScale is for spriteNodes and images in general, and .size is for a vast amount of things that aren't images, like labels and more. So if you are using a sprite, use .setScale. If you are resizing a block use .size.
If you believe that this is the right answer, please mark it as so so it helps anyone else with this issue. Thanks!
I alway resize using the .size property since I believe a node's physics body does not automatically scale when you use the .setScale / .xScale / .yScale. If you scale the node, you will need to redo the size of the physicsBody.
I have a sprite like this:
let sprite = SKSpriteNode(imageNamed: "sprite.png")
print("sprite height: \(sprite.size.height)") //results to 150
sprite.zRotation = 90 * degreesToRadians //turns sprite 90 Degrees
Results to this:
However:
print("sprite height: \(sprite.size.height)") //results to 150
Sprite height is still 150 even though it takes far less space height-wise.
Is there a way to get the actual height of a sprite after zrotating it? I know I could easily work around this with in the example above but my real problem is that I have various sprites at various zrotations and I'm trying to make sure that all of them are fully visible on screen.
So basically I have a sprite (red bar), an anchor point at 0,0 (blue dot) and visible screen (black frame).
I zrotate the sprites to random angles using arc4random_uniform but some sprites end up not being completely visible on screen. Basically I would have to know the height of the green arrow or assign it as the anchorPoint after zrotation. Or perhaps there are other ways that I have not thought of. All help appreciated!
0x141E's comment works.
sprite.frame.size.height
results to correct outcome.
I have to draw a rectangular shape which appears to grow.
My Approach
1.i made a UIView subclass,say DrwArrow and in its draw rect method i am drawing my shape I am initializing it with the height required.
i am now adding the DrwArrow object on my view and setting its center at the required point and then applying rotation at the required angle.
3.In order to apply scale animation i am setting its rect to 0 height and the scaling it to the required height.
Issue
The approach works fine when the rectangle is either vertical or horizontal but when the angle is not a multiple of 90 then it scales erroneously, I guess because the height of rect changes due to rotation applied..
I need suggestion regarding the approach i should follow.
Before downvoting please gv rsn so that i can improve.