SKUtilities2
IntroductionSpriteKit Utilities 2 DiscussionThis is a collection of functions, classes, and categories to greatly simplify and speed up development with SpriteKit. Available for download at GitHub Documentation available at GitHub Pages Miscellaneous:
Classes
ProtocolsCategoriesGroupsCGPoint HelpersGroup members:
Bezier CalculationsGroup members:
LoggingGroup members:
Common ConstantsGroup members:
CGVector HelpersGroup members:
OrientationGroup members:
Coordinate Format ConversionsGroup members:
SKUButton EnumsGroup members:
Number InterpolationGroup members:
Distance FunctionsGroup members:
Random NumbersGroup members:Functions
bezierPointCGPoint bezierPoint ( CGFloat t, CGPoint point0, CGPoint point1, CGPoint point2, CGPoint point3); ParametersReturn ValueCGPoint value. DiscussionReturns a point on a bezier curve. Provide it with the anchor points (point0 and point3) and the handle points (point1 and point2) and the value at which to evaluate between 0.0 and 1.0 (0 being point0 and 1.0 being point3). See Also bezierTValueAtXValuedouble bezierTValueAtXValue ( double x, double p0x, double p1x, double p2x, double p3x); ParametersReturn Valuet value for given x value - you can then get the y value from the previous function DiscussionSince bezier paths are distributed more densely in some areas, while more sparse in others, sometimes you need to be able to get the point at a certain x value instead of a t value. Provide this function with the x values of the curve and what x value you need the y value for. See Also centerOSCursorInWindowvoid centerOSCursorInWindow(); DiscussionCenters the Mac OS Cursor in the center of the window. Typically used incoordination of mouseMoved events. Mac only, but harmless on other platforms. See Also clipFloatWithinRangeCGFloat clipFloatWithinRange ( CGFloat value, CGFloat minimum, CGFloat maximum); ParametersDiscussionReturns a CGFloat clipped within the range provided. See Also clipIntegerWithinRangeNSInteger clipIntegerWithinRange ( NSInteger value, NSInteger minimum, NSInteger maximum); ParametersDiscussionReturns an NSInteger clipped within the range provided. See Also distanceBetweenCGFloat distanceBetween ( CGPoint pointA, CGPoint pointB); ParametersDiscussionReturns a CGFloat value measuring the distance between two CGPoint values. See Also distanceBetweenIsWithinXDistancebool distanceBetweenIsWithinXDistance ( CGPoint pointA, CGPoint pointB, CGFloat xDistance); ParametersDiscussionReturns a bool value determining if the distance between two points is less than a predetermined maximum value. See Also distanceBetweenIsWithinXDistancePreSquaredbool distanceBetweenIsWithinXDistancePreSquared ( CGPoint pointA, CGPoint pointB, CGFloat xDistancePresquared); ParametersDiscussionReturns a bool value determining if the distance between two points is less than a predetermined maximum value. This is a small optimization over the previous function as it saves the step of squaring the x value. See Also getCGPointFromStringCGPoint getCGPointFromString ( NSString *string); ParametersDiscussionReturns a CGPoint struct converted from a properly formatted string. See Also getStringFromPointNSString* getStringFromPoint ( CGPoint location); ParametersDiscussionReturns a properly formatted NSString to later be converted back to a CGPoint. This is useful for saving to plist files. See Also hideOSCursorvoid hideOSCursor( BOOL hide); ParametersDiscussionUsed to show or hide the OS Cursor. Mac only, but harmless on other platforms. See Also linearInterpolationBetweenFloatValuesCGFloat linearInterpolationBetweenFloatValues ( CGFloat valueA, CGFloat valueB, CGFloat pointBetween, bool clipped); ParametersDiscussionReturns a CGFloat interpolated linearly between two values. Clipped determines whether it can go beyond the bounds of the values. See Also midPointOfRectCGPoint midPointOfRect ( CGRect rect); ParametersDiscussionReturns a CGPoint struct positioned at the midpoint of a CGRect, including the offset of the origin of the CGRect. See Also midPointOfSizeCGPoint midPointOfSize ( CGSize size); ParametersDiscussionReturns a CGPoint struct positioned at the midpoint of a CGSize struct. See Also orientToFromDownFaceCGFloat orientToFromDownFace ( CGPoint facing, CGPoint from); ParametersDiscussionReturns a float value in radians to rotate a node at position *from* to face *facing*, when the node is facing down at 0 rotation. See Also orientToFromLeftFaceCGFloat orientToFromLeftFace ( CGPoint facing, CGPoint from); ParametersDiscussionReturns a float value in radians to rotate a node at position *from* to face *facing*, when the node is facing left at 0 rotation. See Also orientToFromRightFaceCGFloat orientToFromRightFace ( CGPoint facing, CGPoint from); ParametersDiscussionReturns a float value in radians to rotate a node at position *from* to face *facing*, when the node is facing right at 0 rotation. See Also orientToFromUpFaceCGFloat orientToFromUpFace ( CGPoint facing, CGPoint from); ParametersDiscussionReturns a float value in radians to rotate a node at position *from* to face *facing*, when the node is facing up at 0 rotation. See Also pointAddCGPoint pointAdd ( CGPoint pointA, CGPoint pointB); ParametersDiscussionReturns a CGPoint struct as a sum from both CGPoint input parameters. See Also pointAddValueCGPoint pointAddValue ( CGPoint point, CGFloat value); ParametersDiscussionReturns a CGPoint struct with value added to both dimensions See Also pointFromCGSizeCGPoint pointFromCGSize ( CGSize size); ParametersDiscussionReturns a CGPoint struct converted from a CGSize See Also pointFromCGVectorCGPoint pointFromCGVector ( CGVector vector); ParametersDiscussionReturns a CGPoint struct converted from a CGVector See Also pointInterpolationLinearBetweenTwoPointsCGPoint pointInterpolationLinearBetweenTwoPoints ( CGPoint pointA, CGPoint pointB, CGFloat factorBetween); ParametersDiscussionReturns a CGPoint struct positioned at a varying value between two points, based on the input of "factorBetween" See Also pointInverseCGPoint pointInverse ( CGPoint point); ParametersDiscussionReturns a CGPoint struct with negated values. See Also pointIsBehindVictimbool pointIsBehindVictim ( CGPoint origin, CGPoint victim, CGVector normalVictimFacingVector, CGFloat latitude); ParametersDiscussionReturns a bool determining whether origin is behind victim, based on victim's facing direction. pointIsZeroBOOL pointIsZero ( CGPoint point); ParametersDiscussionReturns a boolean stating whether the given point was equal to CGPointZero or not. pointMultiplyByFactorCGPoint pointMultiplyByFactor ( CGPoint point, CGFloat factor); ParametersDiscussionReturns a CGPoint that is the product of multiplying both values by the same factor. See Also pointMultiplyByPointCGPoint pointMultiplyByPoint ( CGPoint pointA, CGPoint pointB); ParametersDiscussionReturns a CGPoint that is the product of two other CGPoints (Ax * Bx, Ay * By) See Also pointRelativeToSceneCGPoint pointRelativeToScene ( SKScene *scene, CGPoint point); ParametersDiscussionReturns a CGPoint that is the result of multiplying the scene size by the point provided See Also pointStepTowardsPointWithIntervalCGPoint pointStepTowardsPointWithInterval ( CGPoint origin, CGPoint destination, CFTimeInterval interval, CFTimeInterval maxInterval, CGFloat speed, CGFloat speedModifiers); Parameters
DiscussionReturns a CGPoint struct that is the result of movement based on the factors provided See Also pointStepVectorFromPointCGPoint pointStepVectorFromPoint ( CGPoint origin, CGVector normalVector, CGFloat distance); Parameters
DiscussionReturns a CGPoint struct that is the result of movement based on the factors provided See Also pointStepVectorFromPointWithIntervalCGPoint pointStepVectorFromPointWithInterval ( CGPoint origin, CGVector normalVector, CFTimeInterval interval, CFTimeInterval maxInterval, CGFloat speed, CGFloat speedModifiers); Parameters
DiscussionReturns a CGPoint struct that is the result of movement based on the factors provided See Also rampToValueCGFloat rampToValue ( CGFloat idealValue, CGFloat currentValue, CGFloat stepValue); ParametersDiscussionReturns a CGFloat modifying currentValue that iteratively changes over time to become closer to idealValue in iterations of stepValue See Also randomFloatBetweenZeroAndHighendCGFloat randomFloatBetweenZeroAndHighend ( CGFloat highend); ParametersDiscussionReturns a CGFloat value randomized between zero and a higher value. See Also randomUnsignedIntegerBetweenTwoValuesu_int32_t randomUnsignedIntegerBetweenTwoValues ( u_int32_t lowend, u_int32_t highend); ParametersReturn Valuerandom u_int32_t value DiscussionReturns a u_int_32_t value randomized between two other values. See Also reverseLinearInterpolationBetweenFloatValuesCGFloat reverseLinearInterpolationBetweenFloatValues ( CGFloat valueA, CGFloat valueB, CGFloat valueBetween, bool clipped); ParametersDiscussionReturns a CGFloat of the value of pointBetween interpolated linearly between two values. Clipped determines whether it can go beyond the bounds of the values. If that didn't make sense, it's a reversal of what linearInterpolationBetweenFloatValues does - it will return the value of "pointBetween" from linearInterpolationBetweenFloatValues. See Also SKULogvoid SKULog( NSInteger verbosityLevelRequired, NSString *format, ...); DiscussionPrints text to the console using NSLog, but only prints if verbosityLevelRequired is lower than SKUSharedUtilities.verbosityLevel. Also only prints in compiler debug mode. Language:
See Also SKULogBinaryvoid SKULogBinary( uint32_t value); ParametersDiscussionPrints the 0s and 1s of a number. Useful for visualizing flags. vectorAddCGVector vectorAdd ( CGVector vectorA, CGVector vectorB); ParametersDiscussionReturns a CGVector that is the sum of two other CGVectors See Also vectorDistanceCGFloat vectorDistance ( CGVector vector); ParametersReturn Valuedistance value DiscussionCalculates the distance of a vector vectorFacingPointCGVector vectorFacingPoint ( CGPoint destination, CGPoint origin, bool normalize); ParametersDiscussionReturns a CGVector that would face the destination point from the origin point vectorFromCGPointCGVector vectorFromCGPoint ( CGPoint point); ParametersDiscussionReturns a CGVector struct converted from a CGPoint struct. See Also vectorFromCGSizeCGVector vectorFromCGSize ( CGSize size); ParametersDiscussionReturns a CGVector struct converted from a CGSize struct. See Also vectorFromDegreeCGVector vectorFromDegree ( CGFloat degreeAngle); ParametersDiscussionReturns a normal CGVector converted from a degree value vectorFromRadianCGVector vectorFromRadian ( CGFloat radianAngle); ParametersDiscussionReturns a normal CGVector converted from a radian value vectorInverseCGVector vectorInverse ( CGVector vector); ParametersDiscussionReturns a CGVector struct with negated values. See Also vectorIsZeroBOOL vectorIsZero ( CGVector vector); ParametersReturn Valueboolean determining whether it was equal to zero or not DiscussionChecks if a vector is equal to CGVectorZero vectorMultiplyByFactorCGVector vectorMultiplyByFactor ( CGVector vector, CGFloat factor); ParametersDiscussionReturns a CGVector that is the product of multiplying both values by the same factor. See Also vectorMultiplyByVectorCGVector vectorMultiplyByVector ( CGVector vectorA, CGVector vectorB); ParametersDiscussionReturns a CGVector that is the product of two other CGVectors (dx * dx, dy * dy) See Also vectorNormalizeCGVector vectorNormalize ( CGVector vector); ParametersDiscussionReturns a CGVector normalized to have a distance of 1.0. Constants
HIDDENstatic const BOOL HIDDEN = 1; DiscussionBOOLEAN determining visibility For node.hidden properties, this is more intuitive than YES/NO node.hidden = VISIBLE; //this is visible node.hidden = HIDDEN; //this is hidden kSKUDegToRadConvFactorstatic const CGFloat kSKUDegToRadConvFactor = 0.017453292519943295; // pi/180 DiscussionConstant value to convert from degrees to radians. (0.017453292519943295) kSKUNavConstantCurrentFocusedNodeextern NSString* const kSKUNavConstantCurrentFocusedNode; DiscussionConstant for retrieving whatever node is currently focused. kSKUPadFromPhoneScalestatic const CGFloat kSKUPadFromPhoneScale = ( ( CGFloat) 2.40003840061441); DiscussionConstant approximate value (2.40003840061441) good for scaling assets. kSKUPadToPhoneScalestatic const CGFloat kSKUPadToPhoneScale = ( CGFloat) 0.41666; DiscussionConstant approximate value (0.41666) good for scaling assets. kSKUPhoneFromPadScalestatic const CGFloat kSKUPhoneFromPadScale = ( CGFloat) 0.41666; DiscussionConstant approximate value (0.41666) good for scaling assets. kSKUPhoneToPadScalestatic const CGFloat kSKUPhoneToPadScale = ( CGFloat) 2.40003840061441; //likely very low usage as it would be upscaling DiscussionConstant approximate value (2.40003840061441) good for scaling assets. kSKURadToDegConvFactorstatic const CGFloat kSKURadToDegConvFactor = 57.29577951308232; // 180/pi DiscussionConstant value to convert from radians to degrees. (57.29577951308232) kSKURemoteInteractionOffextern NSString* const kSKURemoteInteractionOff; DiscussionConstant for posting notification to turn user interface interaction off for the remote and controllers on tvOS. kSKURemoteInteractionOnextern NSString* const kSKURemoteInteractionOn; DiscussionConstant for posting notification to turn user interface interaction on for the remote and controllers on tvOS. VISIBLEstatic const BOOL VISIBLE = 0; DiscussionBOOLEAN determining visibility For node.hidden properties, this is more intuitive than YES/NO node.hidden = VISIBLE; //this is visible node.hidden = HIDDEN; //this is hidden Typedefs
kSKUButtonMethodstypedef enum { kSKUButtonMethodPostNotification = 1, kSKUButtonMethodDelegate = 1 << 1, kSKUButtonMethodRunActions = 1 << 2, } kSKUButtonMethods; Constants
DiscussionEnumerator/flags for determining how buttons send signals. See Also kSKUButtonStatestypedef enum { kSKUButtonStateUndefined = 1 << 0, kSKUButtonStateDefault = 1 << 1, kSKUButtonStatePressed = 1 << 2, kSKUButtonStatePressedOutOfBounds = 1 << 3, kSKUButtonStateHovered = 1 << 4, kSKUButtonStateDisabled = 1 << 5, } kSKUButtonStates; Constants
DiscussionEnumerator/flags for determining the state of buttons. States can coexist, but are unlikely for anything other than Hovered and either Pressed or Default to coincide. See Also kSKUButtonTypestypedef enum { kSKUButtonTypePush = 1, kSKUButtonTypeToggle, kSKUButtonTypeSlider, } kSKUButtonTypes; Constants
DiscussionEnumerator to differentiate button types from each other. See Also kSKUGamepadButtonStatestypedef enum { kSKUGamepadButtonStateBegan, kSKUGamepadButtonStateChanged, kSKUGamepadButtonStateEnded, // kSKUGamepadButtonStateCancelled, } kSKUGamepadButtonStates; Constants
DiscussionEnumeration describing the state of a button press from a gamepad. kSKUGamePadInputstypedef enum { kSKUGamePadInputLeftShoulder = 1 << 0, kSKUGamePadInputLeftTrigger = 1 << 1, kSKUGamePadInputLeftThumbstick = 1 << 2, kSKUGamePadInputRightShoulder = 1 << 3, kSKUGamePadInputRightTrigger = 1 << 4, kSKUGamePadInputRightThumbstick = 1 << 5, kSKUGamePadInputDirectionalPad = 1 << 6, kSKUGamePadInputButtonA = 1 << 7, kSKUGamePadInputButtonB = 1 << 8, kSKUGamePadInputButtonX = 1 << 9, kSKUGamePadInputButtonY = 1 << 10, kSKUGamePadInputButtonPause = 1 << 11, } kSKUGamePadInputs; Constants
DiscussionGamepad buttons enumerated as flags. See Also kSKUGamePadPlayerFlagstypedef enum { kSKUGamePadPlayerFlag1 = 1 << 0, kSKUGamePadPlayerFlag2 = 1 << 1, kSKUGamePadPlayerFlag3 = 1 << 2, kSKUGamePadPlayerFlag4 = 1 << 3, } kSKUGamePadPlayerFlags; DiscussionPlayer IDs enumerated as flags. Used to turn flags for which players can control nav, but you can use it for your own purposes as well. See Also kSKUMouseButtonFlagstypedef enum { kSKUMouseButtonFlagLeft = 1 << 0, kSKUMouseButtonFlagRight = 1 << 1, kSKUMouseButtonFlagOther = 1 << 2, } kSKUMouseButtonFlags; Constants
DiscussionMouse button type enumerator/flags for Mac. Platforms:
See Also kSKUNavModestypedef enum { kSKUNavModeOn = 1, kSKUNavModeOff, kSKUNavModePressed, } kSKUNavModes; Constants
DiscussionNav mode enumerator for focus based navigation. See Also Macro DefinitionsSKUFont#define SKUFont UIFont DiscussionAllows simplification between platforms, using similar classes. On iOS and tvOS, remaps UIImage to SKUImage and UIFont to SKUFont and on OS X, remaps NSImage to SKUImage and NSFont to SKUFont as they are close enough that most methods work on all platforms. Also defines a constant that is only true on OS X for conditional targetting. To get the same result in Swift, use this in a global scope: #if os(OSX) typealias SKUImage = NSImage; typealias SKUFont = NSFont; #else typealias SKUImage = UIImage; typealias SKUFont = UIFont; #endif See Also SKUFont#define SKUFont NSFont DiscussionAllows simplification between platforms, using similar classes. On iOS and tvOS, remaps UIImage to SKUImage and UIFont to SKUFont and on OS X, remaps NSImage to SKUImage and NSFont to SKUFont as they are close enough that most methods work on all platforms. Also defines a constant that is only true on OS X for conditional targetting. To get the same result in Swift, use this in a global scope: #if os(OSX) typealias SKUImage = NSImage; typealias SKUFont = NSFont; #else typealias SKUImage = UIImage; typealias SKUFont = UIFont; #endif See Also SKUImage#define SKUImage NSImage DiscussionAllows simplification between platforms, using similar classes. On iOS and tvOS, remaps UIImage to SKUImage and UIFont to SKUFont and on OS X, remaps NSImage to SKUImage and NSFont to SKUFont as they are close enough that most methods work on all platforms. Also defines a constant that is only true on OS X for conditional targetting. To get the same result in Swift, use this in a global scope: #if os(OSX) typealias SKUImage = NSImage; typealias SKUFont = NSFont; #else typealias SKUImage = UIImage; typealias SKUFont = UIFont; #endif See Also SKUImage_multideclaration_block#if TARGET_OS_IPHONE #define SKUImage UIImage #define SKUFont UIFont #else #define SKUImage NSImage #define SKUFont NSFont #define TARGET_OS_OSX_SKU 1 #endif DiscussionAllows simplification between platforms, using similar classes. On iOS and tvOS, remaps UIImage to SKUImage and UIFont to SKUFont and on OS X, remaps NSImage to SKUImage and NSFont to SKUFont as they are close enough that most methods work on all platforms. Also defines a constant that is only true on OS X for conditional targetting. To get the same result in Swift, use this in a global scope: #if os(OSX) typealias SKUImage = NSImage; typealias SKUFont = NSFont; #else typealias SKUImage = UIImage; typealias SKUFont = UIFont; #endif Language:
See Also SKUSharedUtilities#define SKUSharedUtilities [SKUtilities2 sharedUtilities] DiscussionSimplifies access to the singleton. To get the same result in Swift, use this in a global scope: let SKUSharedUtilities = SKUtilities2.sharedUtilities(); Language:
TARGET_OS_OSX_SKU#define TARGET_OS_OSX_SKU 1 DiscussionAllows simplification between platforms, using similar classes. On iOS and tvOS, remaps UIImage to SKUImage and UIFont to SKUFont and on OS X, remaps NSImage to SKUImage and NSFont to SKUFont as they are close enough that most methods work on all platforms. Also defines a constant that is only true on OS X for conditional targetting. To get the same result in Swift, use this in a global scope: #if os(OSX) typealias SKUImage = NSImage; typealias SKUFont = NSFont; #else typealias SKUImage = UIImage; typealias SKUFont = UIFont; #endif See Also |