1

请耐心等待,我是一个新手,我第一次编写自定义初始化,sentance01text加载正常,所以我知道 plist 读取正常 :) 但我似乎无法让我的sentance01Timing计时工作><

这个方法应该传递用户输入的sentance01Timing,它是objectForKey,它应该将相关数组加载到audioIntervals数组中

目前我正在使用 NSString 访问字典并将其传递给数组audiointervals 但这似乎是错误的并导致错误任何帮助将不胜感激。我不明白为什么它对文本有效,但不是这个? 终止 - 原因:'-[__NSCFArray isFileURL]:

可能我在做一些非常愚蠢的事情><如果你可以请帮忙 ps 我不得不使用我的旧mac(我的好那个现在正在修理 - 所以将使用非弧 - 然后在我拿回我的漂亮mac时更新代码,所以请记住它为什么我现在不释放任何对象......)

你好世界.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>
4

1 回答 1

0

非常感谢 trojanfoe,我能够解决问题,主要问题是如果您确实在代码中看到了更好的方法,我的初始化对象不会保留,请说我的目标是学习(但小步骤!一切都很漂亮新的!!)我在我的旧 Mac 笔记本电脑上做这个(所以它不是用于 arc - 因为我目前无法测试它)但也许它会帮助其他人......

所以创建我的文本管理器类:

@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

于 2013-08-14T14:34:26.080 回答