- Registriert
- 06.12.18
- Beiträge
- 2
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.
////// Den ViewController habe ich wie folgt verändert /////
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.
Code:
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))
}
}*/
}
////// Den ViewController habe ich wie folgt verändert /////
Code:
import UIKit
import SpriteKit
import GameplayKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let view = self.view as! SKView? {
// Load the SKScene from 'GameScene.sks'
let scene = GameScene(size: view.bounds.size)
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
view.presentScene(scene)
view.ignoresSiblingOrder = false
view.showsPhysics = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}