// HelloWorldLayer.m
#import "HelloWorldLayer.h"
#import "TeddyBear.h"
#define kNumTeddyBears 15
// HelloWorldLayer implementation
@implementation HelloWorldLayer
+ (CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
HelloWorldLayer *layer = [HelloWorldLayer 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
{
if((self = [super init]))
{
/* Touch abilitato. */
self.isTouchEnabled = YES;
/* Abilita il metodo update. */
[self scheduleUpdate];
/* Inizializzazione della coda di teddyBear. */
_teddyBearsQueue = [[CCArray alloc] initWithCapacity:kNumTeddyBears];
/* Animazione teddyBear. */
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimTeddyBear.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimTeddyBear.png"];
[self addChild:spriteSheet];
walkAnimFrames = [[NSMutableArray array] retain];
for(int i = 1; i <= 4; ++i)
{
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"teddybear%d.png", i]]];
}
/* Array di teddyBear. */
_teddyBears = [[CCArray alloc] initWithCapacity:kNumTeddyBears];
for(int i = 0; i < kNumTeddyBears; i++)
{
TeddyBear *tmpTeddyBear = [TeddyBear new];
[tmpTeddyBear initTeddyBear:1 withAnimFrames:walkAnimFrames withSpriteFrameName:@"teddybear1.png" withDelay:0.09f];
[spriteSheet addChild:[tmpTeddyBear teddyBearSprite]];
[_teddyBears addObject:tmpTeddyBear];
/* Aggiunge alla coda. */
NSNumber *touchedTeddyBear = [NSNumber numberWithInt:i];
[_teddyBearsQueue addObject:touchedTeddyBear];
}
}
return self;
}
- (float) randomValueBetween:(float) low andValue:(float) high
{
return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
}
- (void) update:(ccTime) dt
{
int k = 0;
/* Scorre _teddyBears, se un oggetto è invisibile lo mette nella coda. */
for(TeddyBear *tmpTeddyBear in _teddyBears)
{
if([[tmpTeddyBear teddyBearSprite] visible] == NO)
{
NSNumber *touchedTeddyBear = [NSNumber numberWithInt:k];
[_teddyBearsQueue addObject:touchedTeddyBear];
[tmpTeddyBear teddyBearSprite].visible = YES;
}
k++;
}
if([_teddyBearsQueue count] > 0)
{
int nextTeddyBear = [[_teddyBearsQueue objectAtIndex:0] integerValue];
[_teddyBearsQueue removeObjectAtIndex:0];
[[_teddyBears objectAtIndex:nextTeddyBear] startAnimation];
}
}
- (void) registerWithTouchDispatcher
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
-(BOOL) ccTouchBegan:(UITouch *) touch withEvent:(UIEvent *) event {
return YES;
}
-(void) ccTouchEnded:(UITouch *) touch withEvent:(UIEvent *) event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for(TeddyBear *tmpTeddyBear in _teddyBears)
{
if([tmpTeddyBear teddyBearSprite].visible == NO)
continue;
if(CGRectContainsPoint([[tmpTeddyBear teddyBearSprite] boundingBox], location))
{
//[[tmpTeddyBear teddyBearSprite] setPosition:ccp(200, 200)];
[tmpTeddyBear setInvisible];
break;
}
}
}
// TeddyBear.m
#import "TeddyBear.h"
@implementation TeddyBear
@synthesize teddyBearSprite = _teddyBearSprite;
@synthesize walkAction = _walkAction;
@synthesize moveAction = _moveAction;
@synthesize walkAnim = _walkAnim;
- (float)randomValueBetween:(float)low andValue:(float)high {
return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
}
- (void) initTeddyBear:(int) l withAnimFrames:(NSMutableArray*) animFrames withSpriteFrameName:(NSString*) spriteFrameName withDelay:(float) d
{
/*CGSize winSize = [CCDirector sharedDirector].winSize;
int posX, posY;
float randDuration;*/
//NSLog(@"winSize.height = %f\n", winSize.height);
int posX, posY;
life = l;
_teddyBearSprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
posX = -_teddyBearSprite.contentSize.width;
posY = -_teddyBearSprite.contentSize.height;
_teddyBearSprite.position = ccp(posX, posY);
//[self startAnimation];
_walkAnim = [CCAnimation animationWithFrames:animFrames delay:d];
_walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:_walkAnim restoreOriginalFrame:NO]];
[_teddyBearSprite runAction:_walkAction];
}
- (void) startAnimation
{
CGSize winSize = [CCDirector sharedDirector].winSize;
int posX, posY;
float randDuration;
NSLog(@"Start animation called");
posX = -_teddyBearSprite.contentSize.width;
posY = [self randomValueBetween:100.0 andValue:(winSize.height - _teddyBearSprite.contentSize.height/2)];
_teddyBearSprite.position = ccp(posX, posY);
NSLog(@"Posizionato in (%d, %d)", posX, posY);
randDuration = [self randomValueBetween:10.0 andValue:10.0];
//[CCDelayTime actionWithDuration:3.0];
[_teddyBearSprite runAction:[CCSequence actions:[CCMoveTo actionWithDuration:randDuration position:ccp(winSize.width + _teddyBearSprite.contentSize.width, [self randomValueBetween:0.0 andValue:winSize.height])], [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible)], nil]]; /* Movimento. */
}
- (void) pauseAnimation
{
[_teddyBearSprite pauseSchedulerAndActions];
}
- (void) setInvisible
{
_teddyBearSprite.visible = NO;
}
@end