Created
February 10, 2015 19:30
-
-
Save Krumelur/8fc95affb285bda9f8b8 to your computer and use it in GitHub Desktop.
CocosSharp async extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CocosExtensions | |
{ | |
/// <summary> | |
/// Runs an action so that it can be awaited. | |
/// </summary> | |
/// <param name="node">Node.</param> | |
/// <param name="actions">Actions.</param> | |
public static Task<bool> RunActionsAsync(this CCNode node, params CCFiniteTimeAction[] actions) | |
{ | |
var tcs = new TaskCompletionSource<bool> (); | |
var allActions = new List<CCFiniteTimeAction> (); | |
allActions.AddRange (actions); | |
allActions.Add (new CCCallFunc (() => tcs.TrySetResult (true))); | |
node.RunActions (allActions.ToArray()); | |
return tcs.Task; | |
} | |
/// <summary> | |
/// Scales a sprite so that it fits into a given rectangle. | |
/// </summary> | |
/// <returns>The applied scaling factor.</returns> | |
/// <param name="sprite">Sprite.</param> | |
/// <param name="rect">Rect.</param> | |
public static float ScaleAspectFit(this CCSprite sprite, CCSize rect) | |
{ | |
var scaleW = rect.Width / sprite.ContentSize.Width; | |
var scaleH = rect.Height / sprite.ContentSize.Height; | |
var scale = Math.Min (scaleW, scaleH); | |
sprite.ScaleTo(new CCSize(sprite.ContentSize.Width * scale, sprite.ContentSize.Height * scale)); | |
return scale; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment