2012-06-18 41 views
0

我開始與CoCos2d開發,我認爲我有節點空間和行動的一些問題,這是我的代碼和nothings移動雖然我似乎沒有錯誤時運行代碼,任何意見表示讚賞:CCMoveto不會移動

+(id) create { 
    return [[self alloc] init];} 

    -(id) init { 
     if ((self = [super init])) 
     { 
     CGSize scSize = [[CCDirector sharedDirector] winSize]; 

    self.position = ccp(0,scSize.height);; 


    touchArea = [CCSprite spriteWithFile:@"touchArea.png"]; 
    [touchArea setAnchorPoint:ccp(0, 0.5)]; 
    [self addChild:touchArea]; 


    obj_1 = [CCSprite spriteWithFile:@"obj1.png"]; 
    obj_2 = [CCSprite spriteWithFile:@"obj2.png"]; 
    obj_3 = [CCSprite spriteWithFile:@"obj3.png"]; 

    obj_tab = [NSMutableArray arrayWithObjects:obj_1, obj_2, obj_3, nil]; 


     for (int i=0; i < obj_tab.count; i++) 
     { 
      CCSprite *obj = [obj_tab objectAtIndex:i]; 
      float offSetPos = ((float)(i+1))/(obj_tab.count +1); 
      CGPoint pos = ccp(scSize.width*offSetPos, 0); 
      obj.position = pos; 
      [obj setAnchorPoint:ccp(0.5, -0.25)]; 
      [touchArea addChild:obj]; 
     } 

    velocidad = 3; 
    destino = ccp(0,-100); 
    //[self scheduleUpdate]; 
    [self moveIt] // <-- Edited to call another method 
} 
    return self; 

} 

-(void) moveit{ 
    CCMoveTo *moverAction = [CCMoveTo actionWithDuration:5 position:ccp(0,0)]; 
    CCCallFunc *onEndAction = [CCCallFunc actionWithTarget:self selector:@selector(onEndMove:)]; 
    CCSequence *moveSequence = [CCSequence actions: moverAction, onEndAction, nil]; 
    [touchArea runAction:moveSequence]; 
{ 

-(void) update:(ccTime)delta { 

} 

-(void)onEndMove:(id)sender{ 
    NSLog(@"OnEndMove fired"); 
} 

-(void) onEnter{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES]; 
} 

-(void) onExit { 
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self]; 
} 


-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{ 

    CGRect boundingBox = touchArea.boundingBox; 
    boundingBox.origin = CGPointZero; 

    BOOL isTouchHandled = CGRectContainsPoint(boundingBox, [touchArea convertTouchToNodeSpace: touch]); 

    //NSLog(@"touchLocation y = %f", touchLocation.y); 
    //NSLog(@"touchArea y = %f", touchArea.position.y); 

    if (isTouchHandled){ 
     NSLog(@"Touch Handled"); 
    } 

    return isTouchHandled; 

} 
@end 

回答

0

我假設'自我',上面顯示的代碼的類,從CCNode派生。確保'self'正在運行,它必須是CCNode的後代,並且在將它添加到正在運行的節點時將運行。這是一個CCNode的的OnEnter(從1.0.1版本拍攝)會發生什麼:

-(void) onEnter 
{ 
    [children_ makeObjectsPerformSelector:@selector(onEnter)]; 
    [self resumeSchedulerAndActions]; 

    isRunning_ = YES; 
} 

這是那裏的調度和行動爲節點啓用。如果它從未被調用,您的操作將不會運行。

您可以將moveIt代碼移動到onEnter方法,該方法將在對象運行時調用。

-(void) onEnter{ 
    CCLOG(@"%@<onEnter> : invoked",self.class); 
    [self moveIt]; 
    [super onEnter]; 
} 
+0

感謝Yves,我剛剛打電話給我[super onEnter](我的自定義類繼承自CCNODE),現在它正在工作,我正在學習一本書「學習CoCos2D遊戲開發...」,並且在代碼中提供了甚至沒有對onEnter調用派生自定義類來自CCNODE,但它仍然設法執行這些行動......我不明白它是否有效,是一種苦樂參半的勝利^^。謝謝你的幫助。 – Rubs

+0

您使用的資源非常好。熟悉coco的內部知識,不要害怕在代碼中找出'底層'實際發生的事情,你很快就會加快速度。一旦你獲得了它的對象模型以及如何讓它爲你自己工作,這是一個很好的框架。 – YvesLeBorg

0

-(void) update:(ccTime)delta被調用的頻率是多少? (可能從你的scheduleUpdate方法開始。)你可能每秒鐘都會調用[touchArea runAction:moveSequence];幾次,這會導致大量的CCMoveTos排隊。

您應該將runAction:完全移出,或者至少在您的update:方法中添加一些邏輯,以檢查touchArea是否應該從另一個runAction:開始。

+0

感謝我看到這一點,但是我改變了scheduleUpdate(我剛剛編輯的原始郵件中的代碼),仍然沒有動作:( – Rubs

+0

@Rubs你告訴它移動touchArea到位置0, 0 ...這是默認的位置,嘗試放入類似於:'ccp(150,150)' – MechEthan

+0

Hi HachiEthan,我爲移動動作位置使用了一些不同的值,例如ccp(0,-240),我的意圖是在縱向模式下將它自上而下移動,但仍然沒有任何動作,原始位置self.position = ccp(0,scSize.height);對於整個節點,我也嘗試移動它,而不是[touchArea runAction .. 。],原來我設置了[self run action:moveSequence] ...感謝您的好意和時間! – Rubs