2012-03-24 19 views
1

所以我試圖從Cocos2D示例的EaseActionsTest示例中實現alterTime方法到我的遊戲中。更改來自不同課程的CCSpeed的行動?

我試圖將這個想法落實到我的遊戲中,爲我的遊戲創建一個凍結時間類型的加電。我用CCMenu設置了一個單獨的HUDLayer,將小行星的CCSpeed更改爲0以停止移動。我已經建立了一個CCLog,以確保我的freezeTime方法被調用,它的確如此,但小行星的CCSpeeds保持不變。這是我的代碼更清晰。

HUDLayer.h

#import "ActionLayer.h" 

@interface HUDLayer : CCLayer { 
    GameObject *asteroid; 
} 

-(void)freezeTime:(ccTime)dt; 

HUDLayer.mm

-(void)freezeTime:(ccTime)dt 
{ 
    id action = [asteroid getActionByTag:kTagAsteroidAction]; 
    [action setSpeed:0.0f]; 
    CCLOG(@"Freeze!"); 
} 

-(void)createPowerUpButtons 
{ 
    CGSize winSize = [CCDirector sharedDirector].winSize; 
    CCSprite *freeze = [CCSprite spriteWithSpriteFrameName:@"powerup.png"]; 
    CCMenuItem *freezeButton = [CCMenuItemImage itemFromNormalSprite:freeze selectedSprite:nil target:self selector:@selector(freezeTime)]; 

    CCMenu *powerUpMenu = [CCMenu menuWithItems:freezeButton, nil]; 
    powerUpMenu.position = ccp(winSize.width * 0.5, winSize.height * 0.1); 
    [self addChild:powerUpMenu]; 
} 

-(id)init 
{ 
    if((self = [super init])) 
    { 
     [self createPowerUpButtons]; 
    } 
    return self; 
} 

ActionLayer.h

#import "HUDLayer.h" 
#import "GameObject.h" 

enum Actions 
{ 
    kTagAsteroidAction = 1 
}; 
@interface ActionLayer : CCLayer 
{ 
    GameObject *asteroid; 
} 

ActionLayer.mm

-(void)updateAsteroids:(ccTime)dt 
{ 
    // --------------------------------------------- 
    // Code to set random sizes and speeds for the asteroids from the array... 
    // --------------------------------------------- 

     // Move it offscreen to the left, and when it's done, call removeNode 
     id action = [CCSequence actions: 
        [CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width - asteroid.contentSize.width, 0)], 
        [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)], nil]; 
     [action setTag:kTagAsteroidAction]; 

     [asteroid runAction:[CCSpeed actionWithAction:action speed:1.0f]]; 

     [self schedule:@selector(freezeTime:) interval:1.0f]; 
    } 
} 

-(void)update:(ccTime)dt 
{ 
    [self updateAsteroids:dt]; 
} 

我試圖得到儘可能接近的EaseActionsTest例子,因爲我可以,但我不斷收到關於啓動此錯誤:

'+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil' 

我認爲這是因爲 - (void)freezeTime:(ccTime)dt,因爲在此之前,我最初嘗試只用 - (void)freezeTime;我會收到我在freezeTime方法中設置的CCLog,發現它被調用,但小行星的速度不受影響。

而且,我想:

-(void)freezeTime 
{ 
    id action = [asteroid getActionByTag:kTagAsteroidAction]; 
    [action setSpeed:0.1f]; 
    CCLOG(@"Freeze!"); 
} 

-(id)init 
{ 
    if((self = [super init])) 
    { 
     [self freezeTime]; 
    } 
    return self; 
} 

我只注意到速度不在這裏要麼影響。

關於我能做些什麼的任何想法?任何有用的想法總是感激!

回答

0

嗯...我想這是一個錯誤,當你決定在你的小行星上運行這個動作的時候。行動應該會隨着時間的推移而起作用,並且您通常會一次運行該操作,並等待它完成。我看到你正在向小行星EVERY FRAME添加行動,這可能是問題所在。我會改變函數的名字以及它被調用的次數。

-(void)createAsteroidMovement: (GameObject*) asteroid 
{ 
    // --------------------------------------------- 
    // Code to set random sizes and speeds for the asteroids from the array... 
    // --------------------------------------------- 

     // Move it offscreen to the left, and when it's done, call removeNode 
     id action = [CCSequence actions: 
        [CCMoveBy actionWithDuration:randDuration position:ccp(-winSize.width - asteroid.contentSize.width, 0)], 
       [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)], nil]; 
     [action setTag:kTagAsteroidAction]; 

     [asteroid runAction:[CCSpeed actionWithAction:action speed:1.0f]]; 

     [self schedule:@selector(freezeTime:) interval:1.0f]; 
    } 
} 

-(void)update:(ccTime)dt 
{ 
    if (!_createdAsteroidMovement) 
    { 
     [self createAsteroidMovement: asteroid]; 
     _createdAsteroidMovement = true; 
    } 
}