Comments (3)
This was my logic for an older version of SKTiled when cameraPanned was able to be overridden. But perhaps some of the math can help out:
open func cameraPanned(_ recognizer: UIPanGestureRecognizer) {
guard let scene = self.scene as? SKTiledScene else { return }
let minPanX = (scene.size.halfWidth * zoom) - (scene.tilemap.sizeInPoints.halfWidth * zoom - ((scene.size.width * xScale) / 2)) - (panInsets.left * xScale)
let maxPanX = (scene.size.halfWidth * zoom) + (scene.tilemap.sizeInPoints.halfWidth * zoom - ((scene.size.width * xScale) / 2)) + (panInsets.right * xScale)
let minPanY = (scene.size.halfHeight * zoom) - (scene.tilemap.sizeInPoints.halfHeight * zoom - ((scene.size.height * yScale) / 2)) - (panInsets.bottom * yScale)
let maxPanY = (scene.size.halfHeight * zoom) + (scene.tilemap.sizeInPoints.halfHeight * zoom - ((scene.size.height * yScale) / 2)) + (panInsets.top * yScale)
if (recognizer.state == .began) {
let location = recognizer.location(in: recognizer.view)
lastLocation = location
}
if (recognizer.state == .changed) && (allowMovement == true) {
if lastLocation == nil { return }
let location = recognizer.location(in: recognizer.view)
let difference = CGPoint(x: location.x - lastLocation.x, y: location.y - lastLocation.y)
var newPositionX = position.x - (difference.x * self.xScale)
if((scene.tilemap.sizeInPoints.width / xScale) * zoom < scene.size.width - (panInsets.left + panInsets.right)) {
if(newPositionX < maxPanX) {
newPositionX = maxPanX
}
if(newPositionX > minPanX) {
newPositionX = minPanX
}
} else {
if(newPositionX > maxPanX) {
newPositionX = maxPanX
}
if(newPositionX < minPanX) {
newPositionX = minPanX
}
}
var newPositionY = position.y - -(difference.y * self.yScale)
if((scene.tilemap.sizeInPoints.height / yScale) * zoom < scene.size.height - (panInsets.top + panInsets.bottom)) {
if(newPositionY < maxPanY) {
newPositionY = maxPanY
}
if(newPositionY > minPanY) {
newPositionY = minPanY
}
} else {
if(newPositionY > maxPanY) {
newPositionY = maxPanY
}
if(newPositionY < minPanY) {
newPositionY = minPanY
}
}
centerOn(scenePoint: CGPoint(x: newPositionX, y: newPositionY))
lastLocation = location
}
}
It handles zooming, and if the map is bigger than the screen, the map locks to the edges of the screen as you pan. It also handles if the map is smaller than the screen, the edges act as bumpers.
from sktiled.
I haven't implemented anything to automatically limit panning because that will be very specific to your game, but check out the included SKTiledSceneCameraDelegate
protocol - it will update delegates when the camera position, zoom or bounds have changed. It should be easy to constrain the camera with the tilemap node with a SKContraint
node.
from sktiled.
I will try this! Thanks!!
from sktiled.
Related Issues (20)
- Animations in code HOT 5
- please add infinite maps and random map!
- Large tile maps not rendering correctly HOT 3
- Documentation site install to dash button has wrong URL HOT 1
- SKTile highlight color is defined by frameColor HOT 2
- The tvOS target is not part of the Podspec in the cocoapods master repo HOT 1
- function addTilesetTile of SKTileset.swift crash with nil HOT 1
- Would Apple Watch support be possible? Just asking :) HOT 1
- Wrong ID number for tilesets based on tilesets image HOT 4
- addTileAt not replacing tile properly HOT 2
- Loading new tilemap memory leak HOT 8
- Can I get that the user clicked on a certain location on the map? HOT 3
- objective-c compatibility? HOT 1
- M1 Mac Xcode12 HOT 4
- Scaling of Objects HOT 2
- SKTileMapNode vs SKTiled performance question HOT 8
- Questions: player, enemies; collision detection?
- Feature request: get tile coord from SKTile object
- Memory issues when presenting new SKTiledScene, small on iOS, larger on macOS
- [Solved] Some nodes disappear with the framework
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sktiled.