08/05/12 12:57
_mikele_
Ciao a tutti, sono nuovo nella programmazione di dispositivi Apple e sto programmando un gioco per iPad come progetto scolastico con le librerie Cocos2d.
Ultimamente purtroppo mi sono bloccato perché l'applicazione va in crash quando cerco di accedere ad un array di tipo NSMutableArray (inizializzato nel metodo init()) in un altro metodo.
L'array dovrebbe contenere oggetti di tipo TeddyBear (classe creata da me).
[CODE]
// 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 withAnimFramesNSMutableArray*) animFrames withSpriteFrameNameNSString*) spriteFrameName withDelayfloat) d;
@end
// TeddyBear.m
#import "TeddyBear.h"
@implementation TeddyBear
@synthesize teddyBearSprite = _teddyBearSprite;
@synthesize walkAction = _walkAction;
@synthesize moveAction = _moveAction;
@synthesize walkAnim = _walkAnim;
- (void) initTeddyBearint) l withAnimFramesNSMutableArray*) animFrames withSpriteFrameNameNSString*) spriteFrameName withDelayfloat) 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];
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) ccTouchEndedUITouch *) touch withEventUIEvent *) event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
NSLog(@"Grandezza _teddyBears: %d in ccTouchEnded\n", [_teddyBears count]);
}
[…]
[/CODE]
L'applicazione va in crash nell'ultima riga del metodo -(void) ccTouchEnded… durante la stampa quando tenta di accedere a [_teddyBears count], è come se l'array non esistesse più dopo essere uscito dal metodo init().
Ultimamente purtroppo mi sono bloccato perché l'applicazione va in crash quando cerco di accedere ad un array di tipo NSMutableArray (inizializzato nel metodo init()) in un altro metodo.
L'array dovrebbe contenere oggetti di tipo TeddyBear (classe creata da me).
[CODE]
// 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 withAnimFramesNSMutableArray*) animFrames withSpriteFrameNameNSString*) spriteFrameName withDelayfloat) d;
@end
// TeddyBear.m
#import "TeddyBear.h"
@implementation TeddyBear
@synthesize teddyBearSprite = _teddyBearSprite;
@synthesize walkAction = _walkAction;
@synthesize moveAction = _moveAction;
@synthesize walkAnim = _walkAnim;
- (void) initTeddyBearint) l withAnimFramesNSMutableArray*) animFrames withSpriteFrameNameNSString*) spriteFrameName withDelayfloat) 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];
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) ccTouchEndedUITouch *) touch withEventUIEvent *) event {
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
NSLog(@"Grandezza _teddyBears: %d in ccTouchEnded\n", [_teddyBears count]);
}
[…]
[/CODE]
L'applicazione va in crash nell'ultima riga del metodo -(void) ccTouchEnded… durante la stampa quando tenta di accedere a [_teddyBears count], è come se l'array non esistesse più dopo essere uscito dal metodo init().
aaa