How can you create a bounding box that collides with a circular shape, which is triangular?
Home
/
user-59224
Ramon Alvarez Mora's questions
Ramon Alvarez Mora
Asked:
2020-09-12 06:33:31 +0800 CST
a question about spritekit. Every time I run a game, the object appears to me as if the coordinate point of the scene was in the middle of the scene, that is, 0.5. And I've seen the documentation, and it says that the coordinate axis of a scene is 0.0, I don't know what to do. Help, I'm a beginner. It's basically the background of the flappy bird and its character (because its components are easy to find, and I didn't want to design just for practice) where if you click or drag, the bird goes where you clicked.
class GameScene: SKScene {
let pajaro = SKSpriteNode(imageNamed: "pajaro")
var lastUpdateTime : TimeInterval = 0
var dt : TimeInterval = 0
let pajaroPixelesPerSecond: CGFloat = 300.0
var velocity = CGPoint.zero
override func didMove(to view: SKView) {
let background = SKSpriteNode(imageNamed: "background")
background.zPosition = -1
addChild(background)
pajaro.position = CGPoint(x: -210, y: 150)
pajaro.xScale = 1/2
pajaro.yScale = 1/2
addChild(pajaro)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: self)
sceneTouched(touchLocation: location)
}
override func update(_ currentTime: TimeInterval) {
if lastUpdateTime > 0 {
dt = currentTime - lastUpdateTime
}else{
dt = 0
}
lastUpdateTime = currentTime
moveSprite(sprite: pajaro, velocity: velocity)
checkBounds()
}
func moveSprite(sprite: SKSpriteNode, velocity: CGPoint){
let amount = CGPoint(x: velocity.x * CGFloat(dt), y: velocity.y * CGFloat(dt))
print("La cantidad que nos movemos \(amount)")
sprite.position = CGPoint(x: sprite.position.x + amount.x, y: sprite.position.y + amount.y)
}
func movePajaroToLocation(location: CGPoint){
let offset = CGPoint(x: location.x - pajaro.position.x, y: location.y - pajaro.position.y)
let offsetlenght = sqrt(Double (offset.x*offset.x + offset.y*offset.y))
let direction = CGPoint(x: offset.x/CGFloat(offsetlenght), y: offset.y/CGFloat(offsetlenght))
velocity = CGPoint(x: direction.x * pajaroPixelesPerSecond, y: direction.y * pajaroPixelesPerSecond)
}
func sceneTouched(touchLocation: CGPoint){
movePajaroToLocation(location: touchLocation)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: self)
sceneTouched(touchLocation: location)
}
func checkBounds() {
let bottonLeft = CGPoint.zero
if pajaro.position.x <= bottonLeft.x {
pajaro.position.x = bottonLeft.x
velocity.x = -velocity.x
}
}
I hope you can help me (the last function is to prevent the bird from leaving the screen, but since the coordinate point of the scene is in the middle, the bird does not go beyond the middle), and the other classes are intact.