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.
Alles anzeigen
Mit freundlichen Grüßen Niklas
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
- import SpriteKit
- import GameplayKit
- struct PhysicsCategory {
- static let ball: UInt32 = 0x1
- static let edge: UInt32 = 0x1 << 2
- static let block: UInt32 = 0x1 << 3
- }
- class GameScene: SKScene {
- let player = SKSpriteNode(imageNamed: "ball")
- let block = SKSpriteNode(imageNamed: "block")
- override func didMove(to view: SKView) {
- self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
- self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)
- physicsBody?.categoryBitMask = PhysicsCategory.edge
- let player = SKSpriteNode(imageNamed: "ball")
- player.position = CGPoint(x: frame.midX, y: frame.midY)
- player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height / 2.7 )
- player.physicsBody?.categoryBitMask = PhysicsCategory.ball
- player.size = CGSize(width: 25, height: 25)
- player.physicsBody?.isDynamic = true
- addChild(player)
- // Diesen Block einfach ignorieren
- /*let block = SKSpriteNode(imageNamed: "block")
- block.position = player.position
- block.size = CGSize(width: block.size.width, height: block.size.height)
- block.physicsBody = SKPhysicsBody(rectangleOf: block.size)
- block.physicsBody?.categoryBitMask = PhysicsCategory.block
- addChild(block)*/
- }
- // Unterschiedliche Herangehensweisen:
- // Versuch Nr. 1
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- for touch: AnyObject in touches {
- let location = touch.location(in: self)
- let previousPointOfTouch = touch.previousLocation(in: self)
- let amountDragged = location.x - previousPointOfTouch.x
- block.position.x += amountDragged
- if block.position.x > frame.maxX - block.size.width / 2 {
- block.position.x = frame.maxX - block.size.width / 2
- }
- if block.position.x < frame.minX + block.size.width / 2 {
- block.position.x = frame.minX + block.size.width / 2
- }
- }
- }
- // Versuch Nr. 2
- /* override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- for touch in touches {
- let location = touch.location(in: self)
- block.run(SKAction.moveTo(x: location.x, duration: 0.2))
- }
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- for touch in touches {
- let location = touch.location(in: self)
- block.run(SKAction.moveTo(x: location.x, duration: 0.2))
- }
- }*/
- }
Mit freundlichen Grüßen Niklas