使用Undeclared类型&#NS; NSEvent'

时间:2015-02-14 18:03:05

标签: ios swift sprite-kit

我正在尝试制作鼠标按下事件,但不断收到错误"使用未声明的类型' NSEvent'"在"覆盖func mouseDown(theEvent:NSEvent!){"线。经过研究和尝试不同的事情,我仍然一无所获。以前有人有这个问题吗?

import UIKit
import SpriteKit


let BallCategoryName = "ball"
let MainBallCategoryName = "mainBall"

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)

        // 1. Create a physics body that borders the screen
        let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        // 2. Set the friction of that physicsBody to 0
        borderBody.friction = 0
        // 3. Set physicsBody of scene to borderBody
        self.physicsBody = borderBody

        physicsWorld.gravity = CGVectorMake(0, 0)

        let ball = childNodeWithName(BallCategoryName) as SKSpriteNode
        var mainBall = childNodeWithName(MainBallCategoryName) as SKSpriteNode

    ball.physicsBody!.applyImpulse(CGVectorMake(10, -10))

    ball.physicsBody!.allowsRotation = false
    ball.physicsBody!.friction = 0
    ball.physicsBody!.restitution = 1
    ball.physicsBody!.linearDamping = 0
    ball.physicsBody!.angularDamping = 0

}

override func mouseDown( theEvent: NSEvent!) {
    let action = SKAction.moveTo(
        CGPoint(x:theEvent.locationInWindow.x,y:theEvent.locationInWindow.y),
        duration:2
    );
    MainBallCategoryName.runAction(action)

 }
}

2 个答案:

答案 0 :(得分:2)

iOS设备没有鼠标,iOS没有NSEvent类。它有一个UIEvent类,但iOS上的一个名为mouseDown的函数并没有什么特别之处。 iOS事件报告触摸,而不是鼠标按钮。

如果您从教程中复制了此代码,那么教程适用于OS X,而不是iOS。你应该找一个iOS教程。

答案 1 :(得分:0)

在OS X应用程序中,您不应该:

import UIKit

但请改用

import AppKit