Iphone Cocos2D - SpriteSheet Cocos2D (so confused...) - iphone

I have gotten 0 response from the Cocos2D board so thought Id try here.
Ok. Im ripping out my hair over this. I did a complete re-haul of my code last night in hopes of fixing some iphone4 framerate issues. I created sprites in advance instead of doing some of the creation during the update methods. I only add them to the spritesheet now in the update method as needed.
However, this really didnt fix the issue. So I changed one thing out of curiosity. My main sprite sheet is 2048x2048 and contains 24 frames and one static frame. I used to pull my static image from the frames i generated using spriteWithSpriteFrameName. Instead I just decided to make another image file for it and create it from that. Framerate issue solved mostly...I dont understand why. I thought the texture was already in memory to begin with? Is anything wrong with my code?
circleSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"spritesheetbase.png"];
//Load Note Animations
for (int i = 0; i < 25; i++)
{
int x = i % 5;
int y = i / 5;
CCSpriteFrame* frame = [[CCSpriteFrame alloc] initWithTexture:circleSpriteSheet.textureAtlas.texture rect:CGRectMake(x * 200, y * 200, 200, 200)];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrame:frame name:[NSString stringWithFormat:#"noteFrame%d.png", i]];
[frame release];
}
CCSprite *s = [CCSprite spriteWithFile:#"staticRing.png"];
//CCSprite *s = [CCSprite spriteWithSpriteFrameName:#"noteFrame0.png"]; <--- Old way I used to create it
If someone could help I would be in your debt! I am so confused.

I have found performance issues when building/run in debug mode. Try building/run in release mode and see if that happens to fix your frame-rate issues. This resolved a similar frame-rate issue under this post:
water effect in cocos2d
I hope it happens to solve it for you too!

Related

XCode Spritekit, collision with non-transparent sections of an png

I have the same problem like here: SKSpriteKit, detect non-transparency parts
My post is deleted from there. I don't know why, in other forums, THIS is a double post!!!
But ok, if you like that. Poor danny who still have not the answer!
Ok well, here comes the question again:
There are two png files with transparent areas. Like in the post of danny huang.
If they collide the "-(void)didBeginContact:(SKPhysicsContact *)contact" method fire.
But what we looking for is that they just collied with the non-transparent areas of the image.
Here is a small code snipped:
...
SKTexture *nyanTexture1 = [SKTexture textureWithImageNamed:#"test.png"];
nyanTexture1.filteringMode = SKTextureFilteringNearest;
nyan = [SKSpriteNode spriteNodeWithTexture:nyanTexture1];
nyan.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:nyan.size.height / 2];
nyan.physicsBody.dynamic = YES;
nyan.physicsBody.allowsRotation = NO;
...
nyan.physicsBody.categoryBitMask = nyanKategorie;
nyan.physicsBody.collisionBitMask = worldKategorie | tunnelKategorie;
nyan.physicsBody.contactTestBitMask = worldKategorie | tunnelKategorie;
...
WasserFallTexture1 = [SKTexture textureWithImageNamed:#"Wasserstrahl1.png"];
SKSpriteNode* tunnel1 = [SKSpriteNode spriteNodeWithTexture:WasserFallTexture1];
tunnel1.position = CGPointMake( 0, y );
tunnel1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:tunnel1.size];
tunnel1.physicsBody.dynamic = NO;
tunnel1.physicsBody.categoryBitMask = tunnelKategorie;
tunnel1.physicsBody.contactTestBitMask = nyanKategorie;
...
[tunnel addChild:tunnel1];
I can do that without the nice engine of Spritekit. I implement it as well, but i want to know a way to do that with the SpriteKit engine.
I know that i can change the "SKPhysicsBody" type and areas, but i'm looking for a soloution witch can catch a collision between two png files non-transparent areas.
Thanks,
Sam
Sounds like actually seeing the physics bodies in action is what you need. There is a property on SKView called showsPhysics. Set that to YES and you can see the physics bodies of your sprites in action.

Small FPS with Background Image

I have a big problem with my app.
I am few steps before the end of my app and today I wanted to add a background image.
My normal FPS is 180 during black background and when I added my background for iphone 4 sized 960/640 my FPS changed to 20-30 so its impossible to play. And next thing what happens is, when my player and enemy has a collision they stop their actions, but only if there is my background. I am using SpaceManager so maybe its because of its shapes. So does anyone know how to add background in another way in SpaceManager to do not slow my FPS? Here is the way I try to add Background:
-(id) init
{
CCSprite *background = [CCSprite spriteWithFile:#"Background.png"];
background.anchorPoint = ccp(0, 0);
[self addChild:background z:-1];
}
the most likely cause for the drop in fps is because the picture is designed for the retina display, and since it is a large file it slows down the app. the device which you are using also has an effect on how fast the game works, so if its slow only on a 3gs or something like that just drop support for it.
So my Objects are statit mass, so I dont have there [sm addcollision] but I have there this code
-(void) KolizeHraceSProtiHracem
{
if (player.position.x - enemy.position.x < 40 )
{
[enemy stopAllActions];
[player stopAllActions];
[player runAction:[CCMoveTo actionWithDuration:0.2 position:ccp(player.position.x + 40, 10)]];
[enemy runAction:[CCMoveTo actionWithDuration:0.1 position:ccp(enemy.position.x - 10, 10)]];
}
I dont think, this code is bad, because if I dont have a background this code works but if i do this code stops all actions.

Animating a CCSprite on cocos2d?

Hey guys, I'm new to cocos2d and iPhone development, im currently trying to create a game like "PapiJump",
What I'm currently having problems with, is animating the character CCSprite in my game,
I have created 3 images of my character, character pointing right, left and middle (http://i53.tinypic.com/ngzoyh.png)
so when the character changes its direction (left or right), it wont "jump" directly to the other direction but will animate..
In my code im using the TouchesEnded method, in this method im setting a variable named "touchState",
so if the user touched the right side of the screen it will set "touchState" to 1, otherwise it sets it to 2 (means left).
then in the "init" method i created a "schedule" that runs another method every 0.5 sec that is named: "updateGame",
the "updateGame" method updates the player's position, it works like that:
player.position = ccp(player.position.x , player.position.y - playerGravity.y);
I've read Ray's tutorial on animating CCSprites but i have no idea how to achieve the result that i need..
Thanks in advance and sorry for my bad english!
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i)
{
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:#"spr_fillin_0%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.0333f];
_sprite = [CCSprite spriteWithSpriteFrameName:#"spr_fillin_01.png"];
[_sprite runAction:[CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]]];
[<CCLAYER OR BATCH NODE> addChild:_sprite];
Edit: Looks like you're talking about just flipping the image around. You can just refetch a sprite with
_sprite = [CCSprite spriteWithSpriteFrameName:#"<New Sprite File>"];
(Or cache them and switch)
Can you clarify your question? If you want to set a different sprite frame, you can use setDisplayFrame:
CCSpriteFrame *spriteFrame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:frameName];
[sprite setDisplayFrame:frame];
or if you have an animation:
[sprite runAction:[CCAnimate actionWithAnimation:runAnimation restoreOriginalFrame:NO]];
But I feel like the question is a little vague.
if i got your question properly i can suggest two ways to solve your problem:
first you could rotate your object to till it is upside down, and the continue your previous animations using FlipX(or FlipY) flag marks as true or you can add an animation wich animates only characters rotation and when ever it's finnished change the FLipX(or FlipY) status.

(cocos2d-iphone) Problem-Sprite rotates 180 degree to and fro

hi i have a problem with sprite during startup
The problem is that the sprite rotates 180 degree to and fro once before setting to its position.
It happens very fast.
I know it is some bug but couldnt find the problem.
Can anyone please help me.
The code are as follows
CCSprite *arrowSprite = [[CCSprite spriteWithFile:#"Launcher2.png"] retain];
CGSize size = [arrowSprite contentSize];
arrowSprite.anchorPoint = ccp(25/size.width,20/size.height);
NSLog(#"Value of anchorPoint x,y %f, %f",arrowSprite.anchorPoint.x,arrowSprite.anchorPoint.y);
arrowSprite.position = ccp(65, 80);
[self addChild:arrowSprite z:1];
I don't see anything in your code that would cause any rotation of the sprite. You can confirm this by reducing the posted code down to the bare bones by commenting out the line that sets the anchorPoint. If you're still getting the rotation, it's happening somewhere else in your application.

Cocos2d - Shooting Game Problem

everyone, I made a shooting game just like 1942(classic shooting game),but here's the question, FPS drops to 5~6 when the fighter shoot out a straight line of bullet,about 7~8 bullets in the screen,Actually, only 8 bullets of player will appear in games, when bullets fly out of the screen they would reset to the fighter's center and become invisible,all bullets are individual sprites, just like:
Sprites * bullet1 = [Sprite spriteWithFile:#"bullet.png"];
........
Sprites * bullet8 = [Sprite spriteWithFile:#"bullet.png"];
Is there any method can store a lots of bullets instead of defining them one by one??
Because enemies may fire too, I can imagine the FPS may get closer to zero.....
Can anyone help me??
you could do something like have a "bullet pool" in there, as you are using cocos, what I would do is to allocate lot's of bullets when I start the game. Check how many bullets do you need approximately, pre allocate those.
When an Entity in your game needs to fire a bullet, it just asks for a bullet to this pool, you give the properties to the "pre allocated" bullet, the bullet appears on the screen, and when it impacts/disappear you then return that bullet to your bullet pool.
if you need some code:
/*You pre-allocate your bullets.*/
for(int i = 0; i < MAX_BULLETS; i++)
{
Bullet *aBullet = [[Bullet alloc] init];
[bulletsArray addObject:aBullet];
[aBullet release];
}
//Then in game when you fire:
Bullet *aBullet = [PoolManager bulletWithSprite:myBulletSprite]; // Where myBulletSprite is PRE allocated and you don't allocate sprites in Game.
You can store the sprites in an array:
NSMutableArray * bulletsBuilder = [[NSArray alloc] init];
for(int i = 0; i < MAX_NUMBER_OF_SPRITES; ++i) {
[bullets addObject:[Sprite spriteWithFile:#"bullet.png"]];
}
NSArray * bullets = [NSArray arrayWithArray:bulletsBuilder];
[bulletsBuilder release];
And access them later using their identifier:
Sprite * spr = [bullets objectAtIndex:spriteIndex];
What's a problem? Use sprite batching! It's kind of special layer that allow you to speed up the rendering of the multiple sprites.
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:#"bullet.png"];
Unless you're not Texture Atlas you cannot use different sprites to render them.
Then load one sprite and add to the batch itself as many sprites as you need.
for(;i>10;){[batch addSprite:sprite];}
Add to self.
Core Animation layers are lightweight and should help your performance. You can load the image once and then load the bullets image into the contents of an array of CALayers. Something like this:
// bulletImage is an instance variable
bulletImage = [UIImage imageNamed:#"bullet.png"];
bulletLayers = [[NSMutableArray alloc] init];
for (i = 0; i < 9; ++i)
{
CALayer *bulletLayer = [CALayer layer];
[bulletLayer setBounds:[bulletImage bounds]];
[bulletLayer setPosition:gunBarrelOrigin];
[bulletLayer setContents:(id)[bulletImage CGImage]];
[bulletLayers addObject:bulletLayer];
}
// Use the array of layers.
// ...
You don't specify whether you are using views or layers in your Sprite class, but if you are loading the same image multiple times, you're definitely creating unnecessary overhead.