gpt4 book ai didi

Swift 3 期望参数错误

转载 作者:行者123 更新时间:2023-11-28 11:02:32 26 4
gpt4 key购买 nike

我正在创建一些函数以制作类似于 Flappy Bird 的游戏。我是一个完全的初学者,在继续之前,我正在尝试完全理解所有内容。我已经能够让我的障碍物移动,但是当我试图将它放入一个函数中以允许我在以后遇到多个障碍物时具有更大的灵 active 时,我收到了一个错误。

'无法将类型 '()' 转换为预期的参数类型 'SKAction'

GameScene 类:SKScene、SKPhysicsContactDelegate {

var Player = SKSpriteNode()
var Ground = SKSpriteNode()
var Roof = SKSpriteNode()
var Background = SKSpriteNode()
let Obstacle1 = SKSpriteNode(imageNamed: "Fire Barrel 1")

override func didMove(to view: SKView) {

// Create Background Color
backgroundColor = bgColor

// Set World Gravity
self.physicsWorld.gravity = CGVector(dx: 0.0, dy: -4.0)

// Create Player
Player = SKSpriteNode(imageNamed: "Player")
Player.setScale(0.5)
Player.position = CGPoint(x: -self.frame.width / 2 + 100, y: -Player.frame.height / 2)

self.addChild(Player)

// Create Ground
Ground = SKSpriteNode(imageNamed: "BGTileBtm")
Ground.anchorPoint = CGPoint(x: 0,y: 0.5)
Ground.position = CGPoint(x: -self.frame.width / 2, y: -self.frame.height / 2)

self.addChild(Ground)

// Create Roof
Roof = SKSpriteNode(imageNamed: "BGTileTop")
Roof.anchorPoint = CGPoint(x: 1,y: 1)
Roof.position = CGPoint(x: -self.frame.width / 2, y: self.frame.height / 2 - Roof.frame.height)
Roof.zRotation = CGFloat(M_PI)

self.addChild(Roof)

// Set Physics Rules
Player.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Player"), size: Player.size)
Player.physicsBody!.affectedByGravity = true
Player.physicsBody!.allowsRotation = false

Ground.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "Ground"), size: Ground.size)
Ground.physicsBody!.affectedByGravity = false
Ground.physicsBody!.isDynamic = false

// Obstacle
func addObstacle1(){

Obstacle1.position = CGPoint(x: self.frame.width / 2, y: -self.frame.height / 2 + Obstacle1.frame.height)
Obstacle1.zPosition = 1
addChild(Obstacle1)
}

func moveObstacle1(){

let distance = CGVector(dx: -self.frame.width, dy: 0)
let moveDistance = SKAction.move(by: distance, duration: 5)
run(moveDistance)

}

addObstacle1()
Obstacle1.run(moveObstacle1())


}

最佳答案

moveObstacle1 的声明更改为:

func moveObstacle1() -> SKAction{

let distance = CGVector(dx: -self.frame.width, dy: 0)
let moveDistance = SKAction.move(by: distance, duration: 5)
return moveDistance

}

编辑:

关于您的评论,

run 是一个方法。当您调用它时,它会运行您传入的 SKAction。就是这样!您要做的是 run(moveObstacle1())。这到底是什么意思?如何将方法调用作为参数传递?在运行时,moveObstacle1()返回值 被传递给run。换句话说,要编译 run(moveObstacle1())moveObstacle1() 必须使用 return 语句返回一个值。该值必须是 SKAction 类型,因为这是您要传递给 run 的内容。

return 用于从 moveObstacle1() 返回一个值,以便您可以调用 run(moveObstacle1())

run 只是一个常规的老方法。

关于Swift 3 期望参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com