创建多个精灵(新手Cocos2d问题)

时间:2011-06-28 21:02:14

标签: cocos2d-iphone sprite ccsprite

我有一个CCLayer类,我创建了一个动画精灵,它是游戏的“英雄”。现在,我正试图为他创造多个敌人来战斗,我遇到了麻烦。我想创建一个名为“Enemy”的类,它保存与敌人相关的所有内容,并为我的所有敌人创建该类的多个实例。我该怎么做呢?

这是我到目前为止的课程(我创造了英雄,称为“_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 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;
}

1 个答案:

答案 0 :(得分:0)

我可以给你答案但这没用。您需要在运行之前进行爬网。转到RayWenderlich.com并至少完成他的一些Cocos2D教程。你可以让自己快速上手。