Swift - Spritekit Cgpoint Alignment

Swift - SpriteKit CGPoint Alignment

Your scene it is not necessarily the same size of your view. You can check the size of your scene as follow:

view.scene!.frame.size
view.scene!.frame.width
view.scene!.frame.height
view.scene!.frame.midX
view.scene!.frame.midY

Sprite Kit label in the corner

In Sprite Kit, the default origin of the scene's coordinates is in the bottom left corner of the screen. So, if you want the label in a top corner, you should keep using the screen's height for your label's position, but also use either 0 or the screen's width for the x position. Coupling this with a horizontal alignment mode of left or right depending on the side, and your label will be in the correct position.

For a label in the top left corner:

label.horizontalAlignmentMode = .Left
label.position = CGPoint(x:0.0, y:self.size.height)

Or for the top right:

label.horizontalAlignmentMode = .Right
label.position = CGPoint(x:self.size.width, y:self.size.height)

How to reset the alignment of physics bodies in SpriteKit?

if you want that after you restart the game the brick node go back to start rotation than call:

brick.zRotation = 0

if you want that during the game the node do not rotate than you may put this code in the update func that already have in all spritekit files:

override func update(_ currentTime: TimeInterval) {
brick.zRotation = 0
}

Hope this helps

Swift 3 (SpriteKit): Aligning SKPhysicsBody to SKShapeNode

If you want to make a physics body from one point to the other, you may use something like this:

class GameScene:SKScene {

override func didMove(to view: SKView) {
var points = [CGPoint(x: 22, y: 22), CGPoint(x: 155, y: 155)]
let line = SKShapeNode(points: &points, count: points.count)
print(line.frame.size)

line.physicsBody = SKPhysicsBody(edgeFrom: points[0], to: points[1])

addChild(line)
}
}

This will create an edge based physics body using two points. An edge physics body is static by default so keep that in mind. To register a contact (or make a collision with this body) the other body has to be dynamic.

If you want to have this body dynamic, then look for volume based initializers.

applyImpulse towards CGPoint SpriteKit

Look up the shooting projectiles section in the tutorial by Ray Wenderlich here:

http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners

Change the code from the tutorial as follows:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

// 1 - Choose one of the touches to work with
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];

// 2 - Set up initial location of projectile
SKSpriteNode * projectile = [self childNodeWithName:@"desirednode"];
//make projectile point to your desired node.

// 3- Determine offset of location to projectile
CGPoint offset = rwSub(location, projectile.position);

// 4 - Bail out if you are shooting down or backwards. You can ignore this if required.
if (offset.x <= 0) return;

// 5 - OK to add now - we've double checked position
[self addChild:projectile];

// 6 - Get the direction of where to shoot
CGPoint direction = rwNormalize(offset);

// 7 - Make it shoot far enough to be guaranteed off screen
float forceValue = 200; //Edit this value to get the desired force.
CGPoint shootAmount = rwMult(direction, forceValue);

//8 - Convert the point to a vector
CGVector impulseVector = CGVectorMake(shootAmount.x, shootAmount.y);
//This vector is the impulse you are looking for.

//9 - Apply impulse to node.
[projectile.physicsBody applyImpulse:impulseVector];

}

The projectile object in the code represents your node. Also, you will need to edit the forceValue to get the desired impulse.



Related Topics



Leave a reply



Submit