【问题标题】:Cannot convert value of type 'NSMutableArray' to expected argument type '[SKAction]'无法将类型“NSMutableArray”的值转换为预期的参数类型“[SKAction]”
【发布时间】:2016-03-26 07:07:21
【问题描述】:

我检查了我的旧游戏(用 SpriteKit 制作),我想在 Swift 2.0 中更新它。当我试图修复它时,Xcode 发现了一个错误。

错误是:无法将类型“NSMutableArray”的值转换为预期的参数类型“[SKAction]”

在代码中:

torpedo.runAction(SKAction.sequence(actionArray))

功能:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

self.runAction(SKAction.playSoundFileNamed("torpedo.mp3", waitForCompletion: false))

var touch:UITouch = touches.anyObject() as! UITouch 
var location:CGPoint = touch.locationInNode(self)

var torpedo:SKSpriteNode = SKSpriteNode(imageNamed: "torpedo")
torpedo.position = player.position

torpedo.physicsBody = SKPhysicsBody(circleOfRadius: torpedo.size.width/2)
torpedo.physicsBody!.dynamic = true
torpedo.physicsBody!.categoryBitMask = photonTorpedoCategory
torpedo.physicsBody!.contactTestBitMask = alienCategory
torpedo.physicsBody!.collisionBitMask = 0
torpedo.physicsBody!.usesPreciseCollisionDetection = true

var offset:CGPoint = vecSub(location, b: torpedo.position)

if (offset.y < 0){
    return

self.addChild(torpedo)

var direction:CGPoint = vecNormalize(offset)

var shotLength:CGPoint = vecMult(direction, b: 1000)

var finalDestination:CGPoint = vecAdd(shotLength, b: torpedo.position)

let velocity = 568/1
let moveDuration:Float = Float(self.size.width) / Float(velocity)

var actionArray:NSMutableArray =  NSMutableArray()
actionArray.addObject(SKAction.moveTo(finalDestination, duration: NSTimeInterval(moveDuration)))
actionArray.addObject(SKAction.removeFromParent())

torpedo.runAction(SKAction.sequence(actionArray)) //<-- Here is Error

}

有人可以帮我吗?

【问题讨论】:

    标签: swift sprite-kit nsmutablearray swift2 skaction


    【解决方案1】:

    要运行一系列操作,请使用此代码

    // REMOVE THIS var actionArray:NSMutableArray =  NSMutableArray()
    let move = SKAction.moveTo(finalDestination, duration: NSTimeInterval(moveDuration))
    let remove = SKAction.removeFromParent()
    torpedo.runAction(SKAction.sequence([move,remove]))
    

    【讨论】:

      【解决方案2】:

      您可以这样做:

      var actionArray = Array<SKAction>()
      actionArray.append(SKAction.moveTo(finalDestination, duration: NSTimeInterval(moveDuration)))
      actionArray.append(SKAction.removeFromParent())
      torpedo.runAction(SKAction.sequence(actionArray))
      

      该方法需要一个 NSMutableArray 不符合的 [SKAction] 类型的参数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-27
        • 2016-07-02
        • 2016-03-21
        • 1970-01-01
        • 2020-03-01
        • 2016-08-01
        • 2017-02-07
        相关资源
        最近更新 更多