Ios 运行状况栏的CCProgressNode不工作
我最近发现在最新版本的cocos2d中,CCProgressTimer类被CCProgressNode替换, 但是,当我尝试实现以下代码时,progressNode没有发生任何变化 我已经阅读了各种各样的文档,似乎我使用了所有最新的方法。 这一切都发生在游戏课上 这就是我定义节点的方式Ios 运行状况栏的CCProgressNode不工作,ios,objective-c,cocos2d-iphone,spritebuilder,Ios,Objective C,Cocos2d Iphone,Spritebuilder,我最近发现在最新版本的cocos2d中,CCProgressTimer类被CCProgressNode替换, 但是,当我尝试实现以下代码时,progressNode没有发生任何变化 我已经阅读了各种各样的文档,似乎我使用了所有最新的方法。 这一切都发生在游戏课上 这就是我定义节点的方式 CCProgressNode *_healthBar; float _life; 这是设置方法 - (void)initializeHealthBar { self->_healthBar = [
CCProgressNode *_healthBar;
float _life;
这是设置方法
- (void)initializeHealthBar {
self->_healthBar = [[CCProgressNode alloc] init]; // This wasn't there before, but thought it might be the memory allocation problem,
// _health is the code connection between sprite builder and Xcode.
self->_healthBar = [CCProgressNode progressWithSprite:_health];
[_healthBar setType:CCProgressNodeTypeBar];
[_healthBar setPercentage:_life];
[_healthBar setBarChangeRate:ccp(0.1,0.1)];
_healthBar.position = _health.position;
// So the _healthBar turns out positioned correctly, because _health is already positioned in sprite builder
[_contentNode addChild:_healthBar];
}
这就是我在健康栏上的改变。。。(工作正常,healthBar正在耗尽…)
-(无效)命中{
如果(_healthBar.percentage>0)
{
自我寿命=34;
自身->\u healthBar.percentage-=34;
}
如果(self->\u healthBar.percentage\u healthBar.percentage=0;
[[u myHero isGameOver:TRUE];
[自我放逐];
}
}
我不完全理解您的问题,我刚刚为左->右进度条类型的进度条编写了一个小的工作示例
- (void) onEnter
{
[super onEnter];
CCSprite *sprite = [CCSprite spriteWithImageNamed:@"emma.png"];
_progressNode = [CCProgressNode progressWithSprite:sprite];
_progressNode.type = CCProgressNodeTypeBar;
_progressNode.midpoint = ccp(0.0f, 0.0f);
_progressNode.barChangeRate = ccp(1.0f, 0.0f);
_progressNode.percentage = 0.0f;
_progressNode.positionType = CCPositionTypeNormalized;
_progressNode.position = ccp(0.5f, 0.5f);
[self addChild:_progressNode];
self.userInteractionEnabled = YES;
}
- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
_progressNode.percentage += 10.0f;
}
请注意,CCSprite
未添加到场景中,恐怕您不能将SpriteBuilder
用于该场景。(除非您想将其从父对象中删除,但这会有点混乱)
另外,在调用百分比设置器之前,请执行所有设置
而
百分比实际上是一个双倍的。始终检查以确保没有发生铸造问题。在obj c中,你不使用self->\u life
作为ivar
。你只需像life
一样引用它们。你也可以调用alloc init
,然后用pro创建一个新对象gressWithSprite本质上创建了两个对象。第一个对象被删除,但仍然是。我正在编辑您的问题,使您的代码和文本更通用,以便人们可以关注代码的CCProgressBar
部分。谢谢!它成功了,我想我还没有完成中点和barChangeRate的设置,也没有从文件中加载sprite直接。感谢您的快速回复。欢迎您,请务必查看这些文件的标题,它们通常解释类的功能,您可以看到公共API,它可以让您了解下一次如何解决问题。这始终是我检查的第一个地方。没有比代码本身更好的文档了!
- (void) onEnter
{
[super onEnter];
CCSprite *sprite = [CCSprite spriteWithImageNamed:@"emma.png"];
_progressNode = [CCProgressNode progressWithSprite:sprite];
_progressNode.type = CCProgressNodeTypeBar;
_progressNode.midpoint = ccp(0.0f, 0.0f);
_progressNode.barChangeRate = ccp(1.0f, 0.0f);
_progressNode.percentage = 0.0f;
_progressNode.positionType = CCPositionTypeNormalized;
_progressNode.position = ccp(0.5f, 0.5f);
[self addChild:_progressNode];
self.userInteractionEnabled = YES;
}
- (void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
_progressNode.percentage += 10.0f;
}