Creating multiple sprites (Novice Cocos2d Question)
I have a CCLayer class where I create an animated sprite that is the "hero" for the game. Now, I am trying to create multiple enemies for him to fight, and I'm having trouble. I want to create a class called "Enemy" that holds everything relevant to the enemy, and just create multiple instances of that class for all my enemies. How do I go about doing this?
Here's the class I have so far (where i create teh hero, called "_bear".
#import "FightGame.h"
#import "SneakyJoystick.h"
#import "SneakyJoystickSkinnedBase.h"
#import "ColoredCircleSprite.h"
#import "CCTouchDispatcher.h"
#import "GManager.h"
CCSprite *player;
BOOL _moving, _crouched, _jumping, _actionTouch, _maxJump, _leftJumping, _rightJumping, _punching, _kicking, _touchSet;
int startX, startY;
@implementation FightGame
@synthesize bear = _bear;
@synthesize jumpAction = _jumpAction;
@synthesize walkAction = _walkAction;
@synthesize punchAction = _punchAction;
@synthesize kickAction = _kickAction;
@synthesize crouchAction = _crouchAction;
@synthesize currentTouch;
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
FightGame *layer = [FightGame 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
{
// always call "super" init
// Apple recommends to re-assign "self" with开发者_JAVA技巧 the "super" return value
if( (self=[super init] )) {
/*player = [CCSprite spriteWithFile: @"Person1.png"];
player.position = ccp( 50, 100 );
[self addChild:player]; */
//bools
_maxJump = FALSE;
_jumping = FALSE;
_leftJumping = FALSE;
_rightJumping = FALSE;
_touchSet = FALSE;
//Animated Bear
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Fighter.plist"];
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode
batchNodeWithFile:@"Fighter.png"];
[self addChild:spriteSheet];
//load frames
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 6; ++i) {
[walkAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"walk%i.png", i]]];
}
NSMutableArray *punchAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i) {
[punchAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"punch%i.png", i]]];
}
NSMutableArray *jumpAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
[jumpAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"jump%i.png", i]]];
}
NSMutableArray *kickAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 3; ++i) {
[kickAnimFrames addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:@"kick%i.png", i]]];
}
//create animation
CCAnimation *walkAnim = [CCAnimation
animationWithFrames:walkAnimFrames delay:0.1f];
CCAnimation *punchAnim = [CCAnimation
animationWithFrames:punchAnimFrames delay:0.1f];
CCAnimation *jumpAnim = [CCAnimation
animationWithFrames:jumpAnimFrames delay:0.5f];
CCAnimation *kickAnim = [CCAnimation
animationWithFrames:kickAnimFrames delay:0.1f];
CGSize winSize = [CCDirector sharedDirector].winSize;
self.bear = [CCSprite spriteWithSpriteFrameName:@"walk1.png"];
_bear.position = ccp(winSize.width/2, winSize.height/2);
//creating the actions
self.walkAction = [CCRepeatForever actionWithAction:
[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
self.punchAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:punchAnim] times:1];
self.jumpAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:jumpAnim] times:1];
self.kickAction = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:kickAnim] times:1];
[spriteSheet addChild:_bear];
//end animated bear, start joystick
SneakyJoystickSkinnedBase *leftJoy = [[[SneakyJoystickSkinnedBase alloc] init] autorelease];
leftJoy.position = ccp(60,60);
leftJoy.backgroundSprite = [ColoredCircleSprite circleWithColor:ccc4(105, 105, 105, 200) radius:55];
leftJoy.thumbSprite = [ColoredCircleSprite circleWithColor:ccc4(255, 0, 0, 200) radius:25];
leftJoy.joystick = [[SneakyJoystick alloc] initWithRect:CGRectMake(0,0,128,128)];
leftJoystick = [leftJoy.joystick retain];
[self addChild:leftJoy];
[self schedule:@selector(nextFrame:)];
self.isTouchEnabled = YES;
}
return self;
}
I could just give you the answer on this but that is useless. You need to crawl before you can run. Go to RayWenderlich.com and do at least some of his Cocos2D tutorials. You will get yourself up to speed quickly enough.
精彩评论