我试图从GameScene.swift
调用GameViewController.swift
中的函数,但无论我尝试什么,我都会在调用#34中获得参数#1的"缺失参数。错误。
我做错了什么?
//GameViewController.swift file
import UIKit
import SpriteKit
import AVFoundation
class GameViewController: UIViewController, UITextFieldDelegate{
var backgroundMusicPlayer:AVAudioPlayer = AVAudioPlayer()
var scene:GameScene!
//This function gives the error:
func swipedRight(sender: UISwipeGestureRecognizer){
scene.swipedRight1() //error: Missing argument for parameter #1 in call
}
override func viewDidLoad() {
super.viewDidLoad()
let skView = view as SKView
skView.multipleTouchEnabled = false
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
skView.presentScene(scene)
let swipeRight:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedRight:"))
swipeRight.direction = .Right
view.addGestureRecognizer(swipeRight)
}
override func viewDidLayoutSubviews() {
var bgMusicURL:NSURL=NSBundle.mainBundle().URLForResource("bgMusic",withExtension:"mp3")!
backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: bgMusicURL, error:nil)
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay()
backgroundMusicPlayer.play()
println("music playing")
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Gamescene.swift文件
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var player:SKSpriteNode = SKSpriteNode()
required init(coder aDecoder:NSCoder) {
fatalError("NSCoder not supported")
}
override init(size:CGSize) {
super.init(size:size)
anchorPoint = CGPoint(x:0, y:1.0)
let background = SKSpriteNode(imageNamed: "bg3")
background.position = CGPoint(x:0, y:0)
background.anchorPoint=CGPoint(x:0,y:1.0)
addChild(background)
player = SKSpriteNode(imageNamed:"kikker1")
player.xScale = 0.22
player.yScale = 0.22
self.physicsWorld.gravity = CGVectorMake(0,0)
self.physicsWorld.contactDelegate = self
player.position = CGPointMake(self.frame.size.width/2, -610)
addChild(player)
println("klaar")
println(player.position)
}
//This is the function I am trying to call
func swipedRight1(sender:UISwipeGestureRecognizer){
println("swipe right")
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
答案 0 :(得分:3)
您尝试呼叫的功能具有以下签名:
func swipedRight1(sender:UISwipeGestureRecognizer)
但你没有参数调用它:
scene.swipedRight1()
只需将预期参数传递给它,在这种情况下,它与调用方法中的发件人相同:
scene.swipedRight1(sender)
答案 1 :(得分:0)
函数swipedRight1(sender:...)需要一个类型为sender的参数,所以你应该尝试提供这样的周界:
func swipedRight(sender: UISwipeGestureRecognizer){
scene.swipedRight1(sender: UISwipeGestureRecognizer)
}