0

我需要你的广告:我有 2 个类,首先我初始化第二类的对象,我想使用第二类的功能,例如:

在第一堂课中,我初始化了第二堂课的对象

self.keys = [KeyLayer node];
self.keys.position = CGPointMake(-0.f, -0.f);
[self addChild:self.keys z:1]; 

我想将第二类的关闭功能应用于self.keys对象,例如:

[self.keys.sprite self.keys.close]; 

但它不起作用

二等

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface KeyLayer : CCLayer  {

    CCLabelTTF *_label;
    CCAction *_actionOpen;
    CCAction *_actionClose;
    CCSprite *_sprite;
    CCAnimation *_animation;

}
@property (nonatomic, retain) CCSprite *sprite;
@property (nonatomic, retain) CCAction *actionOpen;
@property (nonatomic, retain) CCAction *actionClose;
@property (nonatomic, retain) CCAnimation *animation;
@property (nonatomic, retain) CCLabelTTF *label;

+(id) scene;
-(void) open;
-(void) close;
@end

// * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *

#import "KeyLayer.h"
#import "GameLayer.h"

@implementation KeyLayer

@synthesize sprite = _sprite;
@synthesize actionOpen = _actionOpen;
@synthesize actionClose = _actionClose;
@synthesize animation = _animation;
@synthesize label = _label;


+(id) scene
{
    CCScene *scene = [CCScene node];

    return scene;
}

-(id) init
{
    if ((self = [super init]))
    {           
                CGSize screenSize = [[CCDirector sharedDirector] winSize];

        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile: [NSString stringWithFormat:@"key.plist"]];
        CCSpriteBatchNode *animationSpriteSheet = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"key.png"]];
        [self addChild:animationSpriteSheet];

        //*************************************************

        NSMutableArray *animationFrames = [NSMutableArray array];
        for(int i = 1; i <= 5; ++i) {

                    [animationFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"key_%d.png", i]]];
        }
        _animation = [CCAnimation animationWithFrames:animationFrames delay:0.1f];
        self.sprite = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"key_1.png"]]; 
        self.actionOpen = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:_animation restoreOriginalFrame:NO] times:1];

        //*********************************************

        for(int i = 3; i <= 5; ++i) {

            [animationFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: [NSString stringWithFormat:@"key_%d.png", i]]];
        }
        _animation = [CCAnimation animationWithFrames:animationFrames delay:0.1f];
        self.sprite = [CCSprite spriteWithSpriteFrameName: [NSString stringWithFormat:@"key_1.png"]]; 
        self.actionClose = [CCRepeat actionWithAction:[CCAnimate actionWithAnimation:_animation restoreOriginalFrame:NO] times:1];

        //*********************************************************  


        self.sprite.anchorPoint = ccp([self.sprite boundingBox].size.width/800, [self.sprite boundingBox].size.height/800);
        self.sprite.position = CGPointMake(-0.f, -0.f); 
        [animationSpriteSheet addChild:self.sprite z:1];


        //****************************************************

        self.label = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"A"] dimensions:CGSizeMake(self.sprite.contentSize.width, self.sprite.contentSize.height) alignment:UITextAlignmentCenter fontName:@"Arial" fontSize:40];
        self.label.anchorPoint = ccp([self.label boundingBox].size.width/800, 0.25);
        self.label.position = CGPointMake(-0.f, -0.f);   
        [self addChild:self.label z:1];

    }
    return self;
}

-(void) openKey
{
    [self.sprite runAction:self.actionOpen];
}
-(void) closeKey
{
    [self.sprite runAction:self.actionClose];
}
- (void) dealloc
{
    self.sprite = nil;
    self.actionOpen = nil;
    self.actionClose = nil;
    self.label=nil;
    [super dealloc];
}
@end
4

1 回答 1

0

首先,这不是您在任何对象上调用方法的方式:

[self.keys.sprite self.keys.close]; // even if self.keys.close was defined this is not how you call it

现在,我猜你想在你的精灵上调用 closeKey 以便可以通过以下方式轻松完成:

[self.keys closeKey]; // self.keys is your object and closeKey is the method you desire to call on your object. 
于 2012-04-04T10:39:05.950 回答