请耐心等待我的努力 我是第一次写自定义初始化, sentance01text 加载正常,所以我知道plist正在读取确定:)但我似乎无法让我的 sentance01Timing 时间工作><
此方法应传递用户输入的 sentance01Timing ,这是objectForKey,它应将相关数组加载到 audioIntervals数组
目前我正在使用NSString访问字典并将其传递给数组 audiointervals 但这似乎是错误的,并导致错误anyhelp将不胜感激。 我不明白为什么它适用于文本而不是这个? 终止 - 原因:' - [__ NSCFArray isFileURL]:
可能我正在做一些非常愚蠢的事情><如果可以的话请帮忙 ps我不得不使用我的旧Mac(我的好一个正在被修复 - 所以将使用非弧 - 然后更新代码当我得到我的漂亮的mac回来,所以请记住它 这就是为什么我现在不释放任何物品......)
HelloWorld.m
#import "HelloWorldLayer.h"
#import "TextWithAudioHilight.h"
@implementation HelloWorldLayer
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init])) {
TextWithAudioHilight *pagetext = [[TextWithAudioHilight alloc]initWith5:@"test words here,\nmore words, more words.."
sentance01Timing:@"TimingSEN01"
withSoundNamed:nil];
[self addChild:pagetext];
}
return self;
}
- (void) dealloc
{
[super dealloc];
}
@end
TextWithAudio.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface TextWithAudioHilight : CCLayer {
}
@property(nonatomic,retain)NSString *sentance01text;
@property(nonatomic,retain)NSString *sentance01Timing;
@property(nonatomic,retain)NSString *soundNamed;
-(id)initWith5:(NSString *)sentance01text sentance01Timing:(NSString *)sentance01Timing withSoundNamed:(NSString *)soundNamed;
@end
TextWithAudio.m
#import "TextWithAudioHilight.h"
@implementation TextWithAudioHilight
@synthesize sentance01text = _sentance01text;
@synthesize sentance01Timing = _sentance01Timing;
@synthesize soundNamed = _soundNamed;
-(id)initWith5:(NSString *)sentance01text sentance01Timing:(NSString *)sentance01Timing withSoundNamed:(NSString *)soundNamed
{
self = [super init];
if(self)
{
CGSize size = [[CCDirector sharedDirector] winSize];
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"AudioTimings" ofType:@"plist"];
NSDictionary* myDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
//needs create array that has timing information as an array from the plist
//NSString *Text01timing = [myDictionary objectForKey:@"sentance01Timing"];
NSString *Text01timing = [myDictionary objectForKey:_sentance01Timing];
NSMutableArray * audioIntervals = [NSMutableArray arrayWithContentsOfFile:Text01timing];
NSLog(@"Text01timing %f",audioIntervals);
//needs to create a label and put the text in it
CGSize maxSize = {800, 200};
CGSize actualSize = [sentance01text sizeWithFont:[UIFont fontWithName:@"Helvetica" size:20]
constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
CGSize containerSize = { actualSize.width, actualSize.height };
CCLabelTTF *label = [CCLabelTTF labelWithString:sentance01text dimensions:containerSize
alignment:UITextAlignmentLeft fontName:@"Helvetica"
fontSize:20];
// Center label
label.position = ccp( size.width /2 , size.height/6 );
label.color = ccc3(80, 80, 80);
// Add label to this scene
[self addChild:label z:7];
}
return self;
}
@end
AudioTimings.plist
<plist version="1.0">
<dict>
<key>TimingSEN01</key>
<array>
<real>0.044444</real>
<real>0.143054</real>
<real>0.213886</real>
<real>0.48055</real>
<real>0.844434</real>
<real>1.345817</real>
<real>1.470816</real>
<real>1.577759</real>
<real>2.020809</real>
<real>2.331917</real>
</array>
</dict>
</plist>
答案 0 :(得分:0)
非常感谢trojanfoe,我能够解决这个问题 主要问题是我保留了初始化对象 如果你确实在代码中看到了更好的方法,请说我的目标是学习(但宝贝步骤!这一切都很新!!) 我在我的旧Mac笔记本电脑上做这个(所以它不是弧 - 因为我现在无法测试它) 但也许它会帮助别人...
所以创建我的textmangerclass:
@interface TextManagerWithpageNum : CCLayer
{
//create the properties you need notice the underscore
//NSString *_varName;
NSString *_background;
NSString* _text; //myText
NSString* _soundFile;
NSString* _audioInterval;
NSString* _currPageNumber;
}
声明@property,它允许访问值访问器(getter)和mutator(setter)
//@property(nonatomic, strong)NSString* background; - in arc
@property(nonatomic, retain) NSString* background;
@property(nonatomic, retain) NSString* text;
@property(nonatomic, retain) NSString* soundFile;
@property(nonatomic, retain) NSString* audioInterval;
@property(nonatomic, retain) NSString* currPageNumber;
和实施.m
//@synthesize varName = _varName;
@synthesize background = _background;
@synthesize text = _text;
@synthesize soundFile = _soundFile;
@synthesize audioInterval = _audioInterval;
@synthesize currPageNumber = _currPageNumber;
-(id)initWithBackground:(NSString *)background
textFromPlist:(NSString *)text
soundNamed:(NSString *)soundFile
audioIntervalPlist:(NSString *)audioInterval
CurrentPage:(NSString *)currPageNumber
{
self = [super init];
if(self)
{
//_varName = varName;
_text = text;
_audioInterval = audioInterval;
_soundFile = soundFile;
_currPageNumber = currPageNumber;
/*here is where I was having the problem - its important to manually retain
and dealloc or you wont be able to use your objects created!*/
//audio interval
_audioInterval= [[myDictionary objectForKey:audioInterval]retain];//***please retain me! and dont forget to dealloc! :)
NSLog(@"audio Intervals %@",audioInterval);
你好世界
TextManagerWithpageNum *Mypage =
[[TextManagerWithpageNum alloc]initWithBackground:@"Page01Bg.png"textFromPlist:@"Page01Text"soundNamed:@"Page01" audioIntervalPlist:@"AudioTimings" CurrentPage:5];
[self addChild:Mypage];
//希望我填充的实际例子可以帮助别人 因为别人的错误让学习更有趣:P