加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift Sprite Kit开始触摸功能

发布时间:2020-12-14 04:56:53 所属栏目:百科 来源:网络整理
导读:我正在搞乱一个 xcode项目,当我触摸它时,我正试图让一个红色的球SKShapeNode四处移动.触摸我自己的源代码开始上课并没有做到.我的代码有什么缺陷,我需要做些什么来修复它. import SpriteKitclass GameScene: SKScene { //Rectangle Skeletions var leftWallR
我正在搞乱一个 xcode项目,当我触摸它时,我正试图让一个红色的球SKShapeNode四处移动.触摸我自己的源代码开始上课并没有做到.我的代码有什么缺陷,我需要做些什么来修复它.

import SpriteKit

class GameScene: SKScene {

    //Rectangle Skeletions
    var leftWallRect = CGRect(x: 0,y: 0,width: 40,height: 1000)

    var ball = SKShapeNode(circleOfRadius: 25);


    override func didMoveToView(view: SKView) {

        // Declare the Sprites in your scene
        var ball = SKShapeNode(circleOfRadius: 25);
        let leftWall = SKShapeNode(rect: leftWallRect);
        let rightWall = SKShapeNode(rect: leftWallRect);
        let pin = SKSpriteNode(imageNamed: "pin");

        // Ball Properties
        ball.strokeColor = UIColor.blackColor()
        ball.fillColor = UIColor.redColor()
        ball.position = CGPointMake(self.frame.width / 2,self.frame.height / 4 - 100 )
        self.addChild(ball)

        // Left Wall Properties
        leftWall.fillColor = UIColor.purpleColor()
        leftWall.strokeColor = UIColor.blackColor()
        leftWall.position = CGPointMake(self.frame.size.width / 3 - 45,self.frame.size.height / 2 - 400)
        self.addChild(leftWall)

        //Right Wall Properties
        rightWall.fillColor = UIColor.purpleColor()
        rightWall.strokeColor = UIColor.blackColor()
        rightWall.position = CGPointMake(self.frame.size.width / 2 + 175,self.frame.size.height / 2 - 400)
        self.addChild(rightWall)
    }

    override func touchesBegan(touches: Set<NSObject>,withEvent event: UIEvent) {
        /* Called when a touch begins */

        let touch = touches.first as! UITouch
        let touchLocation = touch.locationInNode(self)

        if touchLocation == ball.position{

            ball.position = touchLocation

            println("Touches");
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

解决方法

大笑哇,这是一个有趣的,如果touchLocation == ball.position我会留下深刻的印象,如果你可以击中你的球的确切像素位置.你想要的是这个:

let touch = touches.first as! UITouch
let touchLocation = touch.locationInNode(self)
let touchedNode = self.nodeAtPoint(touchLocation)
if touchedNode == ball{
...

但就个人而言,我要做的是制作SKSpriteNode的子类,并在这个孩子的触摸程序中做你的球的触摸代码

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读