我最近開始學習Objective-C和Cocos-2D。我試圖定義自己的方法來自動創建精靈。我自己的方法沒有被編譯器發現
我說我自己的類,我將創建其他自動化方法爲好。總之我的.h文件看起來像這樣:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface ActionsClass : CCNode {
}
@property (nonatomic, strong) CCSprite* createSprite;
@property (nonatomic, strong) CCSprite* spriteName;
@property (nonatomic, strong) NSString* pngName;
@property (nonatomic) CGPoint* spriteCoordinate;
- (CCSprite *)createSprite: (CCSprite *)spriteName: (NSString *)pngName: (CGPoint *)spriteCoordinate;
@end
與.m是:
#import "ActionsClass.h"
@implementation ActionsClass
@synthesize createSprite = _createSprite;
@synthesize spriteName = _spriteName;
@synthesize pngName = _pngName;
@synthesize spriteCoordinate = _spriteCoordinate;
- (CCSprite *)createSprite: (CCSprite *)spriteName: (NSString *)pngName: (CGPoint *)spriteCoordinate
{
if (!_createSprite)
{
_createSprite = [[CCSprite alloc] init];
_spriteName = [CCSprite spriteWithFile:_pngName];
_spriteName.position = ccp(_spriteCoordinate->x, _spriteCoordinate->y);
[self addChild:_spriteName];
}
return _createSprite;
}
@end
在我要調用該方法的主要.m文件:
[self createSprite: saif: @"saif.png": ccp(100,100)];
這將使警告的Xcode沒有找到實例方法createSprite,默認它ID
個非常感謝和抱歉,如果字體或問題的格式不超利落。
您只能在定義方法的對象種類中使用'self'作爲消息的接收者。您似乎在說要從其他.m文件調用'ActionsClass',如果是,您首先需要創建一個'ActionsClass'對象並使用它來調用'createSprite:'。 –
@PhillipMills你說得對,我想稱之爲'createSprite'到另一個.M,但我不完全知道如何完成你在說什麼;/ – Saifallah
您可能需要閱讀一些有關的Objective-C編程,尤其是對象,然後再潛入更遠的地方。 (如:https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html)除了我上面說,你的代碼是做奇怪的事情就像使用'_pngName '不初始化它,返回一個你不使用的值,並返回一個與你正在初始化的值不同的值。換句話說,如果沒有更好的基礎知識,我不會看到一個解決方案。 –