我需要你的adwise:我有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
答案 0 :(得分:0)
t首先,这不是你如何在任何对象上调用方法:
[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.