ios - 更改不同类的 CCSpeed Action ?

标签 ios cocos2d-iphone freeze

所以我正在尝试将 Cocos2D 示例中的 EaseActionsTest 示例中的 alterTime 方法实现到我的游戏中。

我正在尝试将这个想法实现到我的游戏中,为我的游戏创建一个卡住时间类型的能量提升。我已经设置了一个带有 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;
}

我刚刚注意到这里的速度也没有受到影响。

关于我可以做些什么来完成这项工作有什么想法吗?任何有用的想法都会受到赞赏!

最佳答案

嗯...我认为当您决定对您的小行星执行操作时存在错误。 Action 应该随着时间的推移而起作用,您通常会运行一次 Action ,然后等待它完成。我看到您正在为每一帧的小行星添加 Action ,这可能是问题所在。我会更改函数的名称及其调用次数。

-(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;
     }
}

关于ios - 更改不同类的 CCSpeed Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9848047/

相关文章:

java - 为什么 java 程序会在 113882 上卡住?

objective-c - Objective C - 无法在一种方法中设置值并从另一种方法访问它

android - Havok 物理引擎 - Android

objective-c - CCRenderTexture的renderTextureWithWidth在cocos2d v3.2.1+中编译失败

c++ - 在 VB.NET 中故意卡住应用程序

PHP 在对大数组使用 for 循环时卡住

ios - UIButton 以及 IBAction 和 segue 的顺序

ios - cancelLocalNotification 不起作用

iphone - 播放 png 序列

cocos2d-iphone - Cocos2d : How to play a video in the background of a CCLayer