This project is read-only.

Creating your Achievements

Achievement Types

ActivityAchievement - when a particular, one time event has happened.
IncrementableAchievement - when a event that requires multiple user interactions takes place. For example, the user opens the app, and on the 50th time, receives an achievement.
TimedAchievement - when a event that should occur within a given TimeSpan should occur.

Properties

SmallImagePath / LargeImagePath - The URI string which should be used to resolve the Achievements' images.
Title - The Title of the Achievement
Description - The description of the achievement
IsHidden - Whether or not the Achievement should have it's values hidden in the UI.
IsUnlocked - Whether or not the Achievement is unlocked.
AchievementValue - The integer value of the Achievement.


TimedAchievement
Use StartTimer() to begin a timer.
Use ResetTimer() to reset the timer.
Use Failed() to reset the timer.
Use CompletedSuccessfully() to indicate the timer was Achievement was completed successfully.
Check StartTime.HasValue to be sure a Timer has been started.

(note that TimedAchievement is done completely without timers, but relies on the comparison of TimeSpan values.)
(note that attempting to reset the AchievementTime of an Achievemnt is a logical error, and will throw an exception.)
(note that failiing to set an AchievementTime will cause the Achievement to use TimeSpan.Zero instead.)

ActivityAchievement

Use Unlock() to unlock an achievement.

IncrementableAchievement

Use Increment() to add 1 to the current value of the Achievement.

(note that the UnlockAmount should be set to the value which should be achieved to Unlock the Achievement.)


Creating your Achievement Class

Creating your Achievement class is simple - to provide easy serialization using the built in SharpSerializer library,a singleton / non-static class should be used.

/// <summary>
/// A class to hold the achievements of this Application. Created as a singleton so that SharpSerializer could store it, as it does not serialize static properties.
///
///
/// </summary>
public class AppAchievements
{
private static AppAchievements _instance;
private ObservableCollection<Achievement> _allAchievements;
private ActivityAchievement _closedAppAchievement;

/// <summary>
/// Gets or sets the instance.
/// </summary>
/// <value>
/// The instance.
/// </value>
public static AppAchievements Instance
{
get { return _instance ?? (_instance = new AppAchievements()); }
set { _instance = value; }
}

/// <summary>
/// Gets all achievements.
/// </summary>
public ObservableCollection<Achievement> AllAchievements
{
get
{
//add each individual achievement
return _allAchievements ?? (_allAchievements = new ObservableCollection<Achievement>
{
ClosedAppAchievement,
//other achievements here
});
}
}

/// <summary>
/// Gets or sets the closed app achievement.
/// </summary>
/// <value>
/// The closed app achievement.
/// </value>
public ActivityAchievement ClosedAppAchievement
{
get
{
return _closedAppAchievement ?? (_closedAppAchievement = new ActivityAchievement
{
Description = "Closed the app.",
Title =
"I think it's time for me to leave",
IsHidden = false,
SmallImagePath =
"/AchievementSample;component/Images/Small/noun_project_21.png",
LargeImagePath =
"/AchievementSample;component/Images/Big/noun_project_21.png",
AchievementValue = 5,
});
}
set { _closedAppAchievement = value; }
}




Images

For your small image, use a 48x48 transparent PNG with a white body.
For your large image, use a 100x100 transparent PNG with a white body.

note that the images will have a mask applied to them such that they use the "PhoneContrastBackgroundBrush" value.
also note that the large image is completely uo to you -- you choose whether or not / how to display it, so you could use an image of any type / size if you truly wanted.

Saving

Save your Achievements class using the AppSettings class.

private const string AchievementsKey = "achievements";

// Code to execute when the application is launching (eg, from Start)
// This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e)
{
if (AppSettings.Contains(AchievementsKey))
{
AppAchievements.Instance = AppSettings.Get<AppAchievements>(AchievementsKey);
}
}

// Code to execute when the application is activated (brought to foreground)
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
if (AppSettings.Contains(AchievementsKey))
{
AppAchievements.Instance = AppSettings.Get<AppAchievements>(AchievementsKey);
}
}

// Code to execute when the application is deactivated (sent to background)
// This code will not execute when the application is closing
private void Application_Deactivated(object sender, DeactivatedEventArgs e)
{
AppSettings.InsertOrUpdate(AchievementsKey, AppAchievements.Instance);
}

// Code to execute when the application is closing (eg, user hit Back)
// This code will not execute when the application is deactivated
private void Application_Closing(object sender, ClosingEventArgs e)
{
// Ensure that required application state is persisted here.
AppSettings.InsertOrUpdate(AchievementsKey, AppAchievements.Instance);
}

Last edited Dec 30, 2011 at 10:02 AM by weldon, version 6

Comments

No comments yet.