0

私はあなたのアドワイズが必要です:私は2つのクラスを持っています.最初に2番目のオブジェクトを初期化し、次のような2番目のクラスの関数を使用したいと思います:

最初のクラスでは、2番目のオブジェクトを初期化します

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

そして、次のようなself.keysオブジェクトに2番目のクラスのclose関数を適用したい:

[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 に答える