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!
Related
Could someone point me in the right direction for figuring out how to properly write out my code so that all buttons on any particular y-axis would try to fit the width of the current device screen as best as possible?
To be more exact, currently, I have 4 buttons (made from a SKSpriteNode) that need to be positioned within the screen in such a way that they look perfectly fitted. Device screen is portrait, although from a math point of view, that wouldn't matter, only the actual width of the screen. Each button width uses the same texture, thus resulting in identical widths, but the texture it self might change from time to time resulting in each buttons width either to shrink a little or expand.
Right now, my positioning code looks as follows:
btnLvl1.position = CGPointMake(_scrollingNode.size.width/20*3, scrollStart.position.y - 60);
btnLvl2.position = CGPointMake(_scrollingNode.size.width/20*8, scrollStart.position.y - 60);
btnLvl3.position = CGPointMake(_scrollingNode.size.width/20*13, scrollStart.position.y - 60);
btnLvl4.position = CGPointMake(_scrollingNode.size.width/20*18, scrollStart.position.y - 60);
I was hoping someone could recommend a math formula for making sure, regardless of each button width & how many buttons there are, that they would be placed within the screen width as best as possible. A super bonus would be if something could be done for the height of a device screen (but not necessary).
P.S. I did try figuring this one out my self, but I'm not very strong math-wise.
You can use your scene size to "auto-fit" any node. For example, if you want to center a button on the screen:
button.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
Although you can use this:
button.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
I am developing a Ui which has a look & feel like a speedometer of vehicle which has a needle which is fixed from one end & rotate with some angle with that fixed end.
I have done some sort of code for that
self.SIV_Needle.transform =CGAffineTransformMakeRotation((CGFloat(M_PI_2)/180.0) * 80)
Its working but the imageView loss its original x & y coordinates from one end.I have tried lot of things like changing anchor point , center but doesn't seems to work well.
Responding to my comment, OP said the issue was that the control was not rotating around the desired axis. OP was not (I think) clear about what the desired axis is, nor about what the current axis is (I am using axis interchangeably to mean pivot point). There are a few potential fixes.
1) This needle thing is an ImageView, so you could do a simple hack and just make your image bigger or smaller to fix the rotation. To explain: if your needle is rotating around the center of the needle and not the ball part, then simply double the width of the image, such that the needle only takes up the right side and thus the "ball" of the needle is the pivot point.
2) Set the anchor property of the transform to wherever you want it to be rotating around. See this similar SO question & answer.
You need to translate the UI , to compensate the position change comes due to roatation
Try to change the anchor point of the needle imageView and then add the Rotation. I have not tried it, but i think it should work.
You can archive this by putting your pin image in a UIView at desired position and rotating your main UIView.
like;
let myView:UIIView = UIIView(); //set frame of UIView
let pinImgView:UIVImageView(); // set pin image and frame of required position
//--
myView.addSubview(pinImgView);
//--
myView.transform =CGAffineTransformMakeRotation((CGFloat(M_PI_2)/180.0) * 80);
I have made a sample project for this. you can take idea from it;
http://www.filedropper.com/stackover
or
http://wikisend.com/download/263926/StackOver.zip
This is my 3rd day of googling and tinkering with my code, and I'm hopeless, I honestly need help, so please bear with me here.
Explanation
I'll start with explaining the basic concept. There is a 'character' in the middle of the screen, always perfectly centered. Under that layer is a big square, which I will refer to as the canvas. As you move around, the game is actually just simulating the movement, a 'virtual camera' if you will, so the character doesn't actually move, the canvas just moves behind it.
My Problem
In this game, the camera needs to be able to 'zoom' in and out, so to do this, I adjust the size of the canvas and everything else depending on the zoom value. (1 is default) The width and height of the canvas is always 8000 * the zoom value. This is fine, however, I am having major difficulty positioning the canvas so that it remains in the same relative position as you scale in and out - or rather a 'zoom to point' or 'zoom from point' effect. I simply cannot create the math for this.
The Code
local oldScale = g.scale;
local newScale = n.scaleTest.Value;
g.scale = newScale;
g.x = g.x + (g.x * newScale);
g.y = g.y + (g.y * newScale);
g.scale represents the zoom value, everything will be adjusted to this size.
g.x and g.y are the coordinates of the canvas. The math in the code is completely incorrect.
n.scaleTest.Value is my temporary variable for the newly set scale. It can change at any time.
Thoughts / Other Important Information
I can retrieve the AbsolutePosition and AbsoluteSize of the canvas as well. I have considered implementing the distance from the canvas's edges to the character into my calculations, but I am not completely sure how to do this.
Thank you for any help, I would sincerely appreciate it, I am very stuck and don't know who to ask.
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.
I have UIView that isn't big as the whole screen and I want it to appear in the middle of the screen. I did that with the following code:
self.dialogView.center = CGPointMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0);
Unfortunately, when I center it like that, UIView's content appears blurry.
I've fixed it with this:
self.dialogView.center = CGPointMake(self.view.bounds.size.width / 2.0 + 0.5, self.view.bounds.size.height / 2.0);
Now UIView's content appears as it should :)
Can you tell me why that content becomes blurry and why I need to add 0.5 to the left position offset?
The new center is causing the origin of the control's view to fall on a half-pixel, like { 20.5, 20.5 }. Since the screen can't draw only half a pixel, the rendering engine attempts to get the same effect by diffusing the image as best it can, resulting in blurry images.
You need to make sure all your views' origins are whole numbers.
well i think thats due to how the UIView is being placed on the pixel grid of the screen. if you think about it on regular screens (72 px/inch), 0.5 points means 0.5 pixels which would probably cause what you see... thats my theory at least :P