Now when you launch the game it will run in portrait.
Activating/deactivating
When a user presses the Windows button on their phone they deactivate your app. When they press back and continue playing, or if they click your apps icon again, they activate it again. By default the game will just resume from where it left off - which is convenient! However you may wish to pause the game to provide a better user experience when they return to the app.
We won’t add this functionality to the example .capx, as we don’t have pause functionality, but this is how you could add it to your own game. Notice that we use the browser object, and I’m assuming you have a function called “Pause” which would pause the game.
Tombstoning
Tombstoning is a bit of a relic from Windows Phone 7, where multitasking apps had to be simulated by the developer through saving and loading states. These days tombstoning is less relevant, but can still happen if the phone’s memory runs out and you resume your app. By default the game will just restart, which isn’t so bad, but you may want to handle it more elegantly.
You can add the following to the example .capx in the Global event sheet. This stores the name of the layout when suspending, and goes to that layout when resuming from tombstone.
I have experimented with saving the complete state of the game using the System object Save and Load events. Unfortunately it looks like this is too much information to save during suspension and will fail, so I don’t recommend it.
You can easily test tombstoning by navigating to Project -> WindowsPhonePluginForConstruct2 Properties, selecting the Debug tab and checking Tombstone upon deactivation while debugging.
Now when playing the game, hit the Windows button and then press the back button. You’ve now tombstoned your app!
Splash screen
This sample .capx is a very small game so you probably don’t see the splash screen for very long. On larger games it will be displayed while the browser and JavaScript are loaded and will take a little longer, so I recommend adding in your own splash screen.
To do this open up the MainPage.xaml file in the Solution Explorer and find the following code:
<Grid Background="[b]#00b0be[/b]">
<Grid x:Name="LayoutRoot">
<Grid.Background>
<ImageBrush ImageSource=[b]"/Assets/SplashScreen.png[/b]" Stretch="None"/>
</Grid.Background>
Emboldened you’ll first see the hexadecimal background colour and the image URI. Change the colour to whatever you please and either just replace the SplashScreen.png file with your own, or add a new one to the project and specify the URI there.
In-app Purchases
For this section we’ll add an in-app purchase so the player needs make a payment to access the purple cap.
Navigate to the Menu event sheet to the example .capx. After expanding the Hats group you should see the following:
Add in the following logic to add in-app purchases on Windows Phone 8. This simply sets the purple hat button to be active when purchased and prompts to purchase when inactive and pressed.
Notice the product ID “PurpleCap”. This is very important and needs to be consistent at every step of the process.
Now export your project and copy c2runtime.js from the exported directory to the Windows Phone 8 project directory.
Currently the app will fail as we haven’t added the IAP to the Windows Phone project. To do that, open up the file WindowsStoreProxy.xml in the Solution Explorer. You should see the following:
<?xml version="1.0" encoding="utf-16" ?>
<ProductListings>
<!-- Durables-->
<ProductListing Key="TestProduct" Purchased="false" Fulfilled="false">
<Name>Windows Phone Plugin Test Product!</Name>
<Description>This is the great-value test product that comes with the Windows Phone Plugin for Construct 2!</Description>
<ProductId>TestProduct</ProductId>
<ProductType>Durable</ProductType>
<FormattedPrice>$299.99</FormattedPrice>
<ImageUri>ms-appdata:///Tuxedo.png</ImageUri>
<Keywords>test;product</Keywords>
<Tag>Additional text</Tag>
</ProductListing>
<ProductListing Key="TestProduct2" Purchased="false" Fulfilled="false">
<Name>Windows Phone Plugin Test Product!</Name>
<Description>This is the great-value test product that comes with the Windows Phone Plugin for Construct 2!</Description>
<ProductId>TestProduct2</ProductId>
<ProductType>Durable</ProductType>
<FormattedPrice>$299.99</FormattedPrice>
<ImageUri>ms-appdata:///Tuxedo.png</ImageUri>
<Keywords>test;product</Keywords>
<Tag>Additional text</Tag>
</ProductListing>
</ProductListings>
<?xml version="1.0" encoding="utf-16" ?>
<ProductListings>
<!-- Durables-->
<ProductListing Key="PurpleCap" Purchased="false" Fulfilled="false">
<Name>Purple Cap</Name>
<Description>This is a great looking purple cap.</Description>
<ProductId>PurpleCap</ProductId>
<ProductType>Durable</ProductType>
<FormattedPrice>$1.99</FormattedPrice>
<ImageUri></ImageUri>
<Keywords>purple;cap</Keywords>
<Tag></Tag>
</ProductListing>
</ProductListings>
We’ve removed the test products from the file and instead added a name, description, product ID, product type, formatted price and keywords. These are parameters that can be viewable to the end user, so ensure they’re relevant to the product. For the purposes for this project we’re using a Durable product type, which means it’s a one-off purchase. You can find out more about Consumables, the other product type that you can purchase unlimited times, here: In-app purchase for Windows Phone 8.
Vibrate
There exists a standard Vibrate action in the Browser object, but unfortunately it doesn’t work on Windows Phone. For that we’ll need to use the Windows Phone object.
To make it vibrate when you launch your character, navigate to the Gameplay event sheet and expand the Fling group. The last condition in the group is the flinging of the character, so add the Vibrate Windows Phone action from the Windows Phone object and set the Vibrate parameter to 0.4.
Now when you export the phone will vibrate when you fire!