0

I used below code to create sprite in swift.

    var bg : CCSprite = CCSprite.spriteWithImageNamed("Default.png");
    bg.position = ccp(SW*0.5, SH*0.5)
    self.addChild(bg)

Please check image, its giving error for 1st line.enter image description here

Error : Type 'AnyObject' cannot be implicitly downcast to 'CCNode': did you mean to use 'as' to force downcast?

Is there any Cocos2d-Swift documents online ?

iPhoneProcessor
  • 4,980
  • 6
  • 27
  • 49

1 Answers1

2

Just rewrite

var bg : CCSprite = CCSprite.spriteWithImageNamed("Default.png")

as

var bg = CCSprite.spriteWithImageNamed("Default.png") as CCSprite

Swift does not implicitly convert (in this case: AnyObject! to CCSprite), you have to add an explicit cast with as.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382