iosswiftcocos2d-iphonecocos2d-swift

cocos2d swift how to show a sprite?


So I have my scene presenting and am trying to display a sprite when I press a button. I know the function is called because of a NSLog but I can't get the sprite to show.

func ShowShip() {
var booster = CCBReader.load("ccbResources/Booster")
booster.position = CGPoint(x: 0, y: 0)
self.addChild(booster)
NSLog("created sprite")
}

The log is called but the sprite isn't displayed. I looked at the quickstart tutorial and couldn't see much difference.

edit: tried calling the .png resource directly but got unwrapping error


Solution

  • Try direct method:

    //method_1 : read image from disk

    var booster = CCSprite(imageNamed:"Booster.png") 
    booster.position = CGPoint(x: 50, y: 50)
    self.addChild(booster, z:3)
    

    //method_2 : read image from sprite sheet

        var frame1 = CCSpriteFrameCache.sharedSpriteFrameCache().spriteFrameByName("Booster.png") as CCSpriteFrame
        var booster = CCSprite(spriteFrame: frame1)
        booster.position = CGPoint(x: 50, y: 50)
        self.addChild(booster, z:3)