Is there a way to set the SneakyButton sprites from CCFrameCache?
Nothing shows up when I try :
[exitBase.defaultSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:#"exitUp.png"]];
It works fine with a [CCSprite spriteWithFile:#""] statement
Thanks
Just declare your batchnode normally, then setup SneakyButton with spriteWithSpriteFrameName:
SneakyButtonSkinnedBase *sneakyBut = [[[SneakyButtonSkinnedBase alloc] init] autorelease];
sneakyBut.position = ccp(444,280);
sneakyBut.defaultSprite = [CCSprite spriteWithSpriteFrameName:#"sneakyButtonOff.png"];
sneakyBut.activatedSprite = [CCSprite spriteWithSpriteFrameName:#"sneakyButtonOn.png"];
sneakyBut.button = [[SneakyButton alloc] initWithRect:CGRectMake(0, 0, 90, 90)];
sneakyButton = [sneakyBut.button retain];
[self addChild:sneakyBut];
Related
So I tried
self.walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim]];
But then I discovered that CCRepeatForever has been renamed and CCAnimate removed. How can I replace this?
All code: http://pastebin.com/VnrtiCwb
CCRepeatForever was replaced with CCActionRepeatForever and CCAnimate with CCActionAnimate
try this code
CCSprite *sprite = [CCSprite spriteWithImageNamed:#"sv_anim_1.png"]; // comes from your .plist file
sprite.position = ccp( [[CCDirector sharedDirector] viewSize].width/2, [[CCDirector sharedDirector] viewSize].height/2 );
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:#"scene1atlas.png"];
[batchNode addChild:sprite];
[self addChild:batchNode];
NSMutableArray *animFrames = [NSMutableArray array];
for(int i = 1; i < 5; i++)
{
//NSString *str=[NSString stringWithFormat:#"an1_anim%d.png",i];
CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"an1_anim%d.png",i]];
[animFrames addObject:frame];
}
animation = [CCAnimation animationWithSpriteFrames:animFrames delay:0.2f];
[sprite runAction:[CCActionRepeatForever actionWithAction:[CCActionAnimate actionWithAnimation:animation]]];
So I’ve been working on my cocos2d game for a little while now just using a static one frame sprite because I hadn’t gotten the graphics for the full spritesheet yet. Now I have the spritesheet, and am having trouble implementing it.
The way I currently make my “Player” (the sprite) is by having a separate player class, shown below
Player.h
#interface Player : CCSprite{
CCAction *_WalkAction;
}
#property (nonatomic, assign) CCAction *WalkAction;
#property (nonatomic, assign) CGPoint velocity;
#property (nonatomic, assign) CGPoint desiredPosition;
#property (nonatomic, assign) BOOL onGround;
#property (nonatomic, assign) BOOL forwardMarch;
#property (nonatomic, assign) BOOL mightAsWellJump;
#property (nonatomic, assign) BOOL isGoingLeft;
-(void)update:(ccTime)dt;
-(CGRect)collisionBoundingBox;
#end
And Player.m (I won’t show the full thing, because it’s pretty long, it just defines everything about the character)
-(id)initWithFile:(NSString *)filename {
if (self = [super initWithFile:filename]) {
self.velocity = ccp(0.0, 0.0);
}
return self;
}
//THIS IS ONLY A FRACTION OF MY UPDATE METHOD, THE REST OF IT IS ALL SETTING VELOCITY AND WHATNOT FOR MY PHYSICS ENGINE, BUT THIS IS THE ONLY RELEVANT PART
-(void)update:(ccTime)dt {
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
//[self runAction: _WalkAction];
}
}
Now in my GameLevelLayer.m I init the sprite like this (This is where we get filename from from the init in player.m):
//In my init
map = [[CCTMXTiledMap alloc] initWithTMXFile:#"level1.tmx"];
[self addChild:map];
player = [[Player alloc] initWithFile:#"banker1.png"];
player.position = ccp(100, 50);
[map addChild:player z:15];
So that all works perfectly fine. The problem comes when I try to turn that sprite into a sprite with a spritesheet. So I try to do this in player.m
-(id)initWithFile:(NSString *)filename {
if (self = [super initWithFile:filename]) {
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 = [CCSprite spriteWithSpriteFrameName:#"banker1.png"];
//HERE IS WHERE self.WalkAction IS DEFINED
self.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[spriteSheet addChild:self];
}
}
return self;
}
//And then run it in my update method
if (self.forwardMarch) {
self.velocity = ccpAdd(self.velocity, forwardStep);
[self runAction: _WalkAction];
}
Just for reference, self.forwardMarch is a bool that is set every time the user pushes the button to move the player, so whenever its true I move the player like this.
But when I try this I get the error
'NSInvalidArgumentException', reason: '-[CCSprite setWalkAction:]: unrecognized selector sent to instance 0x88fab50'
Any Help is appreciated.
I also posted this on the cocos2d forums if you like their code display system more (color coded and whatnot)
http://www.cocos2d-iphone.org/forums/topic/making-spritesheets-work/
EDIT:
Okay so I have made a little progress, but I'm still a bit stuck. I've defined the batchnodes and all the actions in the init of GameLevelLayer.m
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: #"BankerSpriteSheet_default.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"BankerSpriteSheet_default.png"];
[self addChild:spriteSheet];
[player 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];
player = [[Player alloc] initWithSpriteFrameName:#"banker1.png"];
player.WalkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
//[spriteSheet addChild:player];
}
//player = [[Player alloc] initWithFile:#"koalio_stand.png"];
player.position = ccp(100, 50);
player.scale = 0.2;
[map addChild:player z:15];
And then in my Player.m I run
[self runAction: self.WalkAction];
//I've also tried [self runAction: _WalkAction]; The result is the same
I used NSLogs to find out that the above section ([self runAction: self.WalkAction]; is being called but not finishing. This is where my crash happens, but there is NO ouput to the console as to why there is a crash.. literally just says (lldb)
The problem is one line above the actual error. You assign self with the result from [CCSprite spriteWith...] which replaces the existing self and replaces it with a CCSprite. Instead change the init line to self = [super initWithSpriteFrameName:..]
I'm very headache about this problem, I'm using these code for capturing a part of the screen
-(UIImage *) glToUIImage {
userphotoCount++;
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCLayerColor* whitePage = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];
whitePage.position = ccp(winSize.width/2, winSize.height/2);
CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:815 height:532];
[rtx begin];
[whitePage visit];
[[[CCDirector sharedDirector] runningScene] visit];
[rtx end];
return [rtx getUIImageFromBuffer];
}
I can get my UIImage by
image = [self glToUIImage];
Then I try to use this code to generate a sprite by the UIImage
-(void) ImageSprite{
image = [self glToUIImage];
userAns = [CCSprite spriteWithCGImage:image.CGImage key:[NSString stringWithFormat:#"photoCap%d.png", userphotoCount]];
userAns.position=ccp(780,410);
userAns.scale=0.6;
[self addChild:userAns z:20];
}
I can get image by these code at first time, but after I changed the screen's contents and use these code to generate new sprite(new UIImage) with the new contents, I'm failed.... the image doesn't change to new image...
How can I get the new image sprite when every time I run the "imageSprite" code?
You can get the new image sprite, but you must to change key
[CCSprite spriteWithCGImage:image.CGImage key:key];
I've got an end of level layer added to game, each level has its own scene. I want to be able to restart the current scene. Obviously the scene will change but the layer will remain the same. How is this done. I've tried-
CCScene *currentScene = [[CCDirector sharedDirector]runningScene];
[[CCDirector sharedDirector]replaceScene:currentScene];
Thanks
This does not work because you can't replace the same scene object with itself:
CCScene *currentScene = [[CCDirector sharedDirector]runningScene];
[[CCDirector sharedDirector]replaceScene:currentScene];
Instead you have to create a new instance of your scene, like so:
[[CCDirector sharedDirector] replaceScene:[YourSceneClass scene]];
If you don't know what the current scene class is, then this ought to work:
CCScene *currentScene = [CCDirector sharedDirector].runningScene;
CCScene *newScene = [[[currentScene class] alloc] init];
[[CCDirector sharedDirector] replaceScene:newScene];
Assuming you're using ARC as everyone should these days. Otherwise add an autorelease.
I ran into the same problem. I tried this
CCScene *currentScene = [CCDirector sharedDirector].runningScene;
CCScene *newScene = [[[currentScene class] alloc] init];
[[CCDirector sharedDirector] replaceScene:newScene];
and it gave me a blank screen.
The problem is this line
CCScene *newScene = [[[currentScene class] alloc] init];
[currentScene class] actually returns CCScene..
Hence
[CCScene alloc] init] gives us a blank screen.
The way how I got around this problem was by setting tag for each of my scene class.
For example:
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
scene.tag = 1;
// 'layer' is an autorelease object.
GameOneLayer * layer = [[[GameOneLayer alloc] init];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
Hope this helps.
SpriteSheets are one thing I just can't get my head around. I'm looking for a very clear and easy to follow way to learn about them. I have read and studied up on them but still have problems.
I don't know why my code isn't working.
-(void) addPlayer {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:#"walk1.png"];
sprite.position = ccp(winSize.width/2, winSize.height/2);
CCSpriteBatchNode *batchNode = [CCSpriteBatchNode batchNodeWithFile:#"player-walk.png"];
[batchNode addChild:sprite];
[self addChild:batchNode z:350];
NSMutableArray *frames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i) {
[frames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:#"walk%d.png", i]]];
}
CCAnimation *anim = [CCAnimation animation];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCRepeatForever *repeat = [CCRepeatForever actionWithAction:animate];
[self runAction:repeat];
}
Why is my player not animating?
Spritesheet is just a convenient way to store textures.
Your CCAnimation object has no frames. You gather an array of frames, but you never use it. Change your
CCAnimation *anim = [CCAnimation animation];
line to
CCAnimation *anim = [CCAnimation animationWithFrames:frames delay:0.2f];
If you want to animate a sprite, you have to run animating action on this sprite. Use [sprite runAction:repeat] instead of [self runAction:repeat];.