Debug the SpriteSheet-Unpacker cocos2d project - Closed - cocos2d-iphone

[Solved] Guys, I found another tool on Mac app store named Plist Extractor that can help me do this. Thank you guys so so much!
I found this project on GitHub. It is a utility to unpack cocos2D sprite sheet used by cocos2d engine. After try reading and debugging this project, I cannot found any errors but it does not work as I expect. Particularly, it always generate blank images from sprite frame and save it to disk. So anyone please help me debug it. I also attach a sprite sheet for you guys to test.
Link to sprite sheet.

Try This SpriteSheetUnPacker its working with your sprite sheet.

Try this
#import "ExtractImagesFromTP.h"
[ExtractImagesFromTP createImagesFromTPPlist:#"sprite_sheet.plist"];
ExtractImagesFromTP Class
.h
#interface ExtractImagesFromTP : NSObject
+ (void)createImagesFromTPPlist:(NSString *)plist;
#end
.m
#implementation ExtractImagesFromTP
+ (NSDictionary *)getAllFrameFromPlist:(NSString *)plistFile
{
NSString *path = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: plistFile];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
return [dictionary objectForKey: #"frames"];
}
+ (void)createPngFromSprite:(CCSprite *)sprite fileName:(NSString *)fileName
{
sprite.position = ccpMult(ccpFromSize(sprite.contentSize), 0.5);
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CCRenderTexture* render = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];
[render begin];
[sprite visit];
[render end];
NSLog(#"Texture Name: %#", fileName);
[render saveToFile:fileName format:CCRenderTextureImageFormatPNG];
}
+ (void)createImagesFromTPPlist:(NSString *)plist
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: plist];
NSDictionary *frameNames = [self getAllFrameFromPlist: plist];
for (NSString *frameName in frameNames)
{
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
CCSprite *sprite = [CCSprite spriteWithSpriteFrame: frame];
[self createPngFromSprite:sprite fileName:frameName];
}
}
#end

Related

Advanced Spritesheeting - using a spritesheet in multiple classes

So after taking a little break after being very frustrated for months on end with this issue, I am now back and attempting to solve my issue once and for all.
I’m not going to post code, because the code I have is messy and patchy and is from countless points of view.
My issue is this:
I have a sprite, and his information is stored in a class called player.m. I have a game level, and it is stored in GameLevelLayer.m
I have player.m store velocity, direction, and all the information about the character, and then I have the GameLevelLayer.m implement, move, use bounds to check collisions, etc.
I now want to make the character move using a spritesheet (I know how to do spritesheeting, I just don’t know how to do it while working with two different classes)
My question is, do I create the batchnodes and spritesheet information (CCBatchnodes, caches, etc) and all of the actions and everything in player.m, and then run them in GameLevelLayer.m? Or do I create all that in GameLevelLayer.m
I really need some help here because I’ve been stuck on this for months
EDIT:
Here is where I am, thanks to a suggestion from a community member.
This is my entire player.m (Long story short, I define everything about the player 'you might just want to pay attention to the init, because that seems to be the problem. The other stuff is my physics engine')
#import "Player.h"
#import "SimpleAudioEngine.h"
#import "GameLevelLayer.h"
#implementation Player
#synthesize velocity = _velocity;
#synthesize desiredPosition = _desiredPosition;
#synthesize onGround = _onGround;
#synthesize forwardMarch = _forwardMarch, mightAsWellJump = _mightAsWellJump, isGoingLeft = _isGoingLeft;
#synthesize WalkAction = _WalkAction;
#synthesize isMoving = _isMoving;
-(id)initWithTexture:(CCTexture2D *)texture{
if (self = [super init]) {
self.velocity = ccp(0.0, 0.0);
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: #"BankerSpriteSheet_default.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"BankerSpriteSheet_default.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <=6; ++i) {
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:#"banker%d.png", i]]];
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.1f];
self = [super initWithSpriteFrameName:#"banker1.png"];
self.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
//[_Banker runAction:_WalkAction];
[spriteSheet addChild:self];
[[CCAnimationCache sharedAnimationCache] addAnimation:walkAnim name:#"walkAnim"];
}
}
return self;
}
-(void)update:(ccTime)dt {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
CGPoint jumpForce = ccp(0.0, 310.0);
float jumpCutoff = 150.0;
if (self.mightAsWellJump && self.onGround) {
self.velocity = ccpAdd(self.velocity, jumpForce);
if (![defaults boolForKey:#"All Muted"]) {
if (![defaults boolForKey:#"SFX Muted"]) {
[[SimpleAudioEngine sharedEngine] playEffect:#"jump.wav"];
}
}
} else if (!self.mightAsWellJump && self.velocity.y > jumpCutoff) {
self.velocity = ccp(self.velocity.x, jumpCutoff);
}
CGPoint gravity = ccp(0.0, -450.0);
CGPoint gravityStep = ccpMult(gravity, dt);
CGPoint forwardMove = ccp(800.0, 0.0);
CGPoint forwardStep = ccpMult(forwardMove, dt);
self.velocity = ccp(self.velocity.x * 0.90, self.velocity.y); //2
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
if (!self.isMoving) {
//[self runAction: _WalkAction];
//self.isMoving = YES;
}
} //3
CGPoint minMovement = ccp(0.0, -450.0);
CGPoint maxMovement = ccp(120.0, 250.0);
self.velocity = ccpClamp(self.velocity, minMovement, maxMovement);
self.velocity = ccpAdd(self.velocity, gravityStep);
CGPoint stepVelocity = ccpMult(self.velocity, dt);
if (!self.isGoingLeft) {
self.desiredPosition = ccpAdd(self.position, stepVelocity);
}else{
self.desiredPosition = ccp(self.position.x-stepVelocity.x, self.position.y+stepVelocity.y);
}
}
-(CGRect)collisionBoundingBox {
CGRect collisionBox = CGRectInset(self.boundingBox, 3, 0);
//CGRect collisionBox = self.boundingBox;
//collisionBox = CGRectOffset(collisionBox, 0, -2);
CGPoint diff = ccpSub(self.desiredPosition, self.position);
CGRect returnBoundingBox = CGRectOffset(collisionBox, diff.x, diff.y);
return returnBoundingBox;
}
#end
So now the issue is, how do i get the player to appear in the game like a normal sprite, and then how do I run the animations when the player begins to move forward
Your game architecture is very good! Keep player properties on player and use GameLayer only to send player actions is the best approach.
If your Player Class extends from CCSprite, put everithing related to player in your Player Class: Run animations on it, load the cache on player constructor (use the CCSprite initWithTexture:rect:rotated: or a custom autorelease constructor, like a +(id)player ).
Then, in your GameLevelLayer you will need a CCSpriteBatchNode to add your player to it. You can use this batch node to add other objects that use same spritesheet.
EDIT:
Reviewing your code, your initWithTexture is wrong. The correct is to call [super initWithTexture:]:
-(id)initWithTexture:(CCTexture2D *)texture{
if (self = [super initWithTexture:texture]) {
Then, in your GameLevelLayer, create your player and add it to a CCSpriteBatchNode:
// *** In GameLevelLayer.m ***
// Create player
Player *player = [Player spriteWithFile:#"player.png"];
// Create batch node
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithTexture:player.texture];
// Add batch node as child
[self addChild:batchNode];
// Add player as batch node's child
[batchNode addChild:player];
// Set player position
player.position = ccp(100,100);
Your GameLevelLayer needs to do only this to create your Player. And when it detects user input, call methods like "jump", "moveRight", "moveLeft" in your player. Also detect collisions and send actions to player like "die", "getSpecialItem".
// *** In GameLevelLayer.m ***
[player jump];
[player die];
So, your player needs to handle these methods and performs its own logic to do the actions:
// *** In Player.m ***
- (void)moveRight
{
self.position = ccpAdd(self.position, ccp(10,0));
}

Cocos2d: Black screen when removing sprites

I'm currently doing the tutorial http://www.raywenderlich.com/25736/how-to-make-a-simple-iphone-game-with-cocos2d-2-x-tutorial .
I have a problem on the part that reacts to when a ninja star hits the monsters. My code is:
- (void)update:(ccTime)dt {
NSMutableArray *projectilesToDelete = [[NSMutableArray alloc] init];
for (CCSprite *projectile in _projectiles) {
NSMutableArray *monstersToDelete = [[NSMutableArray alloc] init];
for (CCSprite *monster in _monsters) {
if (CGRectIntersectsRect(projectile.boundingBox, monster.boundingBox)) {
[monstersToDelete addObject:monster];
}
}
for (CCSprite *monster in monstersToDelete) {
[_monsters removeObject:monster];
[self removeFromParentAndCleanup:YES];
}
if (monstersToDelete.count > 0) {
[projectilesToDelete addObject:projectile];
}
[monstersToDelete release];
}
for (CCSprite *projectile in projectilesToDelete) {
[_projectiles removeObject:projectile];
[self removeChild:projectile cleanup:YES];
}
[projectilesToDelete release];
}
which works okay, does not crash, but when I hit a monster with an projectile, the screen turns black on the simulator. No error or anything. I logged the CGRectIntersectRect, and it records as it is supposed to. The problem is that when this happens, it all turns black. Any idea why?
I looked at the tutorial, and the line i identified in the comments above reads :
[self removeChild:monster cleanup:YES];
Try that.
You're doing [self removeFromParentAndCleanup:YES] which removes your current layer from the parent. So you get a black screen.
You probably want to remove the child monster from the layer instead.

EXC BAD ACCESS With Cocos2d pushscene that works for blank scene

So I am trying to do a levelup screen with pushscene/popscene. The pushscene works when it's a blank scene, but not when it's my scene that I want. My scene/layer loads completely with all images and text displaying their exact correct content. After all of the images load there is a EXC BAD ACCESS that doesn't seem to be linked to any particular message being sent. Any help or further diagnostic tests would be appreciated.
I have a version where I commented out the sprites and labels and it still crashes. Is there something big that I'm missing?
EDIT: I've added the [self = [super init]] and [super onEnter] methods and still same problem. It's something else. Any ideas?
EDITEDIT: I think this has something to do with the optionsArray I'm using, not sure what objects need to be retained. The array is a CCArray and contains NSDictionaries of differing capacities
#import "LevelupLayer.h"
#import "GameManager.h"
#implementation LevelupLayer
#synthesize optionsArray,spritesArray;
#synthesize confirmLabel;
#synthesize counter;
+(id) scene {
CCScene *scene = [CCScene node];
CCLayer* layer = [LevelupLayer node];
[scene addChild:layer];
return scene;
}
-(void)onEnter
{
counter = 1; // for debugging
//Detemine what levelups are possible
GameManager* gm = [GameManager sharedManager]; //GameManager is a helper that oversees communication between layers and plists
optionsArray = [gm possibleLevelups]; //Access plist and formats data into expected format
[optionsArray retain];
int numPossibilities = [optionsArray count];
//Build Levelup layer based on possible options
CGSize size = [[CCDirector sharedDirector] winSize];
//float positionIncrement = (size.width / numPossibilities) - ((size.width/numPossibilities) * 0.5);
float positionIncrement = (size.width / numPossibilities);
float stripWidth = size.width / numPossibilities;
for (int i = 0; i < numPossibilities; i++) {
int slot = i+1;
NSDictionary* optionDict = [optionsArray objectAtIndex:i];
NSString* name = [optionDict objectForKey:#"name"];
NSString* title = [optionDict objectForKey:#"title"];
NSString* description = [optionDict objectForKey:#"description"];
// Add the sprite
CCSprite* optionSpite = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:#"%#.png",name]];
[self addChild:optionSpite];
[spritesArray addObject: optionSpite];
optionSpite.position = CGPointMake(slot * positionIncrement, size.height*0.60);
[optionSpite setAnchorPoint:CGPointMake(0.5f, 0.5f)];
// Add the description
CCLabelBMFont *optionDescription = [CCLabelBMFont labelWithString:description fntFile:#"bodyFont.fnt" width:stripWidth alignment:kCCTextAlignmentCenter];
[self addChild:optionDescription];
optionDescription.position = CGPointMake(slot * positionIncrement, size.height*0.30);
[optionDescription setAnchorPoint:CGPointMake(0.5f, 0.5f)];
// Add the title
CCLabelBMFont *optionTitle = [CCLabelBMFont labelWithString:title fntFile:#"titleFont.fnt" width:stripWidth alignment:kCCTextAlignmentCenter];
[self addChild:optionTitle];
optionTitle.position = CGPointMake(slot * positionIncrement, size.height*0.90);
[optionTitle setAnchorPoint:CGPointMake(0.5f, 0.5f)];
}
[self scheduleUpdate]; //Update only prints counter to see how many frames it lasts
}
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
-(void) update:(ccTime)delta
{
CCLOG(#"counter: %d",counter);
counter++;
}
-(void) onExit
{
[optionsArray release];
}
#end
`
i don't see any [super onEnter]; or [super init] or anything like that ...that's your problem
First add [super onEnter] on the first line in onEnter.
Second add an init method like this:
-(id)init{
if (self=[super init]){}
}
Third add [super onExit] at the end of your onExit method
I figured it out, it wasn't anything to do with the code I posted, so sorry about that. It was just a stupid release call to a non-retained array. Retained the array previously and it worked fine. Sorry about crying wolf

Using SneakyJoystick in a layer to control a game character in a different layer

What I'm trying to achieve is the ability to use a Cocos2d SneakyInput SneakyJoystick to control the movement of my LHSprite (created using level helper) Character/Player in my box2d/cocos2d game.
I can get it to work, however, the sneakyJoystick joystick is in the same layer that my gameplay is in, and seen as though my game screen 'follows' the character - the joystick actually moves off the screen when the camera/screen is moved.
I have tried setting the joystick up in a separate layer ('MyUILayer'), and using it to control my character in my 'GameLayer'.
Here is how i've tried doing this:
in 'MyUILayer' i have the code to set up the following sneakyJoystick components:
#interface MyUILayer : CCLayer {
SneakyJoystick *leftJoystick;
SneakyButton * jumpButton;
SneakyButton * attackButton;
}
#property (readonly) SneakyButton *jumpButton;
#property (readonly) SneakyButton *attackButton;
#property (readonly) SneakyJoystick *leftJoystick;
Now, in 'GameLayer I tried to access the value created by the sneakyJoystick called 'leftJoystick' in 'MyUILayer'.
in the declaration file (GameLayer.h):
#import "MyUILayer.h"
#interface GameLayer : CCLayer {
//.............
LHSprite *character;
b2Body *characterBody;
SneakyJoystick *characterJoystick;
SneakyButton *jumpButton;
SneakyButton *attackButton;
//.............
}
in GameLayer.mm:
//in my INIT method
{
MyUILayer *UILAYER = [[MyUILayer alloc]init];
characterJoystick = UILAYER.leftJoystick;
[self scheduleUpdate];
// Define what 'character' is and what 'characterBody' is ('character is a LHSprite, and 'characterBody' is a b2Body)
}
//in my tick method
{
b2Vec2 force;
force.Set(characterJoystick.velocity.x * 10.0f, 0.0f);
characterBody->ApplyForce(force, characterBody->GetWorldCenter());
}
I really can't understand why 'characterBody', in 'GameLayer' won't move based on the value of 'leftJoystick', in 'MyUILayer'.
Sorry if its a bit long winded! - I have also uploaded my project file, so you can take a look at the project itself: https://dl.dropbox.com/u/2578642/ZOMPLETED%202.zip
Huge thanks to anyone who can help!
The problem lies in the way you are associating the MyUILayer with the GameLayer. On myscene.mm, you are creating a MyUILayer and a GameLayer, and adding them both to the scene. This is ok. But then, you make a NEW MyUILayer on GameLayer, and associate that joystick. You should associate the joystick in the MyScene.mm by using properties like so:
In MyScene.mm
// Control Layer
MyUILayer * controlLayer = [MyUILayer node];
[self addChild:controlLayer z:2 tag:2];
// Gameplay Layer
GameLayer *gameplayLayer = [GameLayer node];
gameplayLayer.attackButton = controlLayer.attackButton;
gameplayLayer.leftJoystick = controlLayer.leftJoystick;
gameplayLayer.jumpButton = controlLayer.jumpButton;
[self addChild:gameplayLayer z:1 tag:1];
In GameLayer.h add
#property (nonatomic, retain) SneakyButton *jumpButton;
#property (nonatomic, retain) SneakyButton *attackButton;
#property (nonatomic, retain) SneakyJoystick *leftJoystick;
In GameLayer.mm add
#synthesize jumpButton = jumpButton;
#synthesize attackButton = attackButton;
#synthesize leftJoystick = characterJoystick;
In GameLayer.mm, remove the UILAYER code in the init method
- (id)init
{
self = [super init];
if (self) {
//instalize physics
[self initPhysics];
[self lvlHelper];
[self characterLoad];
[self runAction:[CCFollow actionWithTarget:character worldBoundary:CGRectMake(0, -100, 870, 420)]];
[self scheduleUpdate];
}
return self;
}

Cocos2d: Preloading animation causes a crash

I am trying to preload an animation in the init method of my layer. I then call the animation if the screen is touched. The app crashes with no error message as soon as I touch the screen and seems it is to do with calling the preloaded animation. I would like to do it this way as it seems expensive to create the animation every time the screen is touched - which does seems to work though. Any tips greatly appreciated.
Sample Code:
In my header:
#interface Test : CCLayer {
NSMutableArray *wake;
CCSprite* ani;
CCAnimate *animate;
}
#end
In my implementation:
-(id) init {
if( (self=[super init])) {
// enable touches
self.isTouchEnabled = YES;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"ani.plist" texture:[[CCTexture2D alloc] initWithImage:[UIImage imageNamed:#"ani.png"]]];
ani = [CCSprite spriteWithSpriteFrameName:#"ani1.png"]; //comes from .plist file
ani.anchorPoint=ccp(0,0);
ani.position = ccp(700,65);
[self addChild:ani z:30];
wake = [NSMutableArray array];
for(int i = 1; i <= 4; i++) {
[wake addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"ani%d.png",i]]];
}
animate = [CCAnimate actionWithAnimation:[CCAnimation animationWithFrames:wake delay:1.0f] restoreOriginalFrame:FALSE];
}
return self;
}
Handling the touch:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// run the animation
[ani runAction:animate];
}
Animations in Cocos2d are not designed to be reused. You need to create a new one every time.
Problem solved by creating properties for the array and animation on the class using nonatomic,retain.
You only need to retain the animation but the array can be local.
self.myAnimation = [[CCAnimation animationWithFrames:myAniFramesArray delay:0.1f] retain];
Remember to make the property nonatomic, retain as stated by Chev and to release any objects you retain in the appropriate dealloc method.