因此,我试图将Cocos2D示例中的EaseActionsTest示例中的alterTime方法实现到我的游戏中。
我正在尝试将这种想法应用到我的游戏中,以为游戏创建冻结时间类型的通电。我用CCMenu设置了一个单独的HUDLayer,将小行星的CCSpeed更改为0以停止移动。我已经设置了CCLog以确保正在调用我的freezeTime方法,但是确实如此,但是小行星的CCSpeed仍然不受影响。这是我的代码,更加清晰。
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;
}
动作层
-(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;。我会收到在FrozenTime方法中设置的CCLog,看到它正在被调用,但是小行星的速度没有受到影响。
另外,我尝试了:
-(void)freezeTime
{
id action = [asteroid getActionByTag:kTagAsteroidAction];
[action setSpeed:0.1f];
CCLOG(@"Freeze!");
}
-(id)init
{
if( (self = [super init]) )
{
[self freezeTime];
}
return self;
}
我只是注意到这里的速度也不受影响。
关于我可以做些什么的任何想法?任何有用的想法总是很感激!
最佳答案
嗯...我认为当您决定对小行星进行操作时,存在一个错误。动作应该随着时间的推移而起作用,通常您需要运行一次动作,然后等待动作完成。我看到您在每个小行星上都在向小行星添加动作,这可能是问题所在。我将更改函数的名称以及其调用的次数。
-(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行动?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9848047/