在我的遊戲中,我爲我的敵人制作了一個CCSprite(cocos2d)的子類。 因爲我想控制動畫,所以從這個子類的每個實例中,我必須將我的主類中的動畫代碼翻譯成這個子類,並將敵人加載到這個子類中。在CCSprite子類中實現動畫的正確方法是什麼?
我發現這相當困難,但......一段時間後,它神奇地開始工作。
不幸的是,在創建和設置這個子類的屬性後,我開始出現奇怪的崩潰。因爲他們在cocosDenshion和其他與我的敵人課程無關的地方,經過我的代碼和網絡的深入研究,我確信它的某種數據損壞,我幾乎完全肯定它的因爲我完全用錯誤的方式完成了他的動畫代碼。
說實話,我甚至無法將我的想法放在這裏,以及這個實際上如何工作:S ...我完全卡住了。任何幫助深表感謝!
所以我的主要問題是:什麼是在CCSprite子類中實現動畫的正確方法? /我在這裏做錯了什麼?
簡化我的代碼在這裏:(它觸發動畫每2秒,以顯示我多麼想 使用它)
#import "cocos2d.h"
@interface Npc : CCSprite
{
CCAction *_runAnimation;
NSString* name;
int strength;
}
@property (nonatomic, retain) CCAction *runAnimation;
@property int strength;
@property (nonatomic, retain) NSString* name;
- (Npc*)loadAnimation;
- (void)animate;
@end
#import "Npc.h"
@implementation Npc
@synthesize runAnimation = _runAnimation;
@synthesize name;
@synthesize strength;
-(id) initWithTexture:(CCTexture2D*)texture rect:(CGRect)rect
{
if((self=[super initWithTexture:texture rect:rect]))
{
}
return self;
}
- (Npc*)loadAnimation
{
int lastFrame = 11;
NSString *creatureFile = @"vis 1";
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
[NSString stringWithFormat:@"%@.plist", creatureFile]];
CCSpriteBatchNode* sheet = [CCSpriteBatchNode batchNodeWithFile:
[NSString stringWithFormat:@"%@.png", creatureFile]];
self = [Npc spriteWithTexture:sheet.texture];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
for (int x = 0; x < lastFrame; x++)
{
[animFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"%@ %d.png", creatureFile, x]]];
}
CCAnimation* anim = [CCAnimation animationWithFrames: animFrames delay: 0.1];
self.runAnimation = [CCAnimate actionWithAnimation:anim restoreOriginalFrame:NO];
[self runAction:_runAnimation];
return self;
}
- (void)animate
{
[self runAction:self.runAnimation];
}
- (void)dealloc
{
[super dealloc];
[name release];
}
@end
#import "HelloWorldLayer.h"
#import "Npc.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if((self=[super init]))
{
timer = 0;
creatureTemp = [Npc spriteWithFile:@"Icon.png"];
creature = [creatureTemp loadAnimation];
creature.position = ccp(100,100);
[self addChild:creature];
[self schedule:@selector(nextFrame:)];
}
return self;
}
- (void)nextFrame:(ccTime)dt
{
timer += dt;
if (timer > 2.)
{
[creature animate];
timer = 0.;
}
}
- (void) dealloc
{
[super dealloc];
}
@end
- -----------------編輯--------------------
我改變了我的代碼有教程的幫助雷Wenderlich:http://www.raywenderlich.com/3888/how-to-create-a-game-like-tiny-wings-part-1
這是我認爲更接近它應該是什麼。不幸的是,它仍然在我的iPhone(不是模擬器)上在SimpleAudioEngine上崩潰(我正確實施),所以我仍然做錯了什麼。在NPC類的頂部
:
@synthesize batchNode = _batchNode;
全國人大類的初始化:
-(id) initNpc
{
if((self=[super initWithSpriteFrameName:@"vis 1 0.png"]))
{
_normalAnim = [[CCAnimation alloc] init];
NSMutableArray* animFrames = [[NSMutableArray alloc] init];
int lastFrame = 11;
for (int x = 0; x < lastFrame; x++)
{
[animFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"vis 1 %d.png", x]]];
}
_normalAnim = [CCAnimation animationWithFrames: animFrames delay: 0.1];
self.runAnimation = [CCAnimate actionWithAnimation:_normalAnim restoreOriginalFrame:NO];
}
return self;
}
和的HelloWorldLayer
-(id) init
{
if((self=[super init]))
{
timer = 0;
_batchNode = [CCSpriteBatchNode batchNodeWithFile:@"vis 1.png"];
[self addChild:_batchNode];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"vis 1.plist"];
creature = [[[Npc alloc] initNpc] autorelease];
creature.position = ccp(200,200);
[_batchNode addChild:creature];
[self schedule:@selector(nextFrame:)];
[[SimpleAudioEngine sharedEngine] preloadEffect:@"super1.mp3"];
}
return self;
}
感謝您的回覆!我實際上已經意識到這一點,這是不好的......但我不知道如何以不同的方式做...你可能會指出我在正確的方向嗎? – yurki 2012-08-11 12:59:58