Sprite Sheet Issues? - xcode

I created sprite sheets with Zwoptex and generated the .plist file. I am having issues linking these sprite sheets to my iOS game. I loaded the .plist and .png files created on Zwoptex to Xcode, but the changes are not being recognized. I don't know why this is happening. The game was created with cocos.
Thanks!

Here is an example of how to use a spriteSheet in cocos2d im not sure if this is what your looking for as to the lack of detail but
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:#"TreeSequance.plist"];
CCSpriteBatchNode *treeSheet = [CCSpriteBatchNode batchNodeWithFile:#"TreeSequance.png"];
CCSprite *smallerTree = [CCSprite spriteWithSpriteFrameName:#"Tree1.png"];
smallerTree.position = ccp(500,500);
[treeSheet addChild:smallerTree];
[self addChild:treeSheet];

Related

SpriteKit unarchiveFromFile does not load textures from asset catalogues and atlases

I'm still new with SpriteKit, and wanted to see if I can construct a level using Xcode's SKS editor. When i added a couple of sprites with the "Spaceship.png" texture and built the template app, the textures don't load.
Here's a screenshot of the vanilla OSX Game template using Swift as the language, and adding a supplied "Spaceship.png" sprite. The texture shows fine:
And here's the result of building and running the app with only that modification to the template:
The debug console displays this warning message:
I tried to add an .atlas folder, and got the same result. the scene just displays the red X icon in place of the sprite. If the added sprites where just color sprites, they display fine. I had an app some time ago that I scrapped, where I used to load the SKScene and would manually add the sprite assets within to my SKScene sub-class, and it worked fine.
If, however, i moved the textures - "Spaceship.png" as an example - to the root of the project, i.e. not inside an asset catalogue or .atlas folder, the scene loads with the textures displaying fine.
Here's the texture added to the root of the project:
And this is the desired result:
I tried to manually add the loaded assets from the SKS file to the scene via enumerateChildNodesWithName(_,usingBlock) and I get the same result if the textures were not in the root of the project folder.
This is me trying to add the assets manually:
func applicationDidFinishLaunching(aNotification: NSNotification) {
/* Pick a size for the scene */
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let sceneToBePresented = GameScene()
/* Set the scale mode to scale to fit the window */
sceneToBePresented.scaleMode = .AspectFill
sceneToBePresented.size = scene.size
/* Sprite Kit applies additional optimizations to improve rendering performance */
self.skView!.ignoresSiblingOrder = true
self.skView!.showsFPS = true
self.skView!.showsNodeCount = true
scene.enumerateChildNodesWithName("*") { node, stop in
sceneToBePresented.addChild(node.copy() as SKSpriteNode)
}
self.skView!.presentScene(sceneToBePresented)
}
}
I looked around SpriteKit classes - SKNode, SKScene, SKTexture, SKSPriteNode - for any clue about paths, caching, preloading, or anything but was unable to find a thing that could make this work.
I am running Xcode 6.1.1 and my target is 10.9, and the language I'm using is Swift, although the same behavior holds true using ObjC. Is this a feature or a bug? Anybody else running/ran into a similar situation ?
Thanks in advance for any help
[UPDATE]
Looks like this is not implemented yet - loading textures from asset catalogs - as this post in the dev forums discusses the same issue, and his solution is to rename the asset catalogues to be the same name as the texture. In essence, what I found out about having the image files in the root folder: How do you load sprite textures from Images.xcassets when using SKS scene file, although my earlier app which i managed to roll-back to a working state does load textures from .atlas'es, but i can't seem to do it with a clean template!!!
[Answering my own question here]
As far as I can tell, at this moment, loading texture from asset catalogues - Images.xcassets - while unarchiving or deserializing an SKScene file does not work on OSX based on my attempts and the devforumns post referenced above.
Loading of the textures from image atlases, however, works by forcing SKTexture to load the image. Forcing or 'touch'ing SKTexture can be done via the preloadWithCompletionHandler(_:) or preloadTextures(_:, withCompletionHandler:) methods, or simply by printing the description of the sprite's SKTexture as i have discovered.
For the benefit of anybody who might need further assistance, here is a code snippet that preloads the textures after unarchiving an SKS file:
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
for child in scene.children as [SKNode] {
if child is SKSpriteNode {
let sprite = child as SKSpriteNode
sprite.texture?.preloadWithCompletionHandler({ })
/* or even a simple debug log of the texture */
/* println(sprite.texture) */
}
}
/* Do your other stuff ... */
...
}
If I'm wrong please correct me. Until somebody does, or Apple fixes the discrepancy between SpriteKit's behavior between iOS and OSX, I will not be looking for another solution, and will follow Murphey's law:
If it works, don't fix it
Follow Salam Horani solution's, for Swift 2.x you dont have yet "unarchiveFromFile" so you can create scene like this:
if let gameScene = GameScene(fileNamed: "GameScene") {
// ...
}

Why is my art displayed at 2x on screen?

I am creating a Universal App.
I have created my art in illustrator to fit properly on an iPhone screen, specifically at 640x1136 px. I exported PNGs and named myArt#2x.png and also a 50% version named myArt.png. The art is in an atlas folder.
I am creating sprite nodes a like this:
NSString * backgroundName=#"starField";
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:backgroundName];
background.position = CGPointMake(screenWidth/2, screenHeight/2);
background.zPosition = LayerBackground;
[_worldNode addChild:background];
The problem is that when I run this in the simulator (iPhone 4 w/retina), the images are scaled up. I have to ousel xScale and yScale to bring it back to actual size.
Why? What am I doing wrong?
Thanks,
rich!
You need to set the size property of the SKNode I think. For example:
background.size = CGSizeMake(screenWidth, screenHeight);
Okay I think I solved it -
I don't start creating my content until viewWillLayoutSubviews had been called. The default SpritKit Template uses viewDidLoad, but I changed that to viewWillLayoutSubviews and everything looks fine now.
I think I was trying to create stuff before the view "sorted" itself out...
Not sure what the difference is but it worked.
-r

Problem with creating animations

I'm working with cocos2d 0.99.5. I need to create animations. I have 01.png in my resources and in my project. It's ok. I don't understand why I can't create a frame. I've got error SIGABRT.
CCSprite *frame = [CCSprite spriteWithSpriteFrameName:#"01.png"];
I've tried to use this
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:#"01.png"];
But I've got the same error.
That's because you must have your sprite in the spriteSheet. Here is the tutorial about creating animations: http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:animation

Organizing files in Cocos2d

I have just started learning Cocos2d using the great tutorials
Ray Wenderlich has produced. However I'm getting to a point where my HelloWorldScene main file is getting a little large.
I would like to be able to sort sprite generation methods into one file, misc. methods into another and so on, leaving me with the core level files containing the different scenes and init methods.
Is it possible to copy methods over to a new .m file and bring them into the HelloWorldScene.m when I need them?
How do you organize your game files?
I highly recommend layering your game. It keeps it organised and neater, reducing the amount of code in any one file.
Within cocos2d, there are 3 classes you should take note of to achieve this:
CCDirector, CCScene, CCLayer.
What you can do is, create multiple layers within a single scene (GameScene).Eg:
Have a HUD, Game Interface, Background and Foreground.
These can all be split into their separate CCLayers and added together within the CCScene.
So now you have split HelloWorld.m into:
GameScene.m
HudLayer.m
GameInterfaceLayer.m
Background.m
Forground.m
After importing all the h files into your GameScene.h, You can then add each Layer to the scene as Follows:
#implementation GameScene
+(CCScene){
HudLayer *hud = [HudLayer node];
[self addChild: hud z:3];
GameInterfaceLayer *game = [GameInterfaceLayer node];
[self addChild: game z:1];
...} etc
#end
Once you do this, if you dont change your AppDelegate, your CCDirector will give you an error. CCDirector basically dictates which Scene is loaded.
Change this to point to your GameScene.
You can visualise: the Director of the Movie, Filming a Scene, filled with many Layers.

Replacing image in sprite - cocos2d game development of iPhone

I want to change the sprite image.
Say for example:
mainSprite=[Sprite spriteWithFile:#"redFile.png"];
[self addChild:mainSprite];
Here, Sprite is already added to a layer. I have mainSprite (pointer) which can access it.
If I change
[mainSprite setOpacity:150];
it works perfectly. But here I want to change the sprite image instead of opacity.
But I don't know how.
#sagar:
In cocos2d 0.99.x I use
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"sprite.png"]];
It works. The next question is, how can I get back to my previous sprite? Thanks
Ok. Damn Simple.
I find it by R & D.
Texture2D *x=[[Texture2D alloc]initWithImage:[UIImage imageNamed:#"box-purple-dark.png"]];
[mainSprite setTexture:x];
Replace image in sprite :
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"image.png"]];
Replace image in sprite :
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:#"new-image.png"];
[mainSprite setTexture:tex1];

Resources