---
title: Creating Position Constraints
framework: spritekit
role: article
role_heading: Article
path: spritekit/creating-position-constraints
---

# Creating Position Constraints

Create a position constraint and add it to a node.

## Overview

Overview You lock a node at a specific coordinate with positionX(_:y:). Constrain a node’s horizontal position with positionX(_:), or constrain its vertical position with positionY(_:). The following code shows how you can create a node with an attached physics body that’s affected by a noise field. The node moves with the noise but the constraints keep it within a rectangular region between 300 and 340 points on both the horizontal and vertical axes. scene.physicsWorld.gravity = CGVector(dx: 0, dy: 0)     let noiseField = SKFieldNode.noiseField(withSmoothness: 1, animationSpeed: 0.1) scene.addChild(noiseField)       let node = SKShapeNode(circleOfRadius: 10) node.physicsBody = SKPhysicsBody(circleOfRadius: 10) scene.addChild(node)       let range = SKRange(lowerLimit: 300, upperLimit: 340)

let lockToCenter = SKConstraint.positionX(range, y: range)

node.constraints = [ lockToCenter ]

## See Also

### Creating Position Constraints

- [positionX(_:y:)](spritekit/skconstraint/positionx(_:y:).md)
- [positionX(_:)](spritekit/skconstraint/positionx(_:).md)
- [positionY(_:)](spritekit/skconstraint/positiony(_:).md)
