Airplay Tutorial: An Apple TV multiplayer game (1)

Source: Internet
Author: User

Original http://twitter.com/share?url=http%3A%2F%2Fbit.ly%2F1iRy7Gq&via=rwenderlich&text= AIRPLAYTUTORIAL%3A an Apple TV multiplayer quizgame&related=gpambrozio&lang=en&count=horizontal& Counturl=http%3a%2f%2fwww.raywenderlich.com%2f57161%2fairplay-tutorial

Teach you how to make a multiplayer game with different content on IPhone and AppleTV!

AirPlay has been introduced from iOS 5, allowing iOS devices to display content to AppleTV. This has led to the creation of many games, such as appletv as an additional display device for IOS devices.

Gamekit appeared on iOS 3 (when iOS was also called Iphoneos), and it had an interesting feature called "peer-to". This feature is often used to serve as a channel for data communication in multiplayer games.

In this article, you will demonstrate how to create a small game that displays questions and answers through AppleTV using AirPlay and peer-to connect. Each participant can answer a question with his or her IOS device, scoring the player who first answers and answers correctly. The game interface uses the Sprite Kit framework. This article does not involve much of the content of the sprite kit, but it doesn't matter if you're not familiar with the sprite kit. If you want to know about the sprite kit, you can refer to the Sprite kit Tutorial on this site.

And you don't need appletv--. You can even use an emulator to simulate an external display.

Begin

Download the zip file here , unzip it, and get an initial project used in this tutorial.

By looking at the contents of the project first, you will find that there is no code for airplay or multi-player logic except the UI and the game's startup logic.

Now, let's start learning AirPlay and Gamekit.

Create a 2nd interface

First, the IOS device already supports displaying screen mirroring (mirroring) to an external monitor (for example, AppleTV) by default, which does not require you to write any code. Pull up from the bottom of the screen, click the AirPlay button, and you can select any of the external displays:

However, in the game we need to display different content on the IOS device and the external monitor. For example, in this bidding game, AppleTV displays the questions to be raised in the game, and the answer is displayed on the IOS device. This, of course, requires writing code, which is the focus of this article.

However, AirPlay does not have any APIs for exporting to AppleTV. We can think of only the general external display API. That is, you can only use this API if you connect to AppleTV wirelessly via AirPlay, or if you are connecting to AppleTV and monitors with an apple-only cable.

Therefore, if you want to display different content on different screens (for example, a non-mirroring screen), the most important thing is whether you can detect a valid external monitor if you have no connection with the external display type (whether AppleTV or otherwise).

Open ATVIEWCONTROLLER.M and add the following method:

#pragma mark-airplay and extended display - (void)setupoutputscreen {

//Register for screen notifications

Nsnotificationcenter *Center = [nsnotificationcenter defaultcenter];

[Center addobserver:self selector:@selector(screendidconnect :) name:uiscreendidconnectnotification object:nil];

   [ center Addobserver: Self Selector: @selector ( screendiddisconnect:) Name: uiscreendiddisconnectnotification object: nil ;

[Center addobserver:self selector:@selector(screenmodedidchange :) name:uiscreenmodedidchangenotification object:nil];

//Setup screen mirroring for a existing screen

Nsarray *connectedscreens = [uiscreen screens];

if ([connectedscreens count] > 1) {

UIScreen *mainscreen = [uiscreen mainscreen];

for (uiscreen *ascreen in Connectedscreens) {

if (ascreen! = mainscreen) {

//We ' ve found an external -screen!

[self setupmirroringforscreen:ascreen];

break;

}

}

}

}

- (void)screendidconnect:(nsnotification *)anotification { }

- (void)screendiddisconnect:(nsnotification *) Anotification { }

- (void)screenmodedidchange:(nsnotification *)anotification { }

- (void)setupmirroringforscreen:(uiscreen *) Anexternalscreen { }

- (void)disablemirroringoncurrentscreen { }

The Setupoutputscreen method listens to 3 notifications: Connect an external monitor, disconnect a remote monitor, or change the external monitor. But these notifications can only tell you that the monitor has changed-they won't tell you if you're connected to an external monitor.

To include a connection to an external monitor, you need to traverse the [Uiscrennscreens] array, which includes the connected monitors on all devices. As long as it is not a display other than the primary monitor, we can assume that it is an external monitor. This way you can use the Setupmirroringforscreen: method to send a different interface to it.

The next task is to implement all the empty methods.

In the Screendidconnect: method, add:

NSLog(@"A new screen got connected:%@", [anotification object]);

[Self setupmirroringforscreen: [anotification object]];

The object property of notification contains the UIScreen object for the newly connected display. Once the notification object is received, simply print the Log and call Setupmirroringforscreen to turn on mirroring.

Add the following code to the Screendiddisconnect method:

NSLog(@"A screen got disconnected:%@", [anotification object]); [self disablemirroringoncurrentscreen];

Simply print log and close mirroring. Disablemirroringoncurrentscreen is just an empty implementation, and we'll implement it later.

Next, in Screenmodedidchange: Add the following code:

NSLog(@"A screen mode changed:%@", [anotification object]);

[self disablemirroringoncurrentscreen];

[Self setupmirroringforscreen: [anotification object]];

This method resets the mirroring by turning it off and on to ensure that the new Access screen mode and settings will take effect.

Before implementing the Setupmirroringforscreen: method, you need to save the state of some objects.

Add in atviewcontroller.m :

#import "ATAirPlayScene.h"

Find this line under the import statement:

@property (nonatomic, strong) Atmyscene *scene;

Add the following properties:

@property ( nonatomic, Strong uiwindow * Mirroredwindow;

@property ( nonatomic, Strong) UIScreen * Mirroredscreen;

@property ( nonatomic, Strong) Skview * Mirroredscreenview;

@property ( nonatomic, Strong) Atairplayscene * mirroredscene;

These properties are used for the UIWindow and UIScreen of the second display respectively, corresponding to the Skview and SpriteKit Scene (respectively, for displaying problems and options).

These threeproperties store your secondary Uiwindowand uiscreen objects, thecorresponding Skview for Thatscreen, and the S Prite Kit scene with the interface, displays the Questionand answers to the external display.

In the Setupmirroringforscreen: method, add:

Self.mirroredscreen = anexternalscreen;

//Find Max Resolution

Cgsize Max = {0, 0};

Uiscreenmode *maxscreenmode = nil;

for (uiscreenmode *current in Self.mirroredScreen.availableModes) {

if (maxscreenmode = Nil | | current.size.height > Max.height | | current.size.width > Max.width) {

Max = current.size;

Maxscreenmode = current;

}

}

Self.mirroredScreen.currentMode = maxscreenmode;

In the preceding code, the screen of the method parameter is first saved to the Mirroredscreen property for later use. Then traverse the screen's availablemodes to find out the maximum display resolution that will be used to set the currentmode of screens.


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.