0

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

Guru
  • 21,652
  • 10
  • 63
  • 102
shwick
  • 4,277
  • 6
  • 21
  • 28
  • did you set zPosition of sprite like `booster.zPosition = 1`? – Dharmesh Kheni May 09 '15 at 04:52
  • Hi Dharmesh I stopped using Booster because I made it into a code connection class and I just want the sprite. So when I try to load the sprite directly I still get an error "invalid spriteframe for sprite" – shwick May 10 '15 at 16:39

1 Answers1

0

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)
Guru
  • 21,652
  • 10
  • 63
  • 102
  • Hi unfortunately I get "invalid spriteframe for sprite" when I try that method. – shwick May 10 '15 at 16:32
  • @shwick, if it is sprite sheet then you can use spriteFrame method..updated answer. – Guru May 10 '15 at 17:40
  • I'll try your latest edit nature when my computer restrats it just crashed. I'm really not sure whats going on so far at all. CCSprite's image constructor says, "it takes an image with a name, or the name of an image in a spritesheet" implying it will load from either. And both methods have completely failed for me. I tried naming the images from the spritesheet, sprite frame exception. Just tried throwing an image by itself into spritebuilder, tried loading that by name (not in spritesheet), that failed too, same exception. No idea whats going on right now. – shwick May 10 '15 at 20:20
  • ok nature with your latest suggestoin i get: "unexpected found nil while unwrapping an optional value". I'd be willing to link my bitbucket repository if you are willing to look at my project. But I am going to keep bumping this question on the cocos2d forum until it reaches 9000 posts because this is a simple thing that shouldn't be this difficult: how to load an image with an api built around 2d images. – shwick May 10 '15 at 20:40
  • It was a corrupted project, or it was the spritesheet messing it up. – shwick May 19 '15 at 13:49