body node in Cocos2dx - cocos2d-iphone

Can you please convert this cocos2d code to cocos2dx?
shiprightnode = [[Shipright alloc] initWithBody:shipright game:game_];
[game_ addBodyNode:shiprightnode z:1];
[shiprightnode release];

shiprightnode = new Shipright();
shiprightnode->init(p_game_);
game_->addBodyNode(shiprightnode);
shiprightnode->release();

auto shiprightnode = Shipright::createWithGame(game_);
game_->addBodyNode(shiprightnode,1);
//no need to release shiprightnode because it create autorelease object.

Related

Set the physicsNode.debugDraw = TRUE crash

I am studying the SpriteBuilder to make the example Peeved Penguins,but on the Joint step I set the _physicsNode.debugDraw = TRUE,it cash when I run my game,I don't know how to fix it.
If this is in obj-c, then you enable the debugDraw when you define the physicsWorld properties like this:
physicsWorld = [CCPhysicsNode node];
physicsWorld.gravity = ccp(0,0);
physicsWorld.debugDraw = NO;
physicsWorld.collisionDelegate = self;
[self addChild:physicsWorld];
This is assuming that you have created a physics world

Choose Random Sprite Cocos2d 3.0

I have 4 sprites, every time the game starts it would randomly choose 1 from the 4 sprites to be the main sprite.
How can I do this?
I know I would need to use arc4random
The easiest way would be -- First name you image (.png) files with some numbers for e.g. sprite1.png, sprite2.png ....
int rndSprtNum = (arc4random() % 4) + 1;
CCSPrite *mainSprite = [CCSprite spriteWithFile:[NSString StringWithFormat:#"sprite%d.png",rndSprtNum]];
mainSprite.position = ccp(x,y);
[self addChild:mainSprite];
This way you no need to take a mutable array etc. Hope this helps.
First of all you are add all sprite in NSMutableArray as below code.
Allocate Array
NSMutableArray *AryT = [[NSMutableArray alloc]init];
Differnt sprite
CCSprite *torpedoOne1 = [CCSprite spriteWithFile:#"A#2x.png"];
CCSprite *torpedoOne2 = [CCSprite spriteWithFile:#"B#2x.png"];
CCSprite *torpedoOne3 = [CCSprite spriteWithFile:#"C#2x.png"];
Add All This sprite in define Array
[AryT addObject:torpedoOne1];
[AryT addObject:torpedoOne2];
[AryT addObject:torpedoOne3];
Take Random number from array
int RandomIndex = arc4random_uniform(AryT.count);
Randomaly sprite
[self addchild:[AryT objectAtIndesx:RandomIndex]];
Here's another approach, in case you don't want to rename your files you can simply put their names in an array:
NSString *names[4] = {#"RedSprite.png", #"GreenSprite.png", #"YellowSprite.png", #"PurpleSprite.png"};
CCSprite *sprite = [CCSprite spriteWithFile:names[arc4random_uniform(4)]];
[self addChild:sprite];

CCAnimate error after declaring NSMutable array and animation in init

I'm having an error that i don't know how to solve, I've declared an NSMutablearray in my init, filled it with two sprites to animate, and declare the CCAnimation;
animParpadeoNina = [[NSMutableArray alloc]init];
[animParpadeoNina addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"ninaCamina002b.png"]];
[animParpadeoNina addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:#"ninaCamina002.png"]];
for (id obj in animParpadeoNina) {
NSLog(#"%#",obj);
}
paralanina = [CCAnimation animationWithSpriteFrames:animParpadeoNina delay:.2];
Now, if i call the action just after declare it all, the animation works, but if i try to run the CCAnimation in any other function, I get " EXC_BAD_ACCESS (code = 1, address = 0xb0ab1de8)
If i fill the array AND declare the CCAnimation inside the function, then it works. The thing is that i want to run this action several times, and it doesn't feels the right way to fill and declare once and once again the same code...
What am i doing wrong? Any suggestion to solve this?
Thanks in advance.
animParpadeoNina = [NSMutableArray alloc];
This should be:
animParpadeoNina = [[NSMutableArray alloc] init];

How To Have The Same Sprite In Multiple Locations Cocos2d

How To Have The Same Sprite In Multiple Locations Cocos2d Please Help
I have searched all over and cannot find answer
Just create multiple Sprites (CCSprite instances). They can all use the same texture (bitmap-file).
CCSprite * mySprite1;
CCSprite * mySprite2;
CCSprite * mySprite3;
// create several sprites from the same bitmap file
mySprite1 = [CCSprite spriteWithFile:#"spriteBitmap.png"];
mySprite2 = [CCSprite spriteWithFile:#"spriteBitmap.png"];
mySprite3 = [CCSprite spriteWithFile:#"spriteBitmap.png"];
mySprite1.position = ccp(100, 100);
mySprite2.position = ccp(200, 200);
mySprite3.position = ccp(300, 300);
You can not add the same CCSprite as a child to multiple CCNodes but you can make Cocos2D render the same CCSprite multiple times.
To achieve this you need to create a subclass of CCNode that will store the reference to your CCSprite and draw it in its -draw method applying required transformations.
For example
-(void)draw
{
[super draw];
CGPoint initialPosition = [_node position];
float initialScale = [_node scale];
[_node setScale:self.scale];
[_node setPosition:self.position];
[_node visit];
[_node setPosition:initialPosition];
[_node setScale:initialScale];
}
You may have to to use glScissor if you need picture-in-picture appearance.
Then you just need to addChild an instance of this class for every time you want an additional copy of your original CCSprite rendered.
Put a method on a for loop.
Inside the method create the CCSprite and modify it.
This is best suited for static sprites, since I don't know how you would access these outside of the method.

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.