Cocos2D random sprite movement - iphone

As a very beginner in Cocos2D i´m trying to make an iPhone game where some cows move randomly around the screen. I used the code for moving the sprites from here: highoncoding.com/.../. I´m adding the sprites in the init method wia an addAnimal method:
-(void) addAnimal {
animal = [CCSprite spriteWithFile:#"cow.png"];
animal.position = [self generateRandomCoordinates];
[self addChild:animal];
[self startAnimation:animal];
}
My problem:
When i add more than one cow to my game, they move from that random spawn position to another random position and then the first cow stops and the other cow goes on correctly. The startAnimation command in the finishedMoving method goes always to the last sprite. That means i need better control over my sprites but how to da that right?

You can try to implement animal class, that will contain your sprite and incapsulate random movement. Smth like
#implementation Cow
- (id) init
{
self = [super init];
if( self != nil )
{
CCSprite* cowSprite = [CCSprite spriteWithFile:#"cow.png"];
[self addChild: cowSprite];
}
return self;
}
- (void) onEnter
{
[super onEnter];
[self makeRandomMovement];
}
- (void) makeRandomMovement
{
id randomMoveAction = // create your random move action here
id moveEndCallback = [CCCallFunc actionWithTarget: self selector: #selector(makeRandomMovement)];
id sequence = [CCSequence actionOne: randomMoveAction two: moveEndCallback];
[self runAction: sequence];
}
#end
In such way after ending random movement part, method makeRandomMovement will be called again to generate and start new random movement part.
then remake your addAnimal method to smth like
- (void) addAnimal
{
Cow* newCow = [Cow node];
[newCow setPosition: [self generateRandomPosition]];
[self addChild: newCow];
}

Related

Cocos2d - Character seems to be stutter stepping

I'm making a top down tile based game (think old Pokemon and Zelda games on GameBoy). I'm having problems with the character moving smoothly. I think the problem is the delay between finishing an action and starting a new one.
Here's what the code looks like:
-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchCoor = [self coordinateForTouch:touch];
// If the character was touched, open their dialogue
if (CGPointEqualToPoint(touchCoor, ebplayer.coordinate)) {
[[CCDirector sharedDirector] replaceScene:
[CCTransitionMoveInR transitionWithDuration:1.0 scene:[HelloWorldLayer scene]]];
}
else // otherwise, move the character
{
activeTouch = [self directionForPoint:[touch locationInView:[touch view]]];
[self movePlayer:nil inDirection:activeTouch];
}
return YES;
}
// given in screen dimension points
// is the base movement function for all movements
- (void)movePlayer:(NSString*)pid toPosition:(CGPoint)position{
CGPoint playerCoordinate = [self coordinateForPositionPoint:position];
// if we're not already moving, and we can move, then move
if(!isAnimating && [self coordinateIsOnMap:playerCoordinate] && [self isPassable:playerCoordinate]){
id doneAction = [CCCallFuncN actionWithTarget:self selector:#selector(finishedAnimating)];
id moveAction = [CCMoveTo actionWithDuration:WALK_DURATION position:position];
id animAction = [CCAnimate actionWithAnimation: [ebplayer animateDirection:activeTouch withDuration:WALK_DURATION]];
id walkAndMove = [CCSpawn actionOne:moveAction two:animAction];
id action = [CCSequence actions: walkAndMove, doneAction, nil];
isAnimating = YES;
[player runAction:action];
ebplayer.coordinate = playerCoordinate;
[self setViewpointCenter:position Animated:YES];
}
// if it's not passable, just run the animation
if(!isAnimating){
id doneAction = [CCCallFuncN actionWithTarget:self selector:#selector(finishedAnimating)];
id animAction = [CCAnimate actionWithAnimation: [ebplayer animateDirection:activeTouch withDuration:WALK_DURATION]];
id action = [CCSequence actions: animAction, doneAction, nil];
isAnimating = YES;
[player runAction: action];
}
}
Then when that action is finished, try and start it up again:
(void)finishedAnimating{
isAnimating = NO;
[self movePlayer:nil inDirection:activeTouch];
}
You will always end up with a 1-frame delay when sequencing multiple CCMove* actions.
What happens is the following:
frame 0-100: move action runs, sprite is moving
frame 101: move action ended, CCCallFunc runs
frame 102: new move action begins
This one-frame delay is one of the main problems of sequencing move actions, and the reason why I wouldn't recommend using move actions for gameplay purposes.
The alternative is to move objects manually in a scheduled update method by modifying their position. You can use the CCMove* action code as basis.

Release all object after replacing the scene

I have two class A and B. I create objects of the class B in the A:
B *objectB = [B classInitWithParamiters:paramiters];
[self addChile:objecTB z:1 tag:varForTag];
varForTag++;
I call this code many times.
This is B.h file:
#interface Chicken : CCSprite <CCTargetedTouchDelegate> {
CCsprite *spriteB;
}
+ (id) classInitWithParamiters :(int) paramiters;
This is B.m file:
+ (id) classInitWithParamiters :(int) paramiters
{
return [[[self alloc] init] autorelease];
}
- (id) init
{
if( (self = [super init]) ) {
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
spriteB = [[CCSprite alloc] initWithFile:#"image.png"];
spriteB.position = ccp(160, 240);
[self addChild:spriteB];
}
return self;
}
- (void) update :(ccTime)dt
{
NSLog(#"This is a Class B");
}
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint location = [[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
if(CGrectContainsPoint([spriteB boundingbox], location))
NSLog(#"touch moved in the class B");
}
My problem is: When I replace A scene with scene of the class C, the method update of the class B stops the log, but if I touch the middle of the screen and move the finger it logs "touch moved in the class B".
What I'm doing wrong? These objects of the class B should not released automatically after replacing the scene. Class B is subclass of the CCSprite and A - CCLayer;
Class B is obviously still running. Which means it leaked and never got shut down by cocos2d. Hence it's still receiving touches and may even be running scheduled updates and actions.
My guess is you introduced a retain cycle. Typical cause is a node with a retaining reference to a different node that is not one of its children or grandchildren. For example, retaining the scene node in a child node can cause a retain cycle, if the scene node is not released/nil'ed in the cleanup method (dealloc will not be called if the scene is still retained, so cleanup is the only place to clean up such potential retain cycle references).
Your problem is in Class B:
+ (id) classInitWithParamiters :(int) paramiters
{
[[[self alloc] init] autorelease];
}
You must have to return object.
+ (id) classInitWithParamiters :(int) paramiters
{
return [[[self alloc] init] autorelease];
}

Move Image in CocoS2d

I am developing game in Cocos2d. In it background of scene moving constantly with x - axis.
I want to understand how can i do it because i am new in cocos2d.
thanks in advance
Take a look to my code - here are the sources. Find the "moving" technique in HelloWorldLayer.m. I have taken a 360 panorama picture and put it to move continuously from right to left, in fullscreen.
In your YourClass.h file:
#import "cocos2d.h"
#interface YourClass : CCLayer
+(CCScene *) scene;
#end
In your YourClass.m file:
#import "YourClass.h"
#implementation YourClass
CCSprite *panorama;
CCSprite *appendix;
//_____________________________________________________________________________________
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
YourClass *layer = [YourClass node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
//_____________________________________________________________________________________
// on "init" you need to initialize your instance
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
panorama = [CCSprite spriteWithFile: #"panorama.png"];
panorama.position = ccp( 1709/2 , 320/2 );
[self addChild:panorama];
appendix = [CCSprite spriteWithFile: #"appendix.png"];
appendix.position = ccp( 1709+480/2-1, 320/2 );
[self addChild:appendix];
// schedule a repeating callback on every frame
[self schedule:#selector(nextFrame:)];
}
return self;
}
//_____________________________________________________________________________________
- (void) nextFrame:(ccTime)dt {
panorama.position = ccp(panorama.position.x - 100 * dt, panorama.position.y);
appendix.position = ccp(appendix.position.x - 100 * dt, appendix.position.y);
if (panorama.position.x < -1709/2) {
panorama.position = ccp( 1709/2 , panorama.position.y );
appendix.position = ccp( 1709+480/2-1, appendix.position.y );
}
}
// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
//_____________________________________________________________________________________
#end
Play a little with this code and you will get what you need.

Removing animation sprite frames from layer?

Let's say I have a character in a game and its class is like this.
#interface Player
{
CCSprite* stand;
CCAnimation* run;
}
-(void) playRunAction
{
// Create CCAnimate* object from CCAnimation object (run)
[self runAction:runAniate];
}
-(void) playStandAction
{
stand.visible = YES;
[self stopAllActions];
}
The player has ability to stand or run.
But one problem is, after playStandAction is called, stand animation is visible and running animation stopped, but one frame of running animation still there!
( Now you see 'stand sprite' AND 'one of running animation frame' together. )
How can I make running animation not visible?
P.s Can anyone throw me a better way of managing animation in one character? This is totally disaster as animations added.
-(void) playStandAction
{
//Make the animation object.visible = NO; here
stand.visible = YES;
[self stopAllActions];
}
and in
-(void) playRunAction
{
// Create CCAnimate* object from CCAnimation object (run)
//Make the animation object.visible = YES; here
stand.visible = NO;
[self runAction:runAniate];
}
Use method with parameter restoreOriginalFrame and pass it yes
I don't know which method you are calling for creating CCAnimate object...
Like this:
[CCAnimate actionWithAnimation:animation restoreOriginalFrame:YES]];
And don't call runAction on layer. I would prefer you to runAction on sprite itself...
You don't need to hide and show 2 different objects...
Hope this helps. :)

NSMutableArray and batchNode problems

I'm making a little game, here is some example code of whats going on:
-(id) init
{
self.arrowProjectileArray = [[[NSMutableArray alloc] init] autorelease];
self.batchNode = [CCSpriteBatchNode batchNodeWithTexture:[[CCTextureCache sharedTextureCache] addImage:#"arrow.png"]];
[self addChild:_batchNode z:2];
for (CCSprite *projectile in _arrowProjectileArray) {
[_batchNode removeChild:projectile cleanup:YES];
}
[_arrowProjectileArray removeAllObjects];
self.nextProjectile = nil;
}
}
-(void) callEveryFrame:(ccTime)dt{
for (int i = 0; i < [_arrowProjectileArray count];i++) {
CCSprite *cursprite = [_arrowProjectileArray objectAtIndex:i];
if (cursprite.tag == 1) {
float x = theSpot.x+10;
float y = theSpot.y+10;
cursprite.position = ccp(x, y);
}
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[_batchNode addChild:_nextProjectile z:1 tag:1];
[_arrowProjectileArray addObject: _nextProjectile];
[self spriteMoveFinished];
}
-(void) dealloc
{
self.arrowProjectileArray = nil;
self.nextProjectile = nil;
[super dealloc];
}
The only code that I included was code that is relevant to the arrow's projection.
The arrow shoots fine, the problem is every time I shoot the stupid thing, I think it shoots a new arrow, but puts multiple arrows onto of that 1 arrow and makes it look like a fat ugly arrow pixel thing. What am I doing wrong? I'm not too familiar with NSMutableArray, but I'm currently stuck.
In init method, you create a new NSMutableArray instance and assign it to self.arrowProjectileArray, then you traverse the arrowProjectileArray in the following lines using a for loop. If addChild: method does not add anything to arrowProjectileArray, then your code has a logic mistake, because what you do by traversing arrowProjectileArray is traversing an empty array, which means you do nothing in that code.
You should double-check what you intend to do and what your code is doing actually.
I solved my own problem by doing a little bit of research, I also got rid of the batch node.