Coordinate issues in iOS Game templates

Source: Internet
Author: User

Coordinate issues in iOS Game templates

IOS Game usually has a horizontal screen, so what will happen when the screen is vertical. The cup has occurred, and the coordinates have encountered problems.

When the horizontal screen is displayed, the coordinate origin is in the lower left corner (under the Game template below). The size is 1024*768, which is the same as that of the iPad Air, why is it the size? (maybe Apple wants us to have an ipad to play games instead of mobile phones ). When you use devices of other sizes (such as the iPhone), the size of scene has not changed. What does this mean? That is to say, when you create a 100*100 square in scene, it is displayed as a rectangle on the screen.

Okay, let's start with the question: what happened when the screen was standing upright. Okay. I added the following code to the GameScene class:

This function outputs the coordinates of touch points on the screen.

In turn, I touch the lower left, upper left, lower right, and upper right (I use the 5s simulator) and output the coordinates as follows:

Found? The leftmost coordinate does not start from 0, and height is not the expected 1024. What happened? We can see that the coordinates of the width start from 297 to 727, and the height is from 0 to 768. It is easy to understand the height, that is, the height of the original horizontal screen stretched to the height of the vertical screen, which is easy to understand. In the Game template, the height is 768 points (points) (similar to the concept), a single point may represent a pixel in the landscape screen, while a point in the landscape screen represents two pixels. So since the height is stretched, the width is to compress the point, but we are cruel to find that it is not !! The x coordinate in the lower left corner of the width does not start from 0, but about 297. Why? I verified the coordinates of the landscape screen again.

In turn, the lower left, upper left, lower right, and upper right

We found the problem. The coordinates of the width are from 0 to 1024, but the coordinates of the height are from 97 to 668. Why? (I do not know ~~~~~~~~~~~)

To sum up, the width of the horizontal screen corresponds to the height of the vertical screen, which is no problem. The height and width of the landscape screen exceed the screen. The specific reason may be related to the design model. If a great God sees this article and knows this problem, please do not give me any further advice.

So how can we solve this problem, that is, to change the width from 0 to 1024, and to change the height from 0 to 768. My solution is to instantly enter another Scene when entering GameScene, and set the size of another Scene to self. size. Create a new class in the project. The class inherits from SKScene and is named PlayScene. Then, write the following code in GameScnene:

Then, when you enter GameScene, the coordinates of PlayScene are changed from 0 to 768 and from 0.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.