// TeddyBear.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface TeddyBear : NSObject
{
int life;
CCSprite *_teddyBearSprite;
CCAction *_walkAction;
CCAction *_moveAction;
CCAnimation *_walkAnim;
}
@property (nonatomic, retain) CCSprite *teddyBearSprite;
@property (nonatomic, retain) CCAction *walkAction;
@property (nonatomic, retain) CCAction *moveAction;
@property (nonatomic, retain) CCAnimation *walkAnim;
- (void) initTeddyBear: (int) l withAnimFrames:(NSMutableArray*) animFrames withSpriteFrameName:(NSString*) spriteFrameName withDelay:(float) d;
@end
// TeddyBear.m
#import "TeddyBear.h"
@implementation TeddyBear
@synthesize teddyBearSprite = _teddyBearSprite;
@synthesize walkAction = _walkAction;
@synthesize moveAction = _moveAction;
@synthesize walkAnim = _walkAnim;
- (void) initTeddyBear:(int) l withAnimFrames:(NSMutableArray*) animFrames withSpriteFrameName:(NSString*) spriteFrameName withDelay:(float) d
{
CGSize winSize = [CCDirector sharedDirector].winSize;
int rndY;
rndY = arc4random() % ((int) winSize.height - 200) + 110;
NSLog(@"winSize.height = %f\n", winSize.height);
life = l;
_teddyBearSprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
_teddyBearSprite.position = ccp(arc4random() % (int) winSize.width, rndY);
_walkAnim = [CCAnimation animationWithFrames:animFrames delay:d];
self.walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:_walkAnim restoreOriginalFrame:NO]];
[_teddyBearSprite runAction:_walkAction];
}
@end
// HelloWorldLayer.h
#import "cocos2d.h"
// HelloWorldLayer.h
@interface HelloWorldLayer : CCLayer
{
/*CCSprite *_teddyBearSprite;
CCAction *_walkAction;
CCAction *_moveAction;
CCArray *_teddyBears;
int _nextTeddyBear;
double _nextTeddyBearSpawn;*/
NSMutableArray *_teddyBears;
}
//@property (nonatomic, retain) CCSprite *teddyBearSprite;
//@property (nonatomic, retain) CCAction *walkAction;
//@property (nonatomic, retain) CCAction *moveAction;
//@property (nonatomic, assign) NSMutableArray *teddyBears;
// returns a CCScene that contains the HelloWorldLayer as the only child
+(CCScene *) scene;
@end
// HelloWorldLayer.m
#import "HelloWorldLayer.h"
#import "TeddyBear.h"
#define kNumTeddyBears 15
[…]
- (id) init
{
if((self = [super init]))
{
self.isTouchEnabled = YES;
/* Inizio animazione teddyBear. */
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimTeddyBear.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimTeddyBear.png"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 4; ++i)
{
[walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"teddybear%d.png", i]]];
_teddyBears = [NSMutableArray array];
TeddyBear *tmpTeddyBear = [TeddyBear new];
//[tmpTeddyBear initTeddyBear:1 withAnimFrames:walkAnimFrames withSpriteFrameName:@"teddybear1.png" withDelay:0.09f];
for(int i = 0; i < kNumTeddyBears; i++)
{
[tmpTeddyBear initTeddyBear:1 withAnimFrames:walkAnimFrames withSpriteFrameName:@"teddybear1.png" withDelay:0.09f];
[spriteSheet addChild:[tmpTeddyBear teddyBearSprite]];
[_teddyBears addObject:tmpTeddyBear];
}
NSLog(@"Grandezza _teddyBears: %d in init()\n", [_teddyBears count]);
int i = 0;
for(TeddyBear *tb in _teddyBears)
{
NSLog(@"i = %d\n", i);
}
}
return self;
}
[…]
-(void) ccTouchEnded:(UITouch *) touch withEvent:(UIEvent *) event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
NSLog(@"Grandezza _teddyBears: %d in ccTouchEnded\n", [_teddyBears count]);
}
[…]
|