[分享]iphone游戏中关卡生成 XML 方式

最近在尝试写一个类似于打地鼠的游戏,遇到了小小的困难,主要是老鼠坐标无法存储,手点了之后,不只如何通过坐标的方式遍历并且消失,或者是相关的动作。

从Throw too代码中,学习了一点XML存储并操作,以达到对关卡地图等的绘制。分享下。

 

首先放出XML格式

代码
 1 <thrown map="level 1" author="orta" throws="1">
 2   <!-- important details -->
 3   <background file="default"/>
 4   <player x="40" y="233"/> 
 5   <exit x="240" y="0" width="40" height="2"/>
 6   <size width="480" height="320"/>
 7   <next level="level2"/>
 8   
 9   <!-- actual map -->
10   <line x1="0" y1="200" x2="80" y2="200"/>
11   <line x1="80" y1="200" x2="240" y2="40"/>
12   <line x1="240" y1="40" x2="240" y2="0"/>
13   <line x1="280" y1="0" x2="280" y2="40"/>
14   <line x1="280" y1="40" x2="400" y2="200"/>
15   <line x1="400" y1="200" x2="480" y2="200"/>
16   
17   <spikes direction="up" x="0" y="0" width="200" height="10"/>
18   <spikes direction="up" x="400" y="200" width="80" height="10"/>
19 
20 </thrown>

 

 

方法的调用。

XMLLevelLoader *levelLoader;

  [levelLoader:url parseError:&parseError];parseXMLFileAtURL

 

 

方法的原形,也就是定义。通过URL初始化,读取XML,而URL则是自行定义的,XML地址。

 

- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error {

  NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];

  // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.

  [parser setDelegate:self];


  [parser setShouldProcessNamespaces:NO];

  [parser setShouldReportNamespacePrefixes:NO];

  [parser setShouldResolveExternalEntities:NO];

  

  [parser parse];

  

  NSError *parseError = [parser parserError];

  if (parseError && error) {

    *error = parseError;

  }

  

  [parser release];

} 

 

重点注意下这一句话: [parser parse];

调用了Parse方法。进行细化处理。。。下面贴一下具体情况是如何处理的。比较简单。没见过的那些函数大多是重力,刚体等特性的,移动,或者是图形路径的绘制。不必多说,自行参考下chipmunk。h这个开源的函数吧。

 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

  if (qName) {

    elementName = qName;

  }

//  NSLog(@"element = %@ , attr = %@", elementName, attributeDict);

  

  if ([elementName isEqualToString:@"line"]) {

    float x1 = [[attributeDict valueForKey:@"x1"] floatValue];

    float y1 = [[attributeDict valueForKey:@"y1"] floatValue];

    float x2 = [[attributeDict valueForKey:@"x2"] floatValue];

    float y2 = [[attributeDict valueForKey:@"y2"] floatValue];

    make_scenery_line(x1, y1, x2, y2, kColl_Scenery);

    

  }else if([elementName isEqualToString:@"circle"]) {

    float x = [[attributeDict valueForKey:@"x"] floatValue];

    float y = [[attributeDict valueForKey:@"y"] floatValue];

    float radius = [[attributeDict valueForKey:@"radius"] floatValue];

    make_scenery_circle(x, y, radius);


  }else if([elementName isEqualToString:@"background"]) {

    NSString * url = [attributeDict valueForKey:@"url"];

    if(url != nil){

      //  [game setPsuedoBackground:[NSURL URLWithString:url]];  

    }

    NSString * fileURL = [attributeDict valueForKey:@"file"];

    if(fileURL != nil){

      //TODO

    //  NSBundle* bundle = [NSBundle mainBundle];

      // [game setPsuedoBackground: [NSURL fileURLWithPath: [bundle pathForResource:fileURL ofType:@"png"]]];  

    }

  

  }else if ([elementName isEqualToString:@"spikes"]) {

    float x = [[attributeDict valueForKey:@"x"] floatValue];

    float y = [[attributeDict valueForKey:@"y"] floatValue];

    float width = [[attributeDict valueForKey:@"width"] floatValue];

    float height = [[attributeDict valueForKey:@"height"] floatValue];

    NSString * type = [attributeDict valueForKey:@"direction"];

    if([type compare:@"up"] == NSOrderedSame){

      make_up_scenery_spikes(x, y, width, height);

    }else if([type compare:@"down"] == NSOrderedSame){

      make_down_scenery_spikes(x, y, width, height);

    }else if([type compare:@"left"] == NSOrderedSame){

      make_left_scenery_spikes(x, y, width, height);

    }else if([type compare:@"right"] == NSOrderedSame){

      make_right_scenery_spikes(x, y, width, height);

    }

    

  }else if([elementName isEqualToString:@"next"]) {

    NSString *level = [attributeDict valueForKey:@"level"];

    // TODO

    [game setNextLevel: level];

    

  }else if([elementName isEqualToString:@"size"]) {

    float width = [[attributeDict valueForKey:@"width"] floatValue];

    float height = [[attributeDict valueForKey:@"height"] floatValue];

    mapSize = cpv(width, height);

    make_scenery_box(0, 0, width, height);

    

  }else if([elementName isEqualToString:@"thrown"]) {

    numberOfThrows = [[attributeDict valueForKey:@"throws"] floatValue];

    totalNumberOfThrows = [[attributeDict valueForKey:@"throws"] floatValue];


  }else if([elementName isEqualToString:@"player"]) {

    float x = [[attributeDict valueForKey:@"x"] floatValue];

    float y = [[attributeDict valueForKey:@"y"] floatValue];

    move_player(cpv(x, y));

    player_origin_x = x;

    player_origin_y = y;    

    

  }else if ([elementName isEqualToString:@"exit"]) {

    float x = [[attributeDict valueForKey:@"x"] floatValue];

    float y = [[attributeDict valueForKey:@"y"] floatValue];

    float width = [[attributeDict valueForKey:@"width"] floatValue];

    float height = [[attributeDict valueForKey:@"height"] floatValue];

    make_scenery_exit_box(x, y, width, height);

  }

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{

  NSLog(@"Error on XML Parse: %@", [parseError localizedDescription] );

}


 暂时罗嗦这多,,继续看。

 

 

posted @ 2010-01-17 16:43  AlexLiu  阅读(1481)  评论(0编辑  收藏  举报