1) AppDelegate - the delegate of our project. 2) GameViewController - our project controller. 3) GameScene - the main scene of our game. 4) GameScene.sks is a visual designer of our scene, it was added with the release of iOS 8 , it is used to visually add some elements. |
- (void)viewDidLoad { [super viewDidLoad]; // Configure the view. SKView * skView = (SKView *)self.view; // SKView View. skView.showsFPS = YES; // skView.showsNodeCount = YES; // Node ( ). /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = YES;// // Create and configure the scene. GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"]; // . scene.scaleMode = SKSceneScaleModeAspectFill; // ScaleMode Scene SKSceneScaleModeAspectFill. // Present the scene. // presentScene, skView [skView presentScene:scene]; }
override func viewDidLoad() { super.viewDidLoad() // if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene // GameScene { // Configure the view. let skView = self.view as SKView // SKView UIView skView.showsFPS = true // skView.showsNodeCount = true // Node ( ). /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true // /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill // ScaleMode Scene AspectFill. // presentScene, skView skView.presentScene(scene) } }
showFPS - this property displays the frequency of updates to our scene. showsNodeCount - shows the number of Node (Objects in our scene). ignoressSiblingOrder - applies additional optimization to improve rendering performance. In addition to these, SKView has many other useful properties, look and experiment with them! |
+ (instancetype)unarchiveFromFile:(NSString *)file { /* Retrieve scene file path from the application bundle */ NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"]; /* Unarchive the file to an SKScene object */ NSData *data = [NSData dataWithContentsOfFile:nodePath options:NSDataReadingMappedIfSafe error:nil]; NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [arch setClass:self forClassName:@"SKScene"]; SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey]; [arch finishDecoding]; return scene; }
class func unarchiveFromFile(file : NSString) -> SKNode? { let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") var sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil) var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData) archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene") let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene archiver.finishDecoding() return scene }
Objective c Swift and then we pass the object of our scene as a parameter, now we compile and “voila” our scene is ready! |
#import "GameViewController.h" #import "GameScene.h" @implementation SKScene (Unarchive) + (instancetype)unarchiveFromFile:(NSString *)file { /* Retrieve scene file path from the application bundle */ NSString *nodePath = [[NSBundle mainBundle] pathForResource:file ofType:@"sks"]; /* Unarchive the file to an SKScene object */ NSData *data = [NSData dataWithContentsOfFile:nodePath options:NSDataReadingMappedIfSafe error:nil]; NSKeyedUnarchiver *arch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; [arch setClass:self forClassName:@"SKScene"]; SKScene *scene = [arch decodeObjectForKey:NSKeyedArchiveRootObjectKey]; [arch finishDecoding]; return scene; } @end @implementation GameViewController - (void)viewDidLoad { [super viewDidLoad]; // Configure the view. SKView * skView = (SKView *)self.view; // skView.showsFPS = YES; skView.showsNodeCount = YES; /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = YES; // Create and configure the scene. GameScene *scene = [GameScene unarchiveFromFile:@"GameScene"]; scene.scaleMode = SKSceneScaleModeAspectFill; // Present the scene. [skView presentScene:scene]; } - (BOOL)shouldAutorotate { return YES; } - (NSUInteger)supportedInterfaceOrientations { if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { return UIInterfaceOrientationMaskAllButUpsideDown; } else { return UIInterfaceOrientationMaskAll; } } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } @end
import UIKit import SpriteKit extension SKNode { class func unarchiveFromFile(file : NSString) -> SKNode? { let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") var sceneData = NSData.dataWithContentsOfFile(path, options: .DataReadingMappedIfSafe, error: nil) var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData) archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene") let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene archiver.finishDecoding() return scene } } class GameViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { // Configure the view. let skView = self.view as SKView skView.showsFPS = true skView.showsNodeCount = true /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFill skView.presentScene(scene) } } override func shouldAutorotate() -> Bool { return true } override func supportedInterfaceOrientations() -> Int { if UIDevice.currentDevice().userInterfaceIdiom == .Phone { return Int(UIInterfaceOrientationMask.AllButUpsideDown.toRaw()) } else { return Int(UIInterfaceOrientationMask.All.toRaw()) } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Release any cached data, images, etc that aren't in use. } }
#import "GameScene.h" @implementation GameScene -(void)didMoveToView:(SKView *)view { } @end
import SpriteKit class GameScene: SKScene { override func didMoveToView(view: SKView) { } }
1) size - Determines the size of our scene. 2) scaleMode - Determines the display of our scene. 3) backgroundColor - Determines the color of our scene, the default is gray (as we had noticed above). 4) anchorPoint - Defines the starting point of our scene. 5) view - Our source View. 6) physicsWorld - These are very important properties that are responsible for scene physics (Gravity, speed), we will talk about it in detail later. |
1) didMoveToView: - Called after our scene has been displayed. 2) willMoveFromView: - Called after our scene has been deleted. 3) didChangeSize: - Called when resizing our scene. 4) convertPointToView: - Used to convert coordinates 5) convertPointFromView: - Used to convert coordinates 6) initWithSize: - Called when our scene is initialized. |
Events are needed to write the main logic of our game, They are called exactly in the order in which I wrote! 1) Update: - The main logic of our game is implemented here. 2) didEvaluateActions - Used to calculate our Action - actions. 3) didSimulatePhysics - Used to calculate physical events. 4) didApplyConstriants - Used to calculate restrictions. 5) didFinishUpdate - Called after all the above listed events have completed. |
#import "GameScene.h" @implementation GameScene -(void)didMoveToView:(SKView *)view { [self SceneSetting]; // SceneSetting } -(void)SceneSetting { self.backgroundColor = [SKColor orangeColor]; // } @end
import SpriteKit class GameScene: SKScene { override func didMoveToView(view: SKView) { SceneSetting() // SceneSetting } func SceneSetting() { self.backgroundColor = SKColor.orangeColor() // } }
1) frame - Defines the shape of our Node. 2) position - Determines the position of our Node. 3) zPosition — Determines the position of our Node relative to the Z axis. 4) zRotation - Defines the rotation of our Node in the corners of Euler. 5) xScale - Determines the scale of our Node on the x axis. 6) yScale - Determines the scale of our Node on the y axis. 7) speed - Determines the speed of our Node. 8) alpha - Defines the alpha component of our Node. 9) paused - Returns Yes if our Node does not execute Actions - actions. 10) hidden - Returns Yes if our Node is hidden. 11) isUserInteractionEnabled - Returns Yes if our Node accepts a touch. 12) parent - Returns the parent Node. (If it returns nil, then it is the parent Node itself) 13) children - Returns all descendants of our Node. 14) name - Defines the name of our Node. (Very important property) 15) scene - The scene where our Node is currently located. 16) physicsBody - the Physical Body of Our Node. (A very important property about which we will learn more later) 17) userData - Defines a place where you can record useful information of our Node. 18) reachConstraints - Defines the degree of freedom in IK (Inverse Kinematics) - Action actions. 19) constraints - Defines a list of constraints. |
1) setScale - Set the scale. (Uniformly in X & Y) 2) addChild - Add a child. (Add one Node to another) 3) insertChild - Remove child. 4) removeChildrenInArray - Remove children by the list. 5) removeAllChildren - Remove all descendants. 6) removeFromParent - Remove the parent Node. 7) childNodeWithName - Returns a child by name. 8) enumerateChildNodesWithName - Recalculate all descendants by a specific name. 9) inParentHierarchy - Returns Yes if our Node is a member of the Hierarchy 10) runAction - Execute Action - action. 11) runAction - completion - Execute Action - action, after completion calls the Completion block. 12) runAction - withKey - Perform an Action - action with a given key. 13) hasAction - Returns Yes if our Node executes an Action action 14) actionForKey - Returns an Action - action, with the given key. 15) removeActionForKey - Remove Action - action with the specified key. 16) removeAllActions - Remove all Action - action of our Node. 17) containsPoint - Returns Yes, if our point lies inside our Node / 18) nodeAtPoint - Returns an object of type SKNode located at this point. 19) nodesAtPoint - Returns a list of SKNode objects located at this point. 20) convertPoint - fromNode - Converts and returns coordinates for our coordinate system. 21) convertPoint - toNode - Converts and returns coordinates for NOT our coordinate system. 22) intersectsNode — Returns Yes if our Node intersects with the boundary of another Node. 23) calculateAccumulatedFrame - Returns the coordinates of boundaries, including descendants. 24) locationInNode - Returns the coordinates of the Node that we touched. 25) previousLocationInNode - Returns the coordinates of the Node that we touched on before. |
scene.scaleMode = SKSceneScaleModeResizeFill;
- (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscape; }
scene.scaleMode = .ResizeFill
override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Landscape.toRaw()) }
addChild - Add a child. (Add one Node to another) , which (th) adds our object to the scene. |
#import "GameScene.h" @implementation GameScene -(void)didMoveToView:(SKView *)view { [self SceneSetting]; [self SKSpriteNodeDemo]; // } -(void)SceneSetting { self.backgroundColor = [SKColor orangeColor]; } -(void)SKSpriteNodeDemo { // SKTexture . . SKTexture *Texture = [SKTexture textureWithImageNamed:@"desert_BG"]; // SKSpriteNode . SKTexture, . SKSpriteNode *BackgroundSprite = [SKSpriteNode spriteNodeWithTexture:Texture]; BackgroundSprite.size = CGSizeMake(640, 320); // . BackgroundSprite.position = CGPointMake(0, 0); // . BackgroundSprite.anchorPoint = CGPointMake(0, 0); // . BackgroundSprite.name = @"BackgroundSprite";// . [self addChild:BackgroundSprite];// . // SKSpriteNode . . SKSpriteNode *SimpleSprite = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(50, 50)]; SimpleSprite.position = CGPointMake(200, 150);// . SimpleSprite.zPosition = 1;// Z. SimpleSprite.name = @"SimpleSprite";// . [self addChild:SimpleSprite];// . // SKSpriteNode . . SKSpriteNode *ImageSprite = [SKSpriteNode spriteNodeWithImageNamed:@"DerevoOpora"]; ImageSprite.position = CGPointMake(250, 50);// . ImageSprite.size = CGSizeMake(100, 15);// . ImageSprite.name = @"ImageSprite";// . [self addChild:ImageSprite];// . } @end
import SpriteKit class GameScene: SKScene { override func didMoveToView(view: SKView) { SceneSetting() SKSpriteNodeDemo() // . } func SceneSetting() { self.backgroundColor = SKColor.orangeColor() } func SKSpriteNodeDemo() { // Texture SKTexture. var Texture = SKTexture(imageNamed: "desert_BG") // BackgroundSprite SKSpriteNode. // SKTexture . var BackgroundSprite = SKSpriteNode(texture: Texture) BackgroundSprite.size = CGSizeMake(640, 320) // . BackgroundSprite.position = CGPointMake(0, 0) // . BackgroundSprite.anchorPoint = CGPointMake(0, 0) // . BackgroundSprite.name = "BackgroundSprite" // . self.addChild(BackgroundSprite) // . // SimpleSprite SKSpriteNode. // . var SimpleSprite = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50)) SimpleSprite.position = CGPointMake(200, 150) // . SimpleSprite.zPosition = 1; // Z. SimpleSprite.name = "SimpleSprite" // . self.addChild(SimpleSprite) // . // ImageSprite SKSpriteNode. // . var ImageSprite = SKSpriteNode(imageNamed: "DerevoOpora") ImageSprite.position = CGPointMake(250, 50) // . ImageSprite.size = CGSizeMake(100, 15) // . ImageSprite.name = "ImageSprite" // . self.addChild(ImageSprite) // . } }
-(void)SKShapeNodeDemo { // SKShapeNode . . SKShapeNode *Circle = [SKShapeNode shapeNodeWithCircleOfRadius:20]; Circle.position = CGPointMake(50, 200); // . Circle.lineWidth = 10; // . Circle.strokeColor = [SKColor blueColor]; // . Circle.fillColor = [SKColor redColor]; // . Circle.name = @"Circle"; // . [self addChild:Circle]; // . // SKShapeNode . (CGRectMake). SKShapeNode *Quad = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 50, 50)]; Quad.position = CGPointMake(100, 200); // . Quad.lineWidth = 4; // . Quad.strokeColor = [SKColor whiteColor]; // . Quad.fillColor = [SKColor blackColor]; // . Quad.name = @"Quad"; // . [self addChild:Quad]; // . // SKShapeNode . (CGRectMake). // . SKShapeNode *Ellips = [SKShapeNode shapeNodeWithEllipseInRect:CGRectMake(0, 0, 50, 90)]; Ellips.position = CGPointMake(200, 200); // . Ellips.lineWidth = 2; // . Ellips.strokeColor = [SKColor greenColor]; // . Ellips.fillColor = [SKColor purpleColor]; // . Ellips.glowWidth = 5; // . Ellips.name = @"Ellips"; // . [self addChild:Ellips]; // . // UIBezierPath. (CGRectMake), // . UIBezierPath *RoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 50, 50) cornerRadius:12]; // SKShapeNode . . // , CGPath. SKShapeNode *RoundedRect = [ SKShapeNode shapeNodeWithPath:RoundedRectPath.CGPath centered:YES]; RoundedRect.position = CGPointMake(50, 100); // . RoundedRect.lineWidth = 2; // . RoundedRect.strokeColor = [SKColor blueColor]; // . RoundedRect.fillColor = [SKColor redColor]; // . RoundedRect.name = @"RoundedRect"; // . [self addChild:RoundedRect]; // . UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; // UIBezierPath. [TrianglePath moveToPoint:CGPointMake(0, 0)]; // . [TrianglePath addLineToPoint:CGPointMake(-25, -50)]; // . [TrianglePath addLineToPoint:CGPointMake(25, -50)]; // . [TrianglePath addLineToPoint:CGPointMake(0, 0)]; // . // SKShapeNode . . // , CGPath. SKShapeNode *Triangle = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath centered:YES]; Triangle.position = CGPointMake(200, 70); // . Triangle.lineWidth = 2; // . Triangle.strokeColor = [SKColor blackColor]; // . Triangle.fillColor = [SKColor blueColor]; // . Triangle.name = @"Triangle"; // . [self addChild:Triangle]; // . }
func SKShapeNodeDemo() { // Circle SKShapeNode. . var Circle = SKShapeNode(circleOfRadius: 20) Circle.position = CGPointMake(50, 200) // . Circle.lineWidth = 10 // . Circle.strokeColor = SKColor.blueColor() // . Circle.fillColor = SKColor.redColor() // . Circle.name = "Circle" // . self.addChild(Circle) // . // Quad SKShapeNode. // (CGRectMake). var Quad = SKShapeNode(rect: CGRectMake(0, 0, 50, 50)) Quad.position = CGPointMake(100, 200) // . Quad.lineWidth = 4 // . Quad.strokeColor = SKColor.whiteColor() // . Quad.fillColor = SKColor.blackColor() // . Quad.name = "Quad" // . self.addChild(Quad) // . // Ellips SKShapeNode. // . var Ellips = SKShapeNode(ellipseInRect: CGRectMake(0, 0, 50, 90)) Ellips.position = CGPointMake(200, 200) // . Ellips.lineWidth = 2 // . Ellips.strokeColor = SKColor.greenColor() // . Ellips.fillColor = SKColor.purpleColor() // . Ellips.glowWidth = 5 // . Ellips.name = "Ellips" // . self.addChild(Ellips) // . // RoundedRectPath UIBezierPath. // (CGRectMake), . var RoundedRectPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 50, 50), cornerRadius: 12) // RoundedRect SKShapeNode. // . // , CGPath. var RoundedRect = SKShapeNode(path: RoundedRectPath.CGPath, centered:true) RoundedRect.position = CGPointMake(50, 100) // . RoundedRect.lineWidth = 2 // . RoundedRect.strokeColor = SKColor.blueColor() // . RoundedRect.fillColor = SKColor.redColor() // . RoundedRect.name = "RoundedRect" // . self.addChild(RoundedRect) // . var TrianglePath = UIBezierPath() // UIBezierPath. TrianglePath.moveToPoint(CGPointMake(0, 0)) // . TrianglePath.addLineToPoint(CGPointMake(-25, -50)) // . TrianglePath.addLineToPoint(CGPointMake(25, -50)) // . TrianglePath.addLineToPoint(CGPointMake(0, 0)) // . // Triangle SKShapeNode. // . // , CGPath. var Triangle = SKShapeNode(path: TrianglePath.CGPath, centered: true) Triangle.position = CGPointMake(200, 70) // . Triangle.lineWidth = 2 // . Triangle.strokeColor = SKColor.blackColor() // . Triangle.fillColor = SKColor.blueColor() // . Triangle.name = "Triangle" // . self.addChild(Triangle) // . }
, Circle . , shapeNodeWithCircleOfRadius circleOfRadius (-) . Objective-c Swift , , . , , . lineWidth , (), , . : strokeColor fillColor . • strokeColor — ( ). • fillColor — . , .
, , . |
// SKShapeNode . (CGRectMake). SKShapeNode *Quad = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 50, 50)]; Quad.position = CGPointMake(100, 200); // . Quad.lineWidth = 4; // . Quad.strokeColor = [SKColor whiteColor]; // . Quad.fillColor = [SKColor blackColor]; // . Quad.name = @"Quad"; // . [self addChild:Quad]; // .
// Quad SKShapeNode. // (CGRectMake). var Quad = SKShapeNode(rect: CGRectMake(0, 0, 50, 50)) Quad.position = CGPointMake(100, 200) // . Quad.lineWidth = 4 // . Quad.strokeColor = SKColor.whiteColor() // . Quad.fillColor = SKColor.blackColor() // . Quad.name = "Quad" // . self.addChild(Quad) // .
Quad , , . , (CGRectMake). , , : . : x — X, y — Y. . |
// SKShapeNode . (CGRectMake). // . SKShapeNode *Ellips = [SKShapeNode shapeNodeWithEllipseInRect:CGRectMake(0, 0, 50, 90)]; Ellips.position = CGPointMake(200, 200); // . Ellips.lineWidth = 2; // . Ellips.strokeColor = [SKColor greenColor]; // . Ellips.fillColor = [SKColor purpleColor]; // . Ellips.glowWidth = 5; // . Ellips.name = @"Ellips"; // . [self addChild:Ellips]; // .
// Ellips SKShapeNode. // . var Ellips = SKShapeNode(ellipseInRect: CGRectMake(0, 0, 50, 90)) Ellips.position = CGPointMake(200, 200) // . Ellips.lineWidth = 2 // . Ellips.strokeColor = SKColor.greenColor() // . Ellips.fillColor = SKColor.purpleColor() // . Ellips.glowWidth = 5 // . Ellips.name = "Ellips" // . self.addChild(Ellips) // .
Ellips , . , . shapeNodeWithEllipseInRect ellipseInRect . (CGRectMake) , . , , glowWidth • glowWidth — , , . |
// UIBezierPath. (CGRectMake), // . UIBezierPath *RoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 50, 50) cornerRadius:12]; // SKShapeNode . . // , CGPath. SKShapeNode *RoundedRect = [ SKShapeNode shapeNodeWithPath:RoundedRectPath.CGPath centered:YES]; RoundedRect.position = CGPointMake(50, 100); // . RoundedRect.lineWidth = 2; // . RoundedRect.strokeColor = [SKColor blueColor]; // . RoundedRect.fillColor = [SKColor redColor]; // . RoundedRect.name = @"RoundedRect"; // . [self addChild:RoundedRect]; // .
// RoundedRectPath UIBezierPath. // (CGRectMake), . var RoundedRectPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 50, 50), cornerRadius: 12) // RoundedRect SKShapeNode. // . // , CGPath. var RoundedRect = SKShapeNode(path: RoundedRectPath.CGPath, centered:true) RoundedRect.position = CGPointMake(50, 100) // . RoundedRect.lineWidth = 2 // . RoundedRect.strokeColor = SKColor.blueColor() // . RoundedRect.fillColor = SKColor.redColor() // . RoundedRect.name = "RoundedRect" // . self.addChild(RoundedRect) // .
, RoundedRect . , UIBezierPath , : (CGRectMake) . , () . UIBezierPath , SKShapeNode , . UIBezierPath , centered . , CGPath , ; , ! , — centered , ! , . , . |
UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; // UIBezierPath. [TrianglePath moveToPoint:CGPointMake(0, 0)]; // . [TrianglePath addLineToPoint:CGPointMake(-25, -50)]; // . [TrianglePath addLineToPoint:CGPointMake(25, -50)]; // . [TrianglePath addLineToPoint:CGPointMake(0, 0)]; // . // SKShapeNode . . // , CGPath. SKShapeNode *Triangle = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath centered:YES]; Triangle.position = CGPointMake(200, 70); // . Triangle.lineWidth = 2; // . Triangle.strokeColor = [SKColor blackColor]; // . Triangle.fillColor = [SKColor blueColor]; // . Triangle.name = @"Triangle"; // . [self addChild:Triangle]; // .
var TrianglePath = UIBezierPath() // UIBezierPath. TrianglePath.moveToPoint(CGPointMake(0, 0)) // . TrianglePath.addLineToPoint(CGPointMake(-25, -50)) // . TrianglePath.addLineToPoint(CGPointMake(25, -50)) // . TrianglePath.addLineToPoint(CGPointMake(0, 0)) // . // Triangle SKShapeNode. // . // , CGPath. var Triangle = SKShapeNode(path: TrianglePath.CGPath, centered: true) Triangle.position = CGPointMake(200, 70) // . Triangle.lineWidth = 2 // . Triangle.strokeColor = SKColor.blackColor() // . Triangle.fillColor = SKColor.blueColor() // . Triangle.name = "Triangle" // . self.addChild(Triangle) // .
Triangle . UIBezierPath . . , UIBezierPath , TrianglePath . , . ! , , , . moveToPoint UIBezierPath . (), , , addLineToPoint . , , , ( ). . TrianglePath . SKShapeNode Triangle , «» ! |
CGRectMake(0, 0, 50, 50).
CGRectMake(-25, -25, 50, 50).
#import "GameScene.h" @implementation GameScene -(void)didMoveToView:(SKView *)view { [self SceneSetting]; //[self SKSpriteNodeDemo]; [self SKShapeNodeDemo]; } -(void)SceneSetting { self.backgroundColor = [SKColor orangeColor]; } -(void)SKSpriteNodeDemo { SKTexture *Texture = [SKTexture textureWithImageNamed:@"desert_BG"]; SKSpriteNode *BackgroundSprite = [SKSpriteNode spriteNodeWithTexture:Texture]; BackgroundSprite.size = CGSizeMake(640, 320); BackgroundSprite.position = CGPointMake(0, 0); BackgroundSprite.anchorPoint = CGPointMake(0, 0); BackgroundSprite.name = @"BackgroundSprite"; [self addChild:BackgroundSprite]; SKSpriteNode *SimpleSprite = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(50, 50)]; SimpleSprite.position = CGPointMake(200, 150); SimpleSprite.zPosition = 1; SimpleSprite.name = @"SimpleSprite"; [self addChild:SimpleSprite]; SKSpriteNode *ImageSprite = [SKSpriteNode spriteNodeWithImageNamed:@"DerevoOpora"]; ImageSprite.position = CGPointMake(250, 50); ImageSprite.size = CGSizeMake(100, 15); ImageSprite.name = @"ImageSprite"; [self addChild:ImageSprite]; } -(void)SKShapeNodeDemo { SKShapeNode *Circle = [SKShapeNode shapeNodeWithCircleOfRadius:20]; Circle.position = CGPointMake(50, 200); Circle.lineWidth = 10; Circle.strokeColor = [SKColor blueColor]; Circle.fillColor = [SKColor redColor]; Circle.name = @"Circle"; [self addChild:Circle]; SKShapeNode *Quad = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 50, 50)]; Quad.position = CGPointMake(100, 200); Quad.lineWidth = 4; Quad.strokeColor = [SKColor whiteColor]; Quad.fillColor = [SKColor blackColor]; Quad.name = @"Quad"; [self addChild:Quad]; SKShapeNode *Ellips = [SKShapeNode shapeNodeWithEllipseInRect:CGRectMake(0, 0, 50, 90)]; Ellips.position = CGPointMake(200, 200); Ellips.lineWidth = 2; Ellips.strokeColor = [SKColor greenColor]; Ellips.fillColor = [SKColor purpleColor]; Ellips.glowWidth = 5; Ellips.name = @"Ellips"; [self addChild:Ellips]; UIBezierPath *RoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 50, 50) cornerRadius:12]; SKShapeNode *RoundedRect = [ SKShapeNode shapeNodeWithPath:RoundedRectPath.CGPath centered:YES]; RoundedRect.position = CGPointMake(50, 100); RoundedRect.lineWidth = 2; RoundedRect.strokeColor = [SKColor blueColor]; RoundedRect.fillColor = [SKColor redColor]; RoundedRect.name = @"RoundedRect"; [self addChild:RoundedRect]; UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; [TrianglePath moveToPoint:CGPointMake(0, 0)]; [TrianglePath addLineToPoint:CGPointMake(-25, -50)]; [TrianglePath addLineToPoint:CGPointMake(25, -50)]; [TrianglePath addLineToPoint:CGPointMake(0, 0)]; SKShapeNode *Triangle = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath centered:YES]; Triangle.position = CGPointMake(200, 70); Triangle.lineWidth = 2; Triangle.strokeColor = [SKColor blackColor]; Triangle.fillColor = [SKColor blueColor]; Triangle.name = @"Triangle"; [self addChild:Triangle]; } @end
import SpriteKit class GameScene: SKScene { override func didMoveToView(view: SKView) { SceneSetting() //SKSpriteNodeDemo() SKShapeNodeDemo() } func SceneSetting() { self.backgroundColor = SKColor.orangeColor() } func SKSpriteNodeDemo() { var Texture = SKTexture(imageNamed: "desert_BG") var BackgroundSprite = SKSpriteNode(texture: Texture) BackgroundSprite.size = CGSizeMake(640, 320) BackgroundSprite.position = CGPointMake(0, 0) BackgroundSprite.anchorPoint = CGPointMake(0, 0) BackgroundSprite.name = "BackgroundSprite" self.addChild(BackgroundSprite) var SimpleSprite = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50)) SimpleSprite.position = CGPointMake(200, 150) SimpleSprite.zPosition = 1; SimpleSprite.name = "SimpleSprite" self.addChild(SimpleSprite) var ImageSprite = SKSpriteNode(imageNamed: "DerevoOpora") ImageSprite.position = CGPointMake(250, 50) ImageSprite.size = CGSizeMake(100, 15) ImageSprite.name = "ImageSprite" self.addChild(ImageSprite) } func SKShapeNodeDemo() { var Circle = SKShapeNode(circleOfRadius: 20) Circle.position = CGPointMake(50, 200) Circle.lineWidth = 10 Circle.strokeColor = SKColor.blueColor() Circle.fillColor = SKColor.redColor() Circle.name = "Circle" self.addChild(Circle) var Quad = SKShapeNode(rect: CGRectMake(0, 0, 50, 50)) Quad.position = CGPointMake(100, 200) Quad.lineWidth = 4 Quad.strokeColor = SKColor.whiteColor() Quad.fillColor = SKColor.blackColor() Quad.name = "Quad" self.addChild(Quad) var Ellips = SKShapeNode(ellipseInRect: CGRectMake(0, 0, 50, 90)) Ellips.position = CGPointMake(200, 200) Ellips.lineWidth = 2 Ellips.strokeColor = SKColor.greenColor() Ellips.fillColor = SKColor.purpleColor() Ellips.glowWidth = 5 Ellips.name = "Ellips" self.addChild(Ellips) var RoundedRectPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 50, 50), cornerRadius: 12) var RoundedRect = SKShapeNode(path: RoundedRectPath.CGPath, centered:true) RoundedRect.position = CGPointMake(50, 100) RoundedRect.lineWidth = 2 RoundedRect.strokeColor = SKColor.blueColor() RoundedRect.fillColor = SKColor.redColor() RoundedRect.name = "RoundedRect" self.addChild(RoundedRect) var TrianglePath = UIBezierPath() TrianglePath.moveToPoint(CGPointMake(0, 0)) TrianglePath.addLineToPoint(CGPointMake(-25, -50)) TrianglePath.addLineToPoint(CGPointMake(25, -50)) TrianglePath.addLineToPoint(CGPointMake(0, 0)) var Triangle = SKShapeNode(path: TrianglePath.CGPath, centered: true) Triangle.position = CGPointMake(200, 70) Triangle.lineWidth = 2 Triangle.strokeColor = SKColor.blackColor() Triangle.fillColor = SKColor.blueColor() Triangle.name = "Triangle" self.addChild(Triangle) } }
-(void)SKLabelNodeDemo { // SKLabelNode . . SKLabelNode *First = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; First.position = CGPointMake(280, 200); // . First.fontSize = 25; // . First.fontColor = [SKColor whiteColor]; // . First.color = [SKColor blueColor]; // ( colorBlendFactor). First.colorBlendFactor = 0.5; // colorBlendFactor (0.0 - 1.0) First.text = @"Habra Habr!"; // . First.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter; // . First.name = @"First"; // [self addChild:First]; // . // SKLabelNode . . SKLabelNode *Second = [SKLabelNode labelNodeWithText:@"----"]; Second.fontName = @"Chalkboard SE Bold"; // . Second.fontColor = [SKColor blackColor]; // . Second.position = CGPointMake(280, 50); // . Second.fontSize = 20; // . Second.name = @"Second"; // [self addChild:Second]; // . }
func SKLabelNodeDemo() { // First SKLabelNode. . var First = SKLabelNode(fontNamed: "Chalkduster") First.position = CGPointMake(280, 200) // . First.fontSize = 25; // . First.fontColor = SKColor.whiteColor() // . First.color = SKColor.blueColor() // ( colorBlendFactor). First.colorBlendFactor = 0.5 // colorBlendFactor (0.0 - 1.0) First.text = "Habra Habr!" // . First.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center // . First.name = "First" // self.addChild(First) // . // Second SKLabelNode. . var Second = SKLabelNode(text: "----") Second.fontName = "Chalkboard SE Bold" // . Second.fontColor = SKColor.blackColor() // . Second.position = CGPointMake(280, 50) // . Second.fontSize = 20 // . Second.name = "Second" // self.addChild(Second) // . }
• fontSize — . . • fontColor — . • color — ( colorBlendFactor). • colorBlendFactor — . color . (0.0 — 1.0). • text — . • horizontalAlignmentMode — . , . |
#import "GameScene.h" @implementation GameScene -(void)didMoveToView:(SKView *)view { [self SceneSetting]; //[self SKSpriteNodeDemo]; //[self SKShapeNodeDemo]; [self SKLabelNodeDemo]; } -(void)SceneSetting { self.backgroundColor = [SKColor orangeColor]; } -(void)SKSpriteNodeDemo { SKTexture *Texture = [SKTexture textureWithImageNamed:@"desert_BG"]; SKSpriteNode *BackgroundSprite = [SKSpriteNode spriteNodeWithTexture:Texture]; BackgroundSprite.size = CGSizeMake(640, 320); BackgroundSprite.position = CGPointMake(0, 0); BackgroundSprite.anchorPoint = CGPointMake(0, 0); BackgroundSprite.name = @"BackgroundSprite"; [self addChild:BackgroundSprite]; SKSpriteNode *SimpleSprite = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(50, 50)]; SimpleSprite.position = CGPointMake(200, 150); SimpleSprite.zPosition = 1; SimpleSprite.name = @"SimpleSprite"; [self addChild:SimpleSprite]; SKSpriteNode *ImageSprite = [SKSpriteNode spriteNodeWithImageNamed:@"DerevoOpora"]; ImageSprite.position = CGPointMake(250, 50); ImageSprite.size = CGSizeMake(100, 15); ImageSprite.name = @"ImageSprite"; [self addChild:ImageSprite]; } -(void)SKShapeNodeDemo { SKShapeNode *Circle = [SKShapeNode shapeNodeWithCircleOfRadius:20]; Circle.position = CGPointMake(50, 200); Circle.lineWidth = 10; Circle.strokeColor = [SKColor blueColor]; Circle.fillColor = [SKColor redColor]; Circle.name = @"Circle"; [self addChild:Circle]; SKShapeNode *Quad = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 50, 50)]; Quad.position = CGPointMake(100, 200); Quad.lineWidth = 4; Quad.strokeColor = [SKColor whiteColor]; Quad.fillColor = [SKColor blackColor]; Quad.name = @"Quad"; [self addChild:Quad]; SKShapeNode *Ellips = [SKShapeNode shapeNodeWithEllipseInRect:CGRectMake(0, 0, 50, 90)]; Ellips.position = CGPointMake(200, 200); Ellips.lineWidth = 2; Ellips.strokeColor = [SKColor greenColor]; Ellips.fillColor = [SKColor purpleColor]; Ellips.glowWidth = 5; Ellips.name = @"Ellips"; [self addChild:Ellips]; UIBezierPath *RoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 50, 50) cornerRadius:12]; SKShapeNode *RoundedRect = [ SKShapeNode shapeNodeWithPath:RoundedRectPath.CGPath centered:YES]; RoundedRect.position = CGPointMake(50, 100); RoundedRect.lineWidth = 2; RoundedRect.strokeColor = [SKColor blueColor]; RoundedRect.fillColor = [SKColor redColor]; RoundedRect.name = @"RoundedRect"; [self addChild:RoundedRect]; UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; [TrianglePath moveToPoint:CGPointMake(0,0)]; [TrianglePath addLineToPoint:CGPointMake(-25, -50)]; [TrianglePath addLineToPoint:CGPointMake(25, -50)]; [TrianglePath addLineToPoint:CGPointMake(0, 0)]; SKShapeNode *Triangle = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath centered:YES]; Triangle.position = CGPointMake(200, 70); Triangle.lineWidth = 2; Triangle.strokeColor = [SKColor blackColor]; Triangle.fillColor = [SKColor blueColor]; Triangle.name = @"Triangle"; [self addChild:Triangle]; } -(void)SKLabelNodeDemo { SKLabelNode *First = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; First.position = CGPointMake(280, 200); First.fontSize = 25; First.fontColor = [SKColor whiteColor]; First.color = [SKColor blueColor]; First.colorBlendFactor = 0.5; First.text = @"Habra Habr!"; First.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter; [self addChild:First]; SKLabelNode *Second = [SKLabelNode labelNodeWithText:@"----"]; Second.fontName = @"Chalkboard SE Bold"; Second.fontColor = [SKColor blackColor]; Second.position = CGPointMake(280, 50); Second.fontSize = 20; [self addChild:Second]; } @end
import SpriteKit class GameScene: SKScene { override func didMoveToView(view: SKView) { SceneSetting() //SKSpriteNodeDemo() //SKShapeNodeDemo() SKLabelNodeDemo() } func SceneSetting() { self.backgroundColor = SKColor.orangeColor() } func SKSpriteNodeDemo() { var Texture = SKTexture(imageNamed: "desert_BG") var BackgroundSprite = SKSpriteNode(texture: Texture) BackgroundSprite.size = CGSizeMake(640, 320) BackgroundSprite.position = CGPointMake(0, 0) BackgroundSprite.anchorPoint = CGPointMake(0, 0) BackgroundSprite.name = "BackgroundSprite" self.addChild(BackgroundSprite) var SimpleSprite = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50)) SimpleSprite.position = CGPointMake(200, 150) SimpleSprite.zPosition = 1; SimpleSprite.name = "SimpleSprite" self.addChild(SimpleSprite) var ImageSprite = SKSpriteNode(imageNamed: "DerevoOpora") ImageSprite.position = CGPointMake(250, 50) ImageSprite.size = CGSizeMake(100, 15) ImageSprite.name = "ImageSprite" self.addChild(ImageSprite) } func SKShapeNodeDemo() { var Circle = SKShapeNode(circleOfRadius: 20) Circle.position = CGPointMake(50, 200) Circle.lineWidth = 10 Circle.strokeColor = SKColor.blueColor() Circle.fillColor = SKColor.redColor() Circle.name = "Circle" self.addChild(Circle) var Quad = SKShapeNode(rect: CGRectMake(0, 0, 50, 50)) Quad.position = CGPointMake(100, 200) Quad.lineWidth = 4 Quad.strokeColor = SKColor.whiteColor() Quad.fillColor = SKColor.blackColor() Quad.name = "Quad" self.addChild(Quad) var Ellips = SKShapeNode(ellipseInRect: CGRectMake(0, 0, 50, 90)) Ellips.position = CGPointMake(200, 200) Ellips.lineWidth = 2 Ellips.strokeColor = SKColor.greenColor() Ellips.fillColor = SKColor.purpleColor() Ellips.glowWidth = 5 Ellips.name = "Ellips" self.addChild(Ellips) var RoundedRectPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 50, 50), cornerRadius: 12) var RoundedRect = SKShapeNode(path: RoundedRectPath.CGPath, centered:true) RoundedRect.position = CGPointMake(50, 100) RoundedRect.lineWidth = 2 RoundedRect.strokeColor = SKColor.blueColor() RoundedRect.fillColor = SKColor.redColor() RoundedRect.name = "RoundedRect" self.addChild(RoundedRect) var TrianglePath = UIBezierPath() TrianglePath.moveToPoint(CGPointMake(0, 0)) TrianglePath.addLineToPoint(CGPointMake(-25, -50)) TrianglePath.addLineToPoint(CGPointMake(25, -50)) TrianglePath.addLineToPoint(CGPointMake(0, 0)) var Triangle = SKShapeNode(path: TrianglePath.CGPath, centered: true) Triangle.position = CGPointMake(200, 70) Triangle.lineWidth = 2 Triangle.strokeColor = SKColor.blackColor() Triangle.fillColor = SKColor.blueColor() Triangle.name = "Triangle" self.addChild(Triangle) } func SKLabelNodeDemo() { var First = SKLabelNode(fontNamed: "Chalkduster") First.position = CGPointMake(280, 200) First.fontSize = 25; First.fontColor = SKColor.whiteColor() First.color = SKColor.blueColor() First.colorBlendFactor = 0.5 First.text = "Habra Habr!" First.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center self.addChild(First) var Second = SKLabelNode(text: "----") Second.fontName = "Chalkboard SE Bold" Second.fontColor = SKColor.blackColor() Second.position = CGPointMake(280, 50) Second.fontSize = 20 self.addChild(Second) } }
3 . . , , , : (, , , ). ? |
1) dynamic — No, . 2) usesPreciseCollisionDetection — NO. YES, , , , . 3) allowsRotation — NO. YES ! 4) pinned — NO, YES , , . 5) resting — YES, . 6) friction — , . . 7) charge — . . 8) restitution — ( , ) ( 0.1 — 1.0). 0.2. 9) linearDamping — . ( 0.1 — 1.0). 0.1. 10) angularDamping — . ( 0.1 — 1.0). 0.1. 11) density — . 0.1. 12) mass — . 13) area — . 14) affectedByGravity — YES, NO . 15) fieldBitMask — , 16) categoryBitMask — , . 17) collisionBitMask — , . 18) contactTestBitMask — , 19) joints — , . (Joint — , ) 20) node — Node . 21) velocity — . 22) angularVelocity — . |
1) bodyWithCircleOfRadius — r — . 2) bodyWithCircleOfRadius — r — center — , . 3) bodyWithRectangleOfSize — s — . 4) bodyWithRectangleOfSize — s — center — , . 5) bodyWithPolygonFromPath — path — . 6) bodyWithEdgeFromPoint — p1 — p2 — , . 7) bodyWithEdgeChainFromPath — path — , — . 8) bodyWithEdgeLoopFromPath — path — , . 9) bodyWithEdgeLoopFromRect — rect — , . 10) bodyWithTexture — texture — size — . 11) bodyWithTexture — texture — alphaThreshold — size — , alphaThreshold. 12) bodyWithBodies — bodies — . |
1) applyForce — force — . 2) applyForce — force — point — . ( — ) 3) applyTorque — torque — . 4) applyImpulse — impulse — . 5) applyImpulse — impulse — point — . ( — ) 6) applyAngularImpulse — impulse — . 7) allContactedBodies — Returns an array of physical bodies that are in contact with the given body. |
Let's find our ViewController and add this property to our SKView : Objective-c Swift , . , ! |
, . . Objective-c Swift |
-(void)CreatePhysics { // SKShapeNode . (CGRectMake). SKShapeNode *RectanglePhysics = [SKShapeNode shapeNodeWithRect:CGRectMake(-580/2, -10, 580, 20)]; RectanglePhysics.position = CGPointMake(280, 40); // . RectanglePhysics.fillColor = [SKColor whiteColor]; // . RectanglePhysics.name = @"Rectangle"; // . RectanglePhysics.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:RectanglePhysics.frame.size]; // . RectanglePhysics.physicsBody.dynamic = NO; // . [self addChild:RectanglePhysics]; // . // SKShapeNode . . SKShapeNode *CirclePhysics = [SKShapeNode shapeNodeWithCircleOfRadius:40]; CirclePhysics.position = CGPointMake(100, 160); // . CirclePhysics.strokeColor = [SKColor greenColor]; // . CirclePhysics.lineWidth = 5; // . CirclePhysics.name = @"Circle"; // . CirclePhysics.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:40]; // . CirclePhysics.physicsBody.dynamic = YES; // . [self addChild:CirclePhysics]; // . // SKTexture . . SKTexture *Texture = [SKTexture textureWithImageNamed:@"DerevoOpora"]; // SKSpriteNode . SKTexture. SKSpriteNode *TexturePhysics = [SKSpriteNode spriteNodeWithTexture:Texture]; TexturePhysics.position = CGPointMake(200, 180); // . TexturePhysics.size = CGSizeMake(100, 30); // . TexturePhysics.name = @"TexturePhysics"; // . TexturePhysics.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:TexturePhysics.frame.size]; // . TexturePhysics.physicsBody.dynamic = YES; // // . [self addChild: TexturePhysics]; // . UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; // UIBezierPath. [TrianglePath moveToPoint:CGPointMake(0,0)]; // . [TrianglePath addLineToPoint:CGPointMake(-50, -100)]; // . [TrianglePath addLineToPoint:CGPointMake(50, -100)]; // . [TrianglePath addLineToPoint:CGPointMake(0, 0)]; // . // SKShapeNode . . // , CGPath. SKShapeNode *TrianglePhysics = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath ]; TrianglePhysics.position = CGPointMake(400, 190); // . TrianglePhysics.lineWidth = 2; // . TrianglePhysics.strokeColor = [SKColor blackColor]; // . TrianglePhysics.fillColor = [SKColor blueColor]; // . TrianglePhysics.name = @"Triangle"; // . TrianglePhysics.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:TrianglePath.CGPath]; // . TrianglePhysics.physicsBody.dynamic = YES; // . [self addChild:TrianglePhysics]; // . // SKLabelNode . . SKLabelNode *LabelPhysics = [SKLabelNode labelNodeWithText:@"Xcode"]; LabelPhysics.position = CGPointMake(500, 200); // . LabelPhysics.fontSize = 22; // . LabelPhysics.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(-25, -25, 50, 50)]; // . LabelPhysics.name = @"Label"; // . [self addChild:LabelPhysics]; // . }
func CreatePhysics() { // RectanglePhysics SKShapeNode. // (CGRectMake). var RectanglePhysics = SKShapeNode(rect: CGRectMake(-580/2, -10, 580, 20)) RectanglePhysics.position = CGPointMake(280, 40) // . RectanglePhysics.fillColor = SKColor.whiteColor() // . RectanglePhysics.name = "Rectangle" // . RectanglePhysics.physicsBody = SKPhysicsBody(rectangleOfSize: RectanglePhysics.frame.size) // . RectanglePhysics.physicsBody.dynamic = false // . self.addChild(RectanglePhysics) // . // CirclePhysics SKShapeNode. // . var CirclePhysics = SKShapeNode(circleOfRadius: 40) CirclePhysics.position = CGPointMake(100, 160) // . CirclePhysics.strokeColor = SKColor.greenColor() // . CirclePhysics.lineWidth = 5; // . CirclePhysics.name = "Circle" // . CirclePhysics.physicsBody = SKPhysicsBody(circleOfRadius: 40) // . CirclePhysics.physicsBody.dynamic = true // . self.addChild(CirclePhysics) // . // Texture SKTexture. let Texture = SKTexture(imageNamed: "DerevoOpora") // TexturePhysics SKSpriteNode. // SKTexture . var TexturePhysics = SKSpriteNode(texture: Texture) TexturePhysics.position = CGPointMake(200, 180) // . TexturePhysics.size = CGSizeMake(100, 30) // . TexturePhysics.name = "TexturePhysics" // . TexturePhysics.physicsBody = SKPhysicsBody(rectangleOfSize: TexturePhysics.frame.size) // . TexturePhysics.physicsBody.dynamic = true // . self.addChild(TexturePhysics) // . var TrianglePath = UIBezierPath() // UIBezierPath. TrianglePath.moveToPoint(CGPointMake(0, 0)) // . TrianglePath.addLineToPoint(CGPointMake(-50, -100)) // . TrianglePath.addLineToPoint(CGPointMake(50, -100)) // . TrianglePath.addLineToPoint(CGPointMake(0, 0)) // . // TrianglePhysics SKShapeNode. // // , CGPath. var TrianglePhysics = SKShapeNode(path: TrianglePath.CGPath) TrianglePhysics.position = CGPointMake(400, 190) // . TrianglePhysics.lineWidth = 2 // . TrianglePhysics.strokeColor = SKColor.blackColor() // . TrianglePhysics.fillColor = SKColor.blueColor() // . TrianglePhysics.name = "Triangle" // . TrianglePhysics.physicsBody = SKPhysicsBody(polygonFromPath: TrianglePath.CGPath) // . TrianglePhysics.physicsBody.dynamic = true // . self.addChild(TrianglePhysics) // . // LabelPhysics SKLabelNode. . var LabelPhysics = SKLabelNode(text: "Xcode") LabelPhysics.position = CGPointMake(500, 200) // . LabelPhysics.fontSize = 22; // . LabelPhysics.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(-25, -25, 50, 50)) // . LabelPhysics.name = "Label" // . self.addChild(LabelPhysics) // . }
If you specify a body size smaller than the Sprite itself, in this case, if an object touches a part that does not enter the physical body, it will penetrate into another Sprite, and if you specify a body size larger than the Sprite size, then it will seem that the Sprite shell itself does not touch , but some invisible border. In the image below, I gave an example for these cases!![]() |
CirclePhysics . . : SKPhysicsBody bodyWithCircleOfRadius circleOfRadius . dynamic , , . , , ! |
. , . , : , , ! , , ! |
TexturePhysics . , . , , dynamic , ! |
, TrianglePhysics . , UIBezierPath . , SKPhysicsBody . polygonFromPath bodyWithPolygonFromPath . (-) ? UIBezierPath TrianglePath . ! |
LabelPhysics . , SKPhysicsBody , bodyWithEdgeLoopFromRect edgeLoopFromRect . ? , (CGRectMake) (EDGE). . , showsPhysics , . , ! EDGE, , . . , . , ; EDGE. |
-(void)PhysicsProperties { float RotateAngle = 10 * (2 * M_PI)/360; SKShapeNode *Opora1 = [SKShapeNode shapeNodeWithRect:CGRectMake(-150/2, -10, 150, 20)]; Opora1.position = CGPointMake(100, 200); Opora1.strokeColor = [SKColor greenColor]; Opora1.fillColor = [SKColor greenColor]; Opora1.name = @"Opora1"; Opora1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Opora1.frame.size]; Opora1.physicsBody.dynamic = NO; Opora1.zRotation = -RotateAngle; [self addChild:Opora1]; SKShapeNode *Opora2 = [SKShapeNode shapeNodeWithRect:CGRectMake(-150/2, -10, 150, 20)]; Opora2.position = CGPointMake(468, 200); Opora2.strokeColor = [SKColor redColor]; Opora2.fillColor = [SKColor redColor]; Opora2.name = @"Opora2"; Opora2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Opora2.frame.size]; Opora2.physicsBody.dynamic = NO; Opora2.zRotation = RotateAngle; [self addChild:Opora2]; SKShapeNode *Opora3 = [SKShapeNode shapeNodeWithRect:CGRectMake(-568/2, -10, 568, 20)]; Opora3.position = CGPointMake(568/2, 10); Opora3.strokeColor = [SKColor yellowColor]; Opora3.fillColor = [SKColor yellowColor]; Opora3.name = @"Opora3"; Opora3.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Opora3.frame.size]; Opora3.physicsBody.dynamic = NO; [self addChild:Opora3]; SKShapeNode *Circle1 = [SKShapeNode shapeNodeWithCircleOfRadius:15]; Circle1.position = CGPointMake(250, 280); Circle1.strokeColor = [SKColor whiteColor]; Circle1.fillColor = [SKColor blackColor]; Circle1.name = @"Circle1"; Circle1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15]; Circle1.physicsBody.restitution = 0; [self addChild:Circle1]; SKShapeNode *Circle2 = [SKShapeNode shapeNodeWithCircleOfRadius:15]; Circle2.position = CGPointMake(310, 280); Circle2.strokeColor = [SKColor whiteColor]; Circle2.fillColor = [SKColor purpleColor]; Circle2.name = @"Circle2"; Circle2.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15]; Circle2.physicsBody.restitution = 0.7; [self addChild:Circle2]; SKShapeNode *Quad1 = [SKShapeNode shapeNodeWithRect:CGRectMake(-30/2, -30/2, 30, 30)]; Quad1.position = CGPointMake(50, 320); Quad1.strokeColor = [SKColor whiteColor]; Quad1.fillColor = [SKColor whiteColor]; Quad1.name = @"Quad1"; Quad1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Quad1.frame.size]; Quad1.physicsBody.friction = 1; [self addChild:Quad1]; SKShapeNode *Quad2 = [SKShapeNode shapeNodeWithRect:CGRectMake(-30/2, -30/2, 30, 30)]; Quad2.position = CGPointMake(518, 320); Quad2.strokeColor = [SKColor blackColor]; Quad2.fillColor = [SKColor blackColor]; Quad2.name = @"Quad2"; Quad2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Quad2.frame.size]; Quad2.physicsBody.friction = 0.1; [self addChild:Quad2]; }
func PhysicsProperties() { let RotateAngle = 10 * (2 * M_PI)/360 var Opora1 = SKShapeNode(rect: CGRectMake(-150/2, -10, 150, 20)) Opora1.position = CGPointMake(100, 200) Opora1.strokeColor = SKColor.greenColor() Opora1.fillColor = SKColor.greenColor() Opora1.name = "Opora1" Opora1.physicsBody = SKPhysicsBody(rectangleOfSize: Opora1.frame.size) Opora1.physicsBody.dynamic = false Opora1.zRotation = Float(-RotateAngle) self.addChild(Opora1) var Opora2 = SKShapeNode(rect: CGRectMake(-150/2, -10, 150, 20)) Opora2.position = CGPointMake(468, 200) Opora2.strokeColor = SKColor.redColor() Opora2.fillColor = SKColor.redColor() Opora2.name = "Opora2" Opora2.physicsBody = SKPhysicsBody(rectangleOfSize: Opora2.frame.size) Opora2.physicsBody.dynamic = false Opora2.zRotation = Float(RotateAngle) self.addChild(Opora2) var Opora3 = SKShapeNode(rect:CGRectMake (-568/2, -10, 568, 20)) Opora3.position = CGPointMake(568/2, 10) Opora3.strokeColor = SKColor.yellowColor() Opora3.fillColor = SKColor.yellowColor() Opora3.name = "Opora3" Opora3.physicsBody = SKPhysicsBody(rectangleOfSize: Opora3.frame.size) Opora3.physicsBody.dynamic = false self.addChild(Opora3) var Circle1 = SKShapeNode(circleOfRadius: 15) Circle1.position = CGPointMake(250, 280) Circle1.strokeColor = SKColor.whiteColor() Circle1.fillColor = SKColor.blackColor() Circle1.name = "Circle1" Circle1.physicsBody = SKPhysicsBody(circleOfRadius: 15) Circle1.physicsBody.restitution = 0 self.addChild(Circle1) var Circle2 = SKShapeNode(circleOfRadius: 15) Circle2.position = CGPointMake(310, 280) Circle2.strokeColor = SKColor.whiteColor() Circle2.fillColor = SKColor.purpleColor() Circle2.name = "Circle2" Circle2.physicsBody = SKPhysicsBody(circleOfRadius: 15) Circle2.physicsBody.restitution = 0.7 self.addChild(Circle2) var Quad1 = SKShapeNode(rect: CGRectMake (-30/2, -30/2, 30, 30)) Quad1.position = CGPointMake(50, 320) Quad1.strokeColor = SKColor.whiteColor() Quad1.fillColor = SKColor.whiteColor() Quad1.name = "Quad1" Quad1.physicsBody = SKPhysicsBody(rectangleOfSize: Quad1.frame.size) Quad1.physicsBody.friction = 1 self.addChild(Quad1) var Quad2 = SKShapeNode(rect: CGRectMake (-30/2, -30/2, 30, 30)) Quad2.position = CGPointMake(518, 320) Quad2.strokeColor = SKColor.blackColor() Quad2.fillColor = SKColor.blackColor() Quad2.name = "Quad2" Quad2.physicsBody = SKPhysicsBody(rectangleOfSize: Quad2.frame.size) Quad2.physicsBody.friction = 0.1 self.addChild(Quad2) }
zRotation - Defines the rotation of our Node in the corners of Euler. |
We create a float variable named RotateAngle 10, . 60 , : zRotation : |
let RotateAngle Objective-c , float |
restitution - Determines how much energy the body loses in a collision (how much the body rebounds, in a collision) (takes values ​​between 0.1 - 1.0). The default is 0.2. |
friction - Determines the surface roughness. It is used to calculate the friction force in contact with the body. |
#import "GameScene.h" @implementation GameScene -(void)didMoveToView:(SKView *)view { [self SceneSetting]; //[self SKSpriteNodeDemo]; //[self SKShapeNodeDemo]; //[self SKLabelNodeDemo]; //[self CreatePhysics]; [self PhysicsProperties]; } -(void)SceneSetting { self.backgroundColor = [SKColor orangeColor]; self.physicsWorld.gravity = CGVectorMake(0, -1); } -(void)PhysicsProperties { float RotateAngle = 10 * (2 * M_PI)/360; SKShapeNode *Opora1 = [SKShapeNode shapeNodeWithRect:CGRectMake(-150/2, -10, 150, 20)]; Opora1.position = CGPointMake(100, 200); Opora1.strokeColor = [SKColor greenColor]; Opora1.fillColor = [SKColor greenColor]; Opora1.name = @"Opora1"; Opora1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Opora1.frame.size]; Opora1.physicsBody.dynamic = NO; Opora1.zRotation = -RotateAngle; [self addChild:Opora1]; SKShapeNode *Opora2 = [SKShapeNode shapeNodeWithRect:CGRectMake(-150/2, -10, 150, 20)]; Opora2.position = CGPointMake(468, 200); Opora2.strokeColor = [SKColor redColor]; Opora2.fillColor = [SKColor redColor]; Opora2.name = @"Opora2"; Opora2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Opora2.frame.size]; Opora2.physicsBody.dynamic = NO; Opora2.zRotation = RotateAngle; [self addChild:Opora2]; SKShapeNode *Opora3 = [SKShapeNode shapeNodeWithRect:CGRectMake(-568/2, -10, 568, 20)]; Opora3.position = CGPointMake(568/2, 10); Opora3.strokeColor = [SKColor yellowColor]; Opora3.fillColor = [SKColor yellowColor]; Opora3.name = @"Opora3"; Opora3.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Opora3.frame.size]; Opora3.physicsBody.dynamic = NO; [self addChild:Opora3]; SKShapeNode *Circle1 = [SKShapeNode shapeNodeWithCircleOfRadius:15]; Circle1.position = CGPointMake(250, 280); Circle1.strokeColor = [SKColor whiteColor]; Circle1.fillColor = [SKColor blackColor]; Circle1.name = @"Circle1"; Circle1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15]; Circle1.physicsBody.restitution = 0; [self addChild:Circle1]; SKShapeNode *Circle2 = [SKShapeNode shapeNodeWithCircleOfRadius:15]; Circle2.position = CGPointMake(310, 280); Circle2.strokeColor = [SKColor whiteColor]; Circle2.fillColor = [SKColor purpleColor]; Circle2.name = @"Circle2"; Circle2.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:15]; Circle2.physicsBody.restitution = 0.7; [self addChild:Circle2]; SKShapeNode *Quad1 = [SKShapeNode shapeNodeWithRect:CGRectMake(-30/2, -30/2, 30, 30)]; Quad1.position = CGPointMake(50, 320); Quad1.strokeColor = [SKColor whiteColor]; Quad1.fillColor = [SKColor whiteColor]; Quad1.name = @"Quad1"; Quad1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Quad1.frame.size]; Quad1.physicsBody.friction = 1; [self addChild:Quad1]; SKShapeNode *Quad2 = [SKShapeNode shapeNodeWithRect:CGRectMake(-30/2, -30/2, 30, 30)]; Quad2.position = CGPointMake(518, 320); Quad2.strokeColor = [SKColor blackColor]; Quad2.fillColor = [SKColor blackColor]; Quad2.name = @"Quad2"; Quad2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Quad2.frame.size]; Quad2.physicsBody.friction = 0.1; [self addChild:Quad2]; } -(void)CreatePhysics { SKShapeNode *RectanglePhysics = [SKShapeNode shapeNodeWithRect:CGRectMake(-580/2, -10, 580, 20)]; RectanglePhysics.position = CGPointMake(280, 40); RectanglePhysics.fillColor = [SKColor whiteColor]; RectanglePhysics.name = @"Rectangle"; RectanglePhysics.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:RectanglePhysics.frame.size]; RectanglePhysics.physicsBody.dynamic = NO; [self addChild:RectanglePhysics]; SKShapeNode *CirclePhysics = [SKShapeNode shapeNodeWithCircleOfRadius:40]; CirclePhysics.position = CGPointMake(100, 160); CirclePhysics.strokeColor = [SKColor greenColor]; CirclePhysics.lineWidth = 5; CirclePhysics.name = @"Circle"; CirclePhysics.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:40]; CirclePhysics.physicsBody.dynamic = YES; [self addChild:CirclePhysics]; SKTexture *Texture = [SKTexture textureWithImageNamed:@"DerevoOpora"]; SKSpriteNode *TexturePhysics = [SKSpriteNode spriteNodeWithTexture:Texture]; TexturePhysics.position = CGPointMake(200, 180); TexturePhysics.size = CGSizeMake(100, 30); TexturePhysics.name = @"TexturePhysics"; TexturePhysics.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:TexturePhysics.frame.size]; TexturePhysics.physicsBody.dynamic = YES; [self addChild: TexturePhysics]; UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; [TrianglePath moveToPoint:CGPointMake(0,0)]; [TrianglePath addLineToPoint:CGPointMake(-50, -100)]; [TrianglePath addLineToPoint:CGPointMake(50, -100)]; [TrianglePath addLineToPoint:CGPointMake(0, 0)]; SKShapeNode *TrianglePhysics = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath ]; TrianglePhysics.position = CGPointMake(400, 190); TrianglePhysics.lineWidth = 2; TrianglePhysics.strokeColor = [SKColor blackColor]; TrianglePhysics.fillColor = [SKColor blueColor]; TrianglePhysics.name = @"Triangle"; TrianglePhysics.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:TrianglePath.CGPath]; TrianglePhysics.physicsBody.dynamic = YES; [self addChild:TrianglePhysics]; SKLabelNode *LabelPhysics = [SKLabelNode labelNodeWithText:@"Xcode"]; LabelPhysics.position = CGPointMake(500, 200); LabelPhysics.fontSize = 22; LabelPhysics.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:CGRectMake(-25, -25, 50, 50)]; LabelPhysics.name = @"Label"; [self addChild:LabelPhysics]; } -(void)SKSpriteNodeDemo { SKTexture *Texture = [SKTexture textureWithImageNamed:@"desert_BG"]; SKSpriteNode *BackgroundSprite = [SKSpriteNode spriteNodeWithTexture:Texture]; BackgroundSprite.size = CGSizeMake(640, 320); BackgroundSprite.position = CGPointMake(0, 0); BackgroundSprite.anchorPoint = CGPointMake(0, 0); BackgroundSprite.name = @"BackgroundSprite"; [self addChild:BackgroundSprite]; SKSpriteNode *SimpleSprite = [SKSpriteNode spriteNodeWithColor:[UIColor blueColor] size:CGSizeMake(50, 50)]; SimpleSprite.position = CGPointMake(200, 150); SimpleSprite.zPosition = 1; SimpleSprite.name = @"SimpleSprite"; [self addChild:SimpleSprite]; SKSpriteNode *ImageSprite = [SKSpriteNode spriteNodeWithImageNamed:@"DerevoOpora"]; ImageSprite.position = CGPointMake(250, 50); ImageSprite.size = CGSizeMake(100, 15); ImageSprite.name = @"ImageSprite"; [self addChild:ImageSprite]; } -(void)SKShapeNodeDemo { SKShapeNode *Circle = [SKShapeNode shapeNodeWithCircleOfRadius:20]; Circle.position = CGPointMake(50, 200); Circle.lineWidth = 10; Circle.strokeColor = [SKColor blueColor]; Circle.fillColor = [SKColor redColor]; Circle.name = @"Circle"; [self addChild:Circle]; SKShapeNode *Quad = [SKShapeNode shapeNodeWithRect:CGRectMake(0, 0, 50, 50)]; Quad.position = CGPointMake(100, 200); Quad.lineWidth = 4; Quad.strokeColor = [SKColor whiteColor]; Quad.fillColor = [SKColor blackColor]; Quad.name = @"Quad"; [self addChild:Quad]; SKShapeNode *Ellips = [SKShapeNode shapeNodeWithEllipseInRect:CGRectMake(0, 0, 50, 90)]; Ellips.position = CGPointMake(200, 200); Ellips.lineWidth = 2; Ellips.strokeColor = [SKColor greenColor]; Ellips.fillColor = [SKColor purpleColor]; Ellips.glowWidth = 5; Ellips.name = @"Ellips"; [self addChild:Ellips]; UIBezierPath *RoundedRectPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, 50, 50) cornerRadius:12]; SKShapeNode *RoundedRect = [ SKShapeNode shapeNodeWithPath:RoundedRectPath.CGPath centered:YES]; RoundedRect.position = CGPointMake(50, 100); RoundedRect.lineWidth = 2; RoundedRect.strokeColor = [SKColor blueColor]; RoundedRect.fillColor = [SKColor redColor]; RoundedRect.name = @"RoundedRect"; [self addChild:RoundedRect]; UIBezierPath *TrianglePath = [UIBezierPath bezierPath]; [TrianglePath moveToPoint:CGPointMake(0,0)]; [TrianglePath addLineToPoint:CGPointMake(-25, -50)]; [TrianglePath addLineToPoint:CGPointMake(25, -50)]; [TrianglePath addLineToPoint:CGPointMake(0, 0)]; SKShapeNode *Triangle = [SKShapeNode shapeNodeWithPath:TrianglePath.CGPath centered:YES]; Triangle.position = CGPointMake(200, 70); Triangle.lineWidth = 2; Triangle.strokeColor = [SKColor blackColor]; Triangle.fillColor = [SKColor blueColor]; Triangle.name = @"Triangle"; [self addChild:Triangle]; } -(void)SKLabelNodeDemo { SKLabelNode *First = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; First.position = CGPointMake(280, 200); First.fontSize = 25; First.fontColor = [SKColor whiteColor]; First.color = [SKColor blueColor]; First.colorBlendFactor = 0.5; First.text = @"Habra Habr!"; First.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter; [self addChild:First]; SKLabelNode *Second = [SKLabelNode labelNodeWithText:@"----"]; Second.fontName = @"Chalkboard SE Bold"; Second.fontColor = [SKColor blackColor]; Second.position = CGPointMake(280, 50); Second.fontSize = 20; [self addChild:Second]; } @end
import SpriteKit class GameScene: SKScene { override func didMoveToView(view: SKView) { SceneSetting() //SKSpriteNodeDemo() //SKShapeNodeDemo() //SKLabelNodeDemo() //CreatePhysics() PhysicsProperties() } func SceneSetting() { self.backgroundColor = SKColor.orangeColor() self.physicsWorld.gravity = CGVectorMake(0, -1); } func PhysicsProperties() { let RotateAngle = 10 * (2 * M_PI)/360 var Opora1 = SKShapeNode(rect: CGRectMake(-150/2, -10, 150, 20)) Opora1.position = CGPointMake(100, 200) Opora1.strokeColor = SKColor.greenColor() Opora1.fillColor = SKColor.greenColor() Opora1.name = "Opora1" Opora1.physicsBody = SKPhysicsBody(rectangleOfSize: Opora1.frame.size) Opora1.physicsBody.dynamic = false Opora1.zRotation = Float(-RotateAngle) self.addChild(Opora1) var Opora2 = SKShapeNode(rect: CGRectMake(-150/2, -10, 150, 20)) Opora2.position = CGPointMake(468, 200) Opora2.strokeColor = SKColor.redColor() Opora2.fillColor = SKColor.redColor() Opora2.name = "Opora2" Opora2.physicsBody = SKPhysicsBody(rectangleOfSize: Opora2.frame.size) Opora2.physicsBody.dynamic = false Opora2.zRotation = Float(RotateAngle) self.addChild(Opora2) var Opora3 = SKShapeNode(rect:CGRectMake (-568/2, -10, 568, 20)) Opora3.position = CGPointMake(568/2, 10) Opora3.strokeColor = SKColor.yellowColor() Opora3.fillColor = SKColor.yellowColor() Opora3.name = "Opora3" Opora3.physicsBody = SKPhysicsBody(rectangleOfSize: Opora3.frame.size) Opora3.physicsBody.dynamic = false self.addChild(Opora3) var Circle1 = SKShapeNode(circleOfRadius: 15) Circle1.position = CGPointMake(250, 280) Circle1.strokeColor = SKColor.whiteColor() Circle1.fillColor = SKColor.blackColor() Circle1.name = "Circle1" Circle1.physicsBody = SKPhysicsBody(circleOfRadius: 15) Circle1.physicsBody.restitution = 0 self.addChild(Circle1) var Circle2 = SKShapeNode(circleOfRadius: 15) Circle2.position = CGPointMake(310, 280) Circle2.strokeColor = SKColor.whiteColor() Circle2.fillColor = SKColor.purpleColor() Circle2.name = "Circle2" Circle2.physicsBody = SKPhysicsBody(circleOfRadius: 15) Circle2.physicsBody.restitution = 0.7 self.addChild(Circle2) var Quad1 = SKShapeNode(rect: CGRectMake (-30/2, -30/2, 30, 30)) Quad1.position = CGPointMake(50, 320) Quad1.strokeColor = SKColor.whiteColor() Quad1.fillColor = SKColor.whiteColor() Quad1.name = "Quad1" Quad1.physicsBody = SKPhysicsBody(rectangleOfSize: Quad1.frame.size) Quad1.physicsBody.friction = 1 self.addChild(Quad1) var Quad2 = SKShapeNode(rect: CGRectMake (-30/2, -30/2, 30, 30)) Quad2.position = CGPointMake(518, 320) Quad2.strokeColor = SKColor.blackColor() Quad2.fillColor = SKColor.blackColor() Quad2.name = "Quad2" Quad2.physicsBody = SKPhysicsBody(rectangleOfSize: Quad2.frame.size) Quad2.physicsBody.friction = 0.1 self.addChild(Quad2) } func CreatePhysics() { var RectanglePhysics = SKShapeNode(rect: CGRectMake(-580/2, -10, 580, 20)) RectanglePhysics.position = CGPointMake(280, 40) RectanglePhysics.fillColor = SKColor.whiteColor() RectanglePhysics.name = "Rectangle" RectanglePhysics.physicsBody = SKPhysicsBody(rectangleOfSize: RectanglePhysics.frame.size) RectanglePhysics.physicsBody.dynamic = false self.addChild(RectanglePhysics) var CirclePhysics = SKShapeNode(circleOfRadius: 40) CirclePhysics.position = CGPointMake(100, 160) CirclePhysics.strokeColor = SKColor.greenColor() CirclePhysics.lineWidth = 5; CirclePhysics.name = "Circle" CirclePhysics.physicsBody = SKPhysicsBody(circleOfRadius: 40) CirclePhysics.physicsBody.dynamic = true self.addChild(CirclePhysics) let Texture = SKTexture(imageNamed: "DerevoOpora") var TexturePhysics = SKSpriteNode(texture: Texture) TexturePhysics.position = CGPointMake(200, 180) TexturePhysics.size = CGSizeMake(100, 30) TexturePhysics.name = "TexturePhysics" TexturePhysics.physicsBody = SKPhysicsBody(rectangleOfSize: TexturePhysics.frame.size) TexturePhysics.physicsBody.dynamic = true self.addChild(TexturePhysics) var TrianglePath = UIBezierPath() TrianglePath.moveToPoint(CGPointMake(0, 0)) TrianglePath.addLineToPoint(CGPointMake(-50, -100)) TrianglePath.addLineToPoint(CGPointMake(50, -100)) TrianglePath.addLineToPoint(CGPointMake(0, 0)) var TrianglePhysics = SKShapeNode(path: TrianglePath.CGPath) TrianglePhysics.position = CGPointMake(400, 190) TrianglePhysics.lineWidth = 2 TrianglePhysics.strokeColor = SKColor.blackColor() TrianglePhysics.fillColor = SKColor.blueColor() TrianglePhysics.name = "Triangle" TrianglePhysics.physicsBody = SKPhysicsBody(polygonFromPath: TrianglePath.CGPath) TrianglePhysics.physicsBody.dynamic = true self.addChild(TrianglePhysics) var LabelPhysics = SKLabelNode(text: "Xcode") LabelPhysics.position = CGPointMake(500, 200) LabelPhysics.fontSize = 22; LabelPhysics.physicsBody = SKPhysicsBody(edgeLoopFromRect: CGRectMake(-25, -25, 50, 50)) LabelPhysics.name = "Label" self.addChild(LabelPhysics) } func SKSpriteNodeDemo() { var Texture = SKTexture(imageNamed: "desert_BG") var BackgroundSprite = SKSpriteNode(texture: Texture) BackgroundSprite.size = CGSizeMake(640, 320) BackgroundSprite.position = CGPointMake(0, 0) BackgroundSprite.anchorPoint = CGPointMake(0, 0) BackgroundSprite.name = "BackgroundSprite" self.addChild(BackgroundSprite) var SimpleSprite = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(50, 50)) SimpleSprite.position = CGPointMake(200, 150) SimpleSprite.zPosition = 1; SimpleSprite.name = "SimpleSprite" self.addChild(SimpleSprite) var ImageSprite = SKSpriteNode(imageNamed: "DerevoOpora") ImageSprite.position = CGPointMake(250, 50) ImageSprite.size = CGSizeMake(100, 15) ImageSprite.name = "ImageSprite" self.addChild(ImageSprite) } func SKShapeNodeDemo() { var Circle = SKShapeNode(circleOfRadius: 20) Circle.position = CGPointMake(50, 200) Circle.lineWidth = 10 Circle.strokeColor = SKColor.blueColor() Circle.fillColor = SKColor.redColor() Circle.name = "Circle" self.addChild(Circle) var Quad = SKShapeNode(rect: CGRectMake(0, 0, 50, 50)) Quad.position = CGPointMake(100, 200) Quad.lineWidth = 4 Quad.strokeColor = SKColor.whiteColor() Quad.fillColor = SKColor.blackColor() Quad.name = "Quad" self.addChild(Quad) var Ellips = SKShapeNode(ellipseInRect: CGRectMake(0, 0, 50, 90)) Ellips.position = CGPointMake(200, 200) Ellips.lineWidth = 2 Ellips.strokeColor = SKColor.greenColor() Ellips.fillColor = SKColor.purpleColor() Ellips.glowWidth = 5 Ellips.name = "Ellips" self.addChild(Ellips) var RoundedRectPath = UIBezierPath(roundedRect: CGRectMake(0, 0, 50, 50), cornerRadius: 12) var RoundedRect = SKShapeNode(path: RoundedRectPath.CGPath, centered:true) RoundedRect.position = CGPointMake(50, 100) RoundedRect.lineWidth = 2 RoundedRect.strokeColor = SKColor.blueColor() RoundedRect.fillColor = SKColor.redColor() RoundedRect.name = "RoundedRect" self.addChild(RoundedRect) var TrianglePath = UIBezierPath() TrianglePath.moveToPoint(CGPointMake(0, 0)) TrianglePath.addLineToPoint(CGPointMake(-25, -50)) TrianglePath.addLineToPoint(CGPointMake(25, -50)) TrianglePath.addLineToPoint(CGPointMake(0, 0)) var Triangle = SKShapeNode(path: TrianglePath.CGPath, centered: true) Triangle.position = CGPointMake(200, 70) Triangle.lineWidth = 2 Triangle.strokeColor = SKColor.blackColor() Triangle.fillColor = SKColor.blueColor() Triangle.name = "Triangle" self.addChild(Triangle) } func SKLabelNodeDemo() { var First = SKLabelNode(fontNamed: "Chalkduster") First.position = CGPointMake(280, 200) First.fontSize = 25; First.fontColor = SKColor.whiteColor() First.color = SKColor.blueColor() First.colorBlendFactor = 0.5 First.text = "Habra Habr!" First.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center self.addChild(First) var Second = SKLabelNode(text: "----") Second.fontName = "Chalkboard SE Bold" Second.fontColor = SKColor.blackColor() Second.position = CGPointMake(280, 50) Second.fontSize = 20 self.addChild(Second) } }
Source: https://habr.com/ru/post/225517/
All Articles