如何解决SpriteKit:如何创建与设备屏幕大小相同的区域?
我正在SpriteKit中工作,我需要创建一个“可玩区域”,该区域与设备屏幕大小相同,以便阻止播放器移出屏幕。
我正在使用以下代码行:
var playableRect: CGRect = UIScreen.main.bounds
但是最终得到的矩形大约是设备屏幕的四分之一,该矩形的一个角位于屏幕的中心。而且设备方向不会改变这一点。
我已经尽力想了一切。什么都没用。
如何创建与设备屏幕大小相同的矩形?
在更改为包含Gene的建议后,这是我的完整编码。我通过在didMove方法中修改playableRect来合并Gene的建议。但是结果与该编码相同。
import SpriteKit
import GameplayKit
import CoreMotion
@objcmembers
class GameSceneUsingTilt: SKScene {
let player = SKSpriteNode(imageNamed: "player-motorbike")
let motionManager = cmmotionmanager()
var playableRect = UIScreen.main.bounds
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "road")
background.zPosition = -1
addChild(background)
if let particles = SKEmitterNode(fileNamed: "Mud") {
let farRightPt = frame.maxX // start the emitter at the far right x-point of the view
particles.advanceSimulationTime(10)
particles.position.x = farRightPt
addChild(particles)
}
let nearLeftPt = frame.minX * 3 / 4 // start the player at a quarter of the way to the far left x-point of the view
player.position.x = nearLeftPt
player.zPosition = 1
addChild(player)
motionManager.startAccelerometerUpdates()
// coding below shows outline of playableRect
let bounds = UIScreen.main.bounds
let scale = UIScreen.main.scale
// let size = CGSize(width: bounds.size.width * scale,height: bounds.size.height * scale)
playableRect = CGRect(x: 0,y: 0,width: bounds.size.width * scale,height: bounds.size.height * scale)
drawPlayableRect(rect: playableRect)
}
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
}
override func touchesMoved(_ touches: Set<UITouch>,with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
}
func boundsCheck() {
let playableRect = CGRect(x: frame.minX,y: frame.minY,width: frame.width,height: frame.height)
let bottomLeft = CGPoint(x: playableRect.minX,y: playableRect.minY)
let topRight = CGPoint(x: playableRect.maxX,y: playableRect.maxY)
if player.position.x <= bottomLeft.x {
player.position.x = bottomLeft.x
}
if player.position.x >= topRight.x {
player.position.x = topRight.x
}
if player.position.y <= bottomLeft.y {
player.position.y = bottomLeft.y
}
if player.position.y >= topRight.y {
player.position.y = topRight.y
}
}
override func update(_ currentTime: TimeInterval) {
if let accelerometerData = motionManager.accelerometerData {
// note that since the game will be running in landscape mode,up and down movement is controlled by the x axis and right to left movement is controlled by the y-axis...so this is the inverse of what you'd normally think.
let changeX = CGFloat(accelerometerData.acceleration.y) * 4
let changeY = CGFloat(accelerometerData.acceleration.x) * 4
// you have to subtract from x position and add to the y position because device is rotated so the axises aren't what you would normally expect.
player.position.x -= changeX
player.position.y += changeY
// check to make sure position isn't outside payable area:
boundsCheck()
}
}
// function below shows outline of playableRect
func drawPlayableRect(rect: CGRect) {
let shape = SKShapeNode()
let path = CGMutablePath()
path.addRect(rect)
shape.path = path
shape.strokeColor = .red
shape.linewidth = 4.0
addChild(shape)
}
}
解决方法
使用:UIScreen.main.nativeBounds
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。