Cannot convert value of type 'NSMutableArray' to expected argument type '[SKAction]'

佐手、 提交于 2019-12-13 01:37:57

问题


I checked my old game (made in SpriteKit) and I want to update it in Swift 2.0. When I tried to fix it, Xcode found an errors.

Error is: Cannot convert value of type 'NSMutableArray' to expected argument type '[SKAction]'

In code:

torpedo.runAction(SKAction.sequence(actionArray))

Function:

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

}

Can someone help me ?


回答1:


To run a sequence of actions use this code

// 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:


You can do this instead:

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

The method expects a parameter of type [SKAction] which NSMutableArray does not conform to.



来源:https://stackoverflow.com/questions/34374830/cannot-convert-value-of-type-nsmutablearray-to-expected-argument-type-skacti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!