Probleme bei SpriteKit mit Spielermovement

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Probleme bei SpriteKit mit Spielermovement

    Hallo liebe Community,
    dies hier ist mein erster Post, also sorry, wenn ich etwas falsch mache.
    Ich habe ein Problem in meinem SpriteKit Projekt und zwar habe ich einen Player mit einem image erstellt (image, physicsbody, position, usw.) und es läuft.
    Wenn ich nun die X-Position des Players gleich der X-Position, des Fingers bei touches moved haben möchte bewegt sich der Ball jedoch nicht. Ich möchte es ähnlich, wie beim Spiel Pong haben.
    Ich habe mir im Internet verschiedene Arten angeschaut, wie man so etwas schreiben könnte aber keine davon hat funktioniert, was mich zum Schluss kommen lässt, dass es sich vielleicht gar nicht um den Aufruf der Funktion touches moved handelt, sondern irgendetwas anderes. Ich hoffe ihr könnt mir bei meinem Problem helfen. Danke im Voraus und noch einen schönen Tag.

    Quellcode

    1. import SpriteKit
    2. import GameplayKit
    3. struct PhysicsCategory {
    4. static let ball: UInt32 = 0x1
    5. static let edge: UInt32 = 0x1 << 2
    6. static let block: UInt32 = 0x1 << 3
    7. }
    8. class GameScene: SKScene {
    9. let player = SKSpriteNode(imageNamed: "ball")
    10. let block = SKSpriteNode(imageNamed: "block")
    11. override func didMove(to view: SKView) {
    12. self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
    13. self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
    14. physicsBody?.categoryBitMask = PhysicsCategory.edge
    15. let player = SKSpriteNode(imageNamed: "ball")
    16. player.position = CGPoint(x: frame.midX, y: frame.midY)
    17. player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height / 2.7 )
    18. player.physicsBody?.categoryBitMask = PhysicsCategory.ball
    19. player.size = CGSize(width: 25, height: 25)
    20. player.physicsBody?.isDynamic = true
    21. addChild(player)
    22. // Diesen Block einfach ignorieren
    23. /*let block = SKSpriteNode(imageNamed: "block")
    24. block.position = player.position
    25. block.size = CGSize(width: block.size.width, height: block.size.height)
    26. block.physicsBody = SKPhysicsBody(rectangleOf: block.size)
    27. block.physicsBody?.categoryBitMask = PhysicsCategory.block
    28. addChild(block)*/
    29. }
    30. // Unterschiedliche Herangehensweisen:
    31. // Versuch Nr. 1
    32. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    33. for touch: AnyObject in touches {
    34. let location = touch.location(in: self)
    35. let previousPointOfTouch = touch.previousLocation(in: self)
    36. let amountDragged = location.x - previousPointOfTouch.x
    37. block.position.x += amountDragged
    38. if block.position.x > frame.maxX - block.size.width / 2 {
    39. block.position.x = frame.maxX - block.size.width / 2
    40. }
    41. if block.position.x < frame.minX + block.size.width / 2 {
    42. block.position.x = frame.minX + block.size.width / 2
    43. }
    44. }
    45. }
    46. // Versuch Nr. 2
    47. /* override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    48. for touch in touches {
    49. let location = touch.location(in: self)
    50. block.run(SKAction.moveTo(x: location.x, duration: 0.2))
    51. }
    52. }
    53. override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    54. for touch in touches {
    55. let location = touch.location(in: self)
    56. block.run(SKAction.moveTo(x: location.x, duration: 0.2))
    57. }
    58. }*/
    59. }
    Alles anzeigen

    Mit freundlichen Grüßen Niklas