spritekitでプレイヤーが移動した時に、壁に当たったら動かない様にしたい
どうやってもプレイヤーが壁を押してしまいます、どうすれば、skactionなどで移動したプレイヤー(RPG想定)が、壁に当たった時にそこで止まる様な仕様を実装できますか?
画面のピンク色の長方形の部分のNodeは川になっていて、歩行中のプレイヤーが歩けないようにしたくて、テクスチャーは入れずにダイナミックを入れ、Massを100くらいにしたりして、プレイヤーが接触したらそこで止まる様にしたいのですが、何をどうしても、SKActionやCGvecterで移動したプレイヤーがこのNodeを押してしまいます。
PindにしてもプライヤーのMassやdentityを0にしても押してしまいます。プレイヤーの動きをはねのける様な、そんな壁の存在が欲しいのです。
import SpriteKit
import GameplayKit
class GameScene: SKScene,SKPhysicsContactDelegate {
var textureSide:[SKTexture] = []
var textureUp:[SKTexture] = []
var textureDown:[SKTexture] = []
var upB:SKSpriteNode!
var downB:SKSpriteNode!
var rightB:SKSpriteNode!
var leftB:SKSpriteNode!
var playerposition = CGPoint(x: 0, y: 0)
var yellow:SKSpriteNode!
var player:SKSpriteNode!
var wall:SKSpriteNode!
var rightpoti = true
enum direction {
case right
case left
case up
case down
}
override func didMove(to view: SKView) {
physicsWorld.gravity = CGVector(dx: 0, dy: 0)
wall = self.childNode(withName: "wall") as! SKSpriteNode
wall.physicsBody?.usesPreciseCollisionDetection = true
wall.zPosition = 5
player = SKSpriteNode(imageNamed: "walkman8")
player.position = playerposition
player.physicsBody?.usesPreciseCollisionDetection = true
player.zPosition = 2
player.setScale(2)
player.physicsBody = SKPhysicsBody(rectangleOf: player.frame.size)
player.physicsBody?.categoryBitMask = 1
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.contactTestBitMask = 0
player.physicsBody?.density = 1
player.physicsBody?.mass = 1
player.physicsBody?.restitution = 2
addChild(player)
upB = self.childNode(withName: "upB") as! SKSpriteNode
downB = self.childNode(withName: "downB") as! SKSpriteNode
rightB = self.childNode(withName: "rightB") as! SKSpriteNode
leftB = self.childNode(withName: "leftB") as! SKSpriteNode
yellow = self.childNode(withName: "yellow") as! SKSpriteNode
yellow.position = CGPoint(x: -0, y: -600)
let atras = SKTextureAtlas(named: "walker")
for i in 1...3{
textureSide.append(atras.textureNamed("walkman" + String(i)))
}
for i in 6...7{
textureDown.append(atras.textureNamed("walkman" + String(i)))
}
for i in 8...10{
textureUp.append(atras.textureNamed("walkman" + String(i)))
}
let animation = SKAction.animate(with: textureDown, timePerFrame: 0.6 )
player.run(SKAction.repeatForever(animation))
}
func didBegin(_ contact: SKPhysicsContact) {
print("bodyA:\(String(describing: contact.bodyA.node?.name))")
if contact.bodyA == wall {
player.run(SKAction.rotate(byAngle: 30, duration: 1))
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
if self.atPoint(location) == upB {
movePlayer(Direction: .up)
}
if self.atPoint(location) == downB {
movePlayer(Direction: .down)
}
if self.atPoint(location) == leftB {
movePlayer(Direction: .left)
}
if self.atPoint(location) == rightB {
movePlayer(Direction: .right)
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
}
func movePlayer(Direction:direction){
player.physicsBody?.velocity = CGVector(dx: 0, dy: 100)
}
}