I'm learning Swift and am getting some weird positioning issues in Sprite Kit with an SKLabelNode.
I wrote the following code:
shareText.position = CGPointMake( self.frame.size.width / 2 ,
shareButton.position.y );
This does exactly what I expected. It centers the text horizontally on the screen.
Well, I'm splitting my screen in half visually so I'd expect to divide by 4 and have it show up "centered" on the left half of the screen.
shareText.position = CGPointMake( self.frame.size.width / 4 , shareButton.position.y );
Unfortunately it looks as if the SKLabelNode is at the "0" position related to the frame. In fact, based on how much of the text I can see it looks like it's slightly into the negative.
When I check it's position using NSLog, when I divide by 2, it says its x position is 512. When I divide by 4, it says its x position is 256 (what I'd expect) but visually, it's not even close. I've also tried setting it to shareButton.position.x which is also at 256 (and shows visually exactly how I expect it to) but I get the same results. When I manually set it to around 400, it seems to work.
Is there something wrong with Swift (I now it's a beta) or am I doing something wrong?
So I think I figured out the problem and it had nothing to do with my Swift code but with XCode itself (or the documentation on how XCode functions by default). The Documentation #duci9y linked to in the above comments mentions the following...
"So, a scene is initialized with a height of 1024 and a width of 768, has the origin (0,0) in the lower-left corner, and the (1024,768) coordinate in the upper-right corner."
I found that in my GameScene.sks file, it defaulted to the opposite. A width of 1024 and a height of 768. My game is supposed to be in portrait so I want it to be as the documentation says, NOT how XCode defaulted my project. I'm guessing this means that (0,0) would have been 128 points off the screen to the left rather than the bottom left corner in portrait but in the correct location in landscape.
I'm unsure if this is an issue with XCode 6 beta 4 or if the documentation listed is just out of date, but as soon as I swapped these numbers, it all seemed to work the way I wanted it.
Related
I am trying to make my game character loop around the screen infinitely like this: http://i.imgur.com/F2yuqen.gifv
Basically, there are three characters. One spawns in the middle of the screen, and the other two spawn exactly the screen width away on opposite sides and move with the main player. Once the main player's x position is less than 0, or greater than the screen width, it is placed back into the middle of the screen. This makes it infinite in essence.
It works great in processing, but in swift my variables for screen width self.size.width seem much larger than the actual screen width when I run the game in the iOS simulator. This makes the character movement not seem smooth and if it is cut off on one side, it does not show up on the other side like it does in the first example.
Example: http://i.imgur.com/gLS4ijK.gifv
Again, I think it has something to do with self.size.width not actually being the width of the display in the simulator.
Code:
Figured it out, I had to add
scene.size = skView.bounds.size
to GameViewController.swift
displaycontent = display.newImageRect (rawdata[currentpath][3], screenW*1.1, ((screenW*1.1/1654)*rawdata[currentpath][6]))
displaycontent.anchorY = 0
displaycontent.y = screenH*0.78
My program loads an image from a database to be displayed on the mobile phone's screen, everything works correctly apart from being able to position it with the y coordinates.
The only thing that changes its position is the anchor point 0 puts the top of the image in the centre of the screen, and values from 0.1 - 1 all position it higher. Changing the y position via object.y has zero effect regardless of what I set it as.
(the size settings probably look a bit weird in the first line, but this is because the images are different sizes and need to show the correct proportions on different screen types).
Btw I am using a tabbar widget as the UI (in case that is relevant)
Any help would be appreciated.
Edit: I am aware that displaycontent is bad name for a variable because of its similarity to things like display.contentCenterY for example, this will be changed to prevent any confusion when I look over the code in future.
I went through my code and tried disabling sections to find the culprit and a content mask was preventing me from setting the position of the loaded images within it.
I will look over my masking code and fix it (should be straight forward now I know where the problem started).
If anyone else has a similar problem (where an image or object wont position itself on given coordinates) check your content mask as that may be the issue!
I make a game using XNA. I would like to align sprite in the center of the screen. This works well when fullscreen is set to false. But when I set IsFullScreen to true, it doesn't work.
I activated the console and print the screen size on it. The size is good (1366 * 768). I also print the current position of the mouse pointer, and when I'm at the bottom right corner, it shows 1279*719, that's why my sprites are not center-align. Why ?
width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
_positionStart = new Vector2(width * 0.5F - _startButton.Width * 0.5F, height * 0.5F - 20);
PS : I'm using XNA 4.0 with VS2012
PS2 : sorry for my grammar, I don't speak English fluently :)
Probably because the top left corner of your sprite is drawn in the middle of the screen which means that the sprite will be drawn slightly on the right side and slightly lower too.
Try values lover than 0.5, maybe 0,45, or even less. This should help.
Well, if the other answer didn't work, a longer, but much more efficient way to do this would be too either:
Set a Rectangle() to the object, and make that act as a bounding box, and use the .Center feature to align the center of it.
Divide the height and the width by 2, as you did, but then minus half the height and width of the image itself (If you can't find that then just use a bounding box as above).
Hope this helps, I know this post is a bit old now, but someone else might stumble across this, and appreciate this answer!
Is it possible to make the default CGPoint size for an iOS app "bigger"?
For example, the iPhone's main view is 320 points wide - is it possible to change that to, say, 100 points, and base all calculations on that 100 (as in still be able to use CGRectMake, get sizes etc. normally).
So does anyone know how to subclass UIView in such a way as to make the above work, and, more importantly, would Apple allow it?
Thanks for your time!
Apply a CGAffineTransformMakeScale(320/100, 320/100) transform to your view so that it is scaled, and 100 points will be scaled to 100 * (320/100) = 320 points wide.
Or course, don't use magic numbers like above, but use some constant for the value 100 and something like [UIScreen mainScreen].applicationFrame.size.width (or view.window.screen.applicationFrame.size.width or anything similar) to get the width of the screen instead of the value 320 (because magic numbers are bad, and we never know if the size of the screen will change in any future like the height just changed with iPhone5)
Have a look at UIViews transform property. But this is probably a bad idea: If you rescale your coordinate system, almost all your views are going to be misaligned with the screen's pixels. This will lead to blurry display of text, lines and other graphics.
Why would you want to do this in the first place?
I would think MyImagePicker.frame.center is where I should place the icon (subtract icon.size.width/2 and height /2., but when the picture actually saves, what I thought the cross hair is pointing at is not the middle of the image at all. X seems okay, but Y happens to be some distance off. I can approximate by trial and error, but I would like to be exact.
Help?
The frame actually defines the origin and dimensions of the view in the coordinate system of its superview and is commonly used during layout to adjust the size or position of the view as detailed here.
You most likely want to use MyImagePicker.bounds.size.width / 2 and MyImagePicker.bounds.size.height / 2 to get the actual center of your image picker.
I actually learned this recently by watching the Stanford lecture series on iOS 5 development on iTunes U.