我试图在你养猪的地方做一个游戏,你可以在那里滑动或抓住猪的动作。我有一个带有属性的猪的课程但GameScene.swift
中出现错误,说guy
不是porker
的成员。请帮忙。
import SpriteKit
import UIKit
class GameScene: SKScene {
var porker:Porker!
var touchLocation = CGFloat()
var gameOver = false
override func didMoveToView(view: SKView) {
addBG()
addPig()
}
func addBG() {
let bg = SKSpriteNode(imageNamed: "bg");
addChild(bg)
}
func addPig() {
let Pig = SKSpriteNode(imageNamed: "pig")
porker = Porker(guy:Pig)
addChild(Pig)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch: AnyObject in touches{
if !gameOver {
touchLocation = (touch.locationInView(self.view!).y * -1) + (self.size.height/2)
}
}
let MoveAction = SKAction.moveToY(touchLocation, duration: 0.5)
MoveAction.timingMode = SKActionTimingMode.EaseOut
Porker.guy.runAction(MoveAction) }
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
这是猪班
import Foundation
import SpriteKit
class Porker {
var Guy:SKSpriteNode
var speed = 0.1
init(guy:SKSpriteNode){
self.Guy = guy
}
}
答案 0 :(得分:0)
首先,变量/常量/参数名称应以小写字母开头,因此不要将它们与类名混合。例如,
class Porker {
var guy:SKSpriteNode
var speed = 0.1
init(guy:SKSpriteNode){
self.guy = guy
}
}
开始内部接触:
let moveAction = SKAction.moveToY(touchLocation, duration: 0.5)
moveAction.timingMode = SKActionTimingMode.EaseOut
porker.guy.runAction(MoveAction)
希望能解决问题!