2012-12-12 27 views
1

在我的應用程序中,我想移動一個點到點的路徑上的對象(基於某個事件)。我想從cocosbuilder文件(.ccbi)中提取這些位置。那麼如何提取陣列中的位置?cocos2d:CocosBuilder:如何獲取動畫關鍵幀的位置

的一種方法是把上的位置精靈並與變量一樣爲它們分配: SPR1,SPR2 spr3

,並採取在代碼中spr1.position。

另一種方法是在cocos-builder中製作位置動畫。現在提取這些關鍵幀在代碼中的位置。所以我的問題是:「有沒有辦法從動畫關鍵幀中提取位置?」

+0

這是更好地使用CocosBuilder效果進行一個簡單的開機動畫。即使有可能,最好綁定你的spr1,...並更改代碼中的位置。 –

回答

0

我認爲這是可能的,儘管我自己沒有這樣做。 如果您查看一個ccb文件,您會注意到它只是一個XML文件。 而這個XML文件只存儲關鍵幀信息(當關鍵幀發生時)。

ccb文件here有詳細記錄的結構。 你可能想看看如何獲​​得關鍵幀信息。

1

這是一個與CocosBuilder 2.1兼容的解決方案。 以下功能添加到CCBAnimationManager類:

- (void)enumeratePropertiesForSequence:(NSString*)name Block:(BOOL(^)(CCNode *node, CCBSequenceProperty *seqProp))block 
{ 
    int seqId = [self sequenceIdForSequenceNamed:name]; 
    if (seqId == -1) 
    { 
     NSLog(@"Sequence %@ couldn't be found",name); 
     return; 
    } 
    for (NSValue* nodePtr in nodeSequences) 
    { 
     CCNode* node = [nodePtr pointerValue]; 

     NSDictionary* seqs = [nodeSequences objectForKey:nodePtr]; 
     NSDictionary* seqNodeProps = [seqs objectForKey:[NSNumber numberWithInt:seqId]]; 

     // Nodes that have sequence node properties 
     for (NSString* propName in seqNodeProps) 
     { 
      CCBSequenceProperty* seqProp = [seqNodeProps objectForKey:propName]; 

      if (!block(node, seqProp)) 
       return; 
     } 
    } 
} 

使用此枚舉您可以訪問所有可用的屬性,例如如果你有興趣只是位置則:

#import "MyClass.h" 
#import "CCBAnimationManager.h" 
#import "CCBSequenceProperty.h" 
#import "CCBKeyframe.h" 
#import "CCNode+CCBRelativePositioning.h" 

@implementation MyClass 
-(void)fn 
{ 
    CCBAnimationManager* animationManager = self.userObject; 

    BOOL(^block)(CCNode *node, CCBSequenceProperty *seqProp) = 
    ^BOOL(CCNode *node, CCBSequenceProperty *seqProp) 
    { 
     NSLog(@"Node tag %d, Prop name [%@], type %d", node.tag, seqProp.name, seqProp.type); 

     for (CCBKeyframe *kf in seqProp.keyframes) 
     { 
      if ([seqProp.name isEqualToString:@"position"]) 
      { 
       id value = kf.value; 

       // Get relative position 
       float x = [[value objectAtIndex:0] floatValue]; 
       float y = [[value objectAtIndex:1] floatValue]; 

       // Get position type 
       int type = [[[self.userObject baseValueForNode:node propertyName:seqProp.name] objectAtIndex:2] intValue]; 

       CGSize containerSize = [self.userObject containerSize:node.parent]; 

       CGPoint absPos = [node absolutePositionFromRelative:ccp(x,y) type:type parentSize:containerSize propertyName:seqProp.name]; 

       NSLog(@"--- relative position (%f, %f), type %d, abs position (%f, %f)", x, y, type, absPos.x, absPos.y); 
      } 
     } 

     return YES; // YES to continue, NO to stop enumeration 
    }; 

    [animationManager enumeratePropertiesForSequence:@"MySequence" Block:block]; 
} 

@end 

這裏是如何實例MyClass的,並呼籲枚舉:

CCNode *myclass = [CCBReader nodeGraphFromFile:@"MyClass.ccbi"]; 
[myclass fn];