Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am currently doing the tutorials in the book: "learn iPhone and iPad cocos2d Game Development" by Steffen Itterheim and have been running into problems with the code for the DoodleDrop tutorial, chapter 4. I have tried to find the source of the problem but i am not able to do so.

Here is the code i am using:

#import "GameScene.h"


@implementation GameScene


-(void) spiderBelowScreen:(id)sender {
NSLog(@">>spiderBelowScreen<<");

// Make sure sender is actually of the right class
NSAssert([sender isKindOfClass:[CCSprite class]], @"sender is not a CCSprite!");
CCSprite* spider = (CCSprite*)sender;

// Move the spider back up outside the top of the screen
CGPoint pos = spider.position;
CGSize screenSize = [[CCDirector sharedDirector] winSize];
pos.y = screenSize.height + [spider texture].contentSize.height;
spider.position = pos;
}



-(void) runSpiderMoveSequence:(CCSprite*)spider {
NSLog(@">>runSpiderMoveSequence<<");

// Slowly increase the spider speed over time
numSpidersMoved++;

if (numSpidersMoved % 8 == 0 && spiderMoveDuration > 2.0f) {
    spiderMoveDuration -= 0.1f;
}

// This is the sequence which controls the spiders' movement
CGPoint belowScreenPosition = CGPointMake(spider.position.x, [spider texture].contentSize.height);
CCMoveTo *move = [CCMoveTo actionWithDuration:spiderMoveDuration position:belowScreenPosition];
CCCallFuncN *call = [CCCallFuncN actionWithTarget:self selector:@selector(spiderBelowScreen)];
CCSequence *sequence = [CCSequence actions:move, call, nil];
[spider runAction:sequence];
}


-(void) spidersUpdate:(ccTime)delta {
NSLog(@">>spidersUpdate<<");

// Try to find a spioder which isn't currently moving
for (int i = 0; i < 10; i++) {
    int randomSpiderIndex = CCRANDOM_0_1() * [spiders count];
    CCSprite *spider = [spiders objectAtIndex:randomSpiderIndex];

    // If the spider isn't moving it won't have any running actions
    if ([spider numberOfRunningActions] == 0) {
        // This is the squense wich controls the spiders' movement
        [self runSpiderMoveSequence:spider];

        // Only onbe spider should start moving at a time
        break;
    }
}
}


-(void) resetSpiders {
NSLog(@">>resetSpiders<<");

CGSize screenSize = [[CCDirector sharedDirector] winSize];

// Get any spider to get its image width
CCSprite *tempSpider = [spiders lastObject];
CGSize size = [tempSpider texture].contentSize;

int numSpiders = [spiders count];

for (int i = 0; i < numSpiders; i++) {

    // Put each spider at its designated position outside the screen
    CCSprite *spider = [spiders objectAtIndex:i];
    spider.position = CGPointMake(size.width * i + size.width * 0.5f, screenSize.height + size.height);
    [spider stopAllActions];
}

// Uschedule the selector just in case. If it isn't scheduled it wont do anything
[self unschedule:@selector(spidersUpdate:)];

// Schedule the spider update logic to run at the given interval
[self schedule:@selector(spidersUpdate:) interval:0.7f];
}


-(void) initSpiders {
NSLog(@">>initSpiders<<");

CGSize screenSize = [[CCDirector sharedDirector] winSize];

// using a temporary spider sprite is the easiest way to get the image's size
CCSprite *tempSpider = [CCSprite spriteWithFile:@"spider.png"];
float imageWidth = [tempSpider texture].contentSize.width;

// Use as many spiders as can fit next to each other over the whole screen width
int numSpiders = screenSize.width / imageWidth;

// Initialize the spider array using alloc
spiders = [[CCArray alloc] initWithCapacity:numSpiders];

for (int i = 0; i < numSpiders; i++) {
    CCSprite *spider = [CCSprite spriteWithFile:@"spider.png"];
    [self addChild:spider z:0 tag:2];

    // Also add the spider to the spider array
    [spiders addObject:spider];
}
// call the method to reposition all spiders
[self resetSpiders];

}



-(void) update:(ccTime)delta {
NSLog(@">>update:(ccTime)delta<<");

// Keep adding up the playerVelocity to the players's position
CGPoint pos = player.position;
pos.x   += playerVelocity.x;

// The player should also be stopped from going outside the screen
CGSize  screenSize  = [[CCDirector sharedDirector] winSize];
float   imageWidthHalved    = [player texture].contentSize.width * 0.5f;
float   leftBorderLimit     = imageWidthHalved;
float   rightBorderLimit    = screenSize.width - imageWidthHalved;

// Preventing the player sprite from moving outside the screen
if (pos.x < leftBorderLimit)
{
    pos.x   = leftBorderLimit;
    playerVelocity  = CGPointZero;
}
else if (pos.x > rightBorderLimit)
{
    pos.x   = rightBorderLimit;
    playerVelocity  = CGPointZero;
}

player.position = pos;


}


-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
NSLog(@">>accelerometer<<");

// controls how quickly velocity decelerates (lower = quicker to change direction)
float   deceleration    = 0.2f;

// determines how sensitive the accelerometer reacts (higher = more sensitivity)
float   sensitivity     = 6.0f;

// how fast the velocity can be at most
float   maxVelocity     = 550;

// adjust velocity based on current accelerometer acceleration
playerVelocity.x    = playerVelocity.x * deceleration + acceleration.x * sensitivity;

// we must limit the maximum velocity of the player sprite, in both directions
if (playerVelocity.x > maxVelocity)
{
    playerVelocity.x    = maxVelocity;
}
else if (playerVelocity.x < -maxVelocity)
{
    playerVelocity.x    = -maxVelocity;
}
}

+(id)scene {
NSLog(@">>scene<<");
CCScene *scene = [CCScene node];
CCLayer *layer = [GameScene node];
[scene addChild:layer];
return scene;
}

-(id)init {
NSLog(@">>init<<");
if ((self = [super init])) {
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

    self.isAccelerometerEnabled = YES;

    player = [CCSprite spriteWithFile:@"alien.png"];
    [self addChild:player z:0 tag:1];

    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    float imageHeight = [player texture].contentSize.height;
    player.position = CGPointMake(screenSize.width / 2, imageHeight / 2);

    // scheduling the update method in order to adjust the player's speed every frame
    [self scheduleUpdate];
    [self initSpiders];

}
return self;
}

-(void) deallaoc {
NSLog(@">>dealloc<<");
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);

// The spiders array must be released, it was created using [CCArray alloc]
[spiders release];
spiders = nil;

// Never forget to call [super dealloc]
[super dealloc];
}

@end

The result i get (edited), including the NSLog i have included, is:

2011-08-13 15:18:38.401 DoodleDrop[1751:707] >>update:(ccTime)delta<<
2011-08-13 15:18:38.404 DoodleDrop[1751:707] -[GameScene spiderBelowScreen]: unrecognized selector sent to instance 0x257f50
2011-08-13 15:18:38.407 DoodleDrop[1751:707] >>accelerometer<<
2011-08-13 15:18:38.417 DoodleDrop[1751:707] >>update:(ccTime)delta<<
2011-08-13 15:18:38.419 DoodleDrop[1751:707] -[GameScene spiderBelowScreen]: unrecognized selector sent to instance 0x257f50

I wonder if there is someone nice that can help me here?

share|improve this question
    
You forgot a colon in the selector's name. Basic programming questions should go to Stack Overflow. –  user744 Aug 13 '11 at 14:28
    
sorry for that Joe, as it is about Cocos2D i did put it here. –  PeterK Aug 13 '11 at 16:07
add comment

1 Answer

up vote 4 down vote accepted

I think you need to use @selector(spiderBelowScreen:). Note the :.

share|improve this answer
    
(I checked my copy of the book, and it has the colon.) –  user744 Aug 13 '11 at 19:19
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.