2017년 1월 21일 토요일

NFC Cup Game for AI2


Introduction

This tutorial will demonstrate App Inventor's Near Field Communication (NFC) capabilities. You'll build an application that will simulate the shell game by writing messages to NFC tags in setup mode and reading them in play mode. In order to get the full experience from this application, you will need to purchase readable, writable, NFC tags.
IMPORTANT: Applications built with the NFC component will not respond to tags while in live development mode. To test your application, you must build your app and download the APK to your phone. (How do I build an APK?)

Getting Started

Go to the App Inventor website, begin a new project titled 'BallShuffle', and connect your phone. Download the following images onto your computer and upload them into your project. (Right-click or ctrl-click and choose Save Image. Remember where you put them!)

Set up the Components

The user interface will consist of a menu at the top that lets you switch between the “setup” and “play” modes, a menu that will appear during setup mode and disappear during play mode, and in image that will change based on NFC input and output.
Component TypePalette GroupWhat You'll Name ItPurpose of ComponentComponent Settings
LabelUser InterfaceMainMenuLabelTitle bar for main menuText: Main Menu; Font: Bold; Fontsize: 16
HorizontalArrangementLayoutMainMenuArrangementLine up two "mode" buttons side by sideWidth: Fill Parent; Height: Automatic
ButtonUser InterfaceSetupModeButtonChange to setup modeText: Setup; Width: Fill Parent
ButtonUser InterfacePlayModeButtonChange to play modeText: Play; Width: Fill Parent
VerticalArrangementLayoutSetupArrangementWidth: Fill Parent; Height: Fill Parent
VerticalArrangementLayoutSetupMenuArrangementLine up the setup menu optionsWidth: Fill Parent; Height: Fill Parent
LabelUser InterfaceSetupMenuLabelTitle bar for setup menuText: SetupMenu; Font: Bold
HorizontalArrangementLayoutSetupButtonArrangementHold the buttons for setup menuWidth: Fill Parent; Height: Fill Parent
ButtonUser InterfaceBallButtonLets user define NFC tag as "ball"Text: Ball; Width: Fill Parent
ButtonUser InterfaceCupButtonLets user define NFC tag as "cup"Text: Cup; Width: Fill Parent
LabelUser InterfaceInstructionLabelGive the user instructions for the mode they are inText: -; Width: Fill Parent
ImageUser InterfaceImage1Place holder for imagesHeight: 200 pixels; Width: Fill Parent; Picture: questionmark.png
Horizontal ArrangementLayoutImageArrangementAllows you to center the imageAlignHorizontal: Center; Width: Fill Parent;
LabelUser InterfaceResultsLabelShow results of user's actionsText: -; Width: Fill Parent
NearFieldSensorsNearField1Gives access to NFC capabilitiesReadMode: checked;

Add Behaviors to the Components

We're going to build all of the menus and make the menus work, then we'll modify them to incorperate the NearField component.
To start, we're going to create several variables to make it easier to reference text that will be reused throughout the application:
Next we're going to define the initialization behavior and define the button behavior:
When the screen is initialized, we want the app to display the question mark picture and the setup instructions. Since the SetupMenuArrangement is initially set to visible, we don't have to address it in the Screen1.Initialize block. When the user clicks on the SetupModeButton or on the PlayModeButton, then we want to change the SetupMenuArrangement to visible or invisible respectively. In addition, when the user clicks one of these two buttons, we want to make sure that the correct instructions for that mode is displayed and that the image resets to the question mark picture. When the user clicks on BallButton, we want the image to a change picture of the ball and the CupButton should change the image to a picture of the cup.
The blocks should look like the image below and the buttons should correctly toggle all of the images
Now that the interface works correctly, we're going to add the NFC functionality to the app. We're going to modify the 5 blocks by adding the correct NFC behavior to each one. This will make the buttons turn on read mode when the
Now, we define the app's behavior when a tag is written. When the app writes a tag, we want the the instructions to update, we want the picture to reset, and we want to change NearField1.ReadMode to true so that the tag isn't accidentally overwritten.
Finally, we're going to define the tag read block which contains the majority of the logic for the app. If the app is in play mode and it reads a tag that says "ball," then it will display the victory message and show a picture of a ball. If it reads "cup," then it will display the loss message and show a picture of a cup. Finally, if it doesn't recognize the message, it will display an error message and append the message to the end.
Finally, here's a screen shot of the entire completed app

Download Source Code

If you'd like to work with this sample in App Inventor, download the source code to your computer, then open App Inventor, go to the My Projects page, and choose More Actions | Upload Source.
Done with CupGame? Return to to the other App Inventor 2 tutorials here.
Tutorial Version: App Inventor 2
Tutorial Difficulty: Advanced
Tutorial Type: NFC (Near Field Comm.) Game


댓글 없음:

댓글 쓰기