简体   繁体   中英

Error In Touches Began Function Sprite Kit

Im trying make a redball in my XCode Poject move wherever my finger does. this is my code and It is not working. How would I fix my code?

import SpriteKit

class GameScene: SKScene {

let  redBall = SKSpriteNode(imageNamed: "redball")
var touching = false
var touchPoint: CGPoint = CGPoint()

override func didMoveToView(view: SKView) {

    //Middle Rectangle
    var mid = SKShapeNode(rectOfSize: CGSize(width: 50, height: 300))
    mid.physicsBody = SKPhysicsBody(edgeLoopFromRect: mid.frame)
    mid.physicsBody?.friction = 1
    mid.name = "middle"
    mid.fillColor = UIColor.whiteColor()
    mid.position = CGPointMake(self.size.width/2, self.size.height/2)
    addChild(mid)


    //World Border
    let worldBorder = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody = worldBorder





    //RedBall
    let redBall = SKSpriteNode(imageNamed: "redball")
    redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.width/2)
    redBall.physicsBody?.friction = 1
    redBall.physicsBody?.restitution = 1
    redBall.position = CGPointMake(self.size.width/4, self.size.height/2 + 20)
    addChild(redBall)


    //Paddle
    let paddle = SKShapeNode(rectOfSize: CGSize(width: 75, height: 20))
    paddle.physicsBody = SKPhysicsBody(edgeLoopFromRect: paddle.frame)
    paddle.position = CGPointMake(self.size.width/4 - 100 , self.size.height/3)
    addChild(paddle)

    //Physics World


    self.physicsWorld.gravity = CGVectorMake(0, 0)

}

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    let location = touch.locationInNode(self)
    println("hi")
    if location == redBall.position{
        redBall.position = location
        println("yo")


    }else{
        println("nothin")
    }


}

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {


}

override func update(currentTime: CFTimeInterval) {

}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {

}
override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
}
}

I do not know why this code is not working how can I fix this thanks!

If the ball is to be situated at the position of your touch and is also supposed to move with your finger you probably need something like this:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as! UITouch
    redBall.position = touch.locationInNode(self)
}

This will reset the ball's position to the position of your touch.

override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        let touch = touches.first as! UITouch
        redBall.position = touch.locationInNode(self)
}

And the exact same thing in touchesMoved will move the ball wherever your finger moves.


Also: There's a bug in your code. Your redBall property is never added to the scene, so that changing the position won't make a difference:

//RedBall
let redBall = SKSpriteNode(imageNamed: "redball") // <- this needs to go
redBall.physicsBody = SKPhysicsBody(circleOfRadius: redBall.size.width/2)
redBall.physicsBody?.friction = 1
redBall.physicsBody?.restitution = 1
redBall.position = CGPointMake(self.size.width/4, self.size.height/2 + 20)
addChild(redBall)

The above is a local scope sprite with the same name as your redBall-property. It is this node that is added to your scene, but you have no way of accessing it from elsewhere in the code. Remove let redBall = SKSpriteNode(imageNamed: "redball") from the above snippet and you should probably be up and running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM