Cocos2d iphone 动画工作不正常(Cocos2d)
我的问题是,在精灵移动期间,动画无法正常工作。 下面是我正在使用的代码Cocos2d iphone 动画工作不正常(Cocos2d),cocos2d-iphone,Cocos2d Iphone,我的问题是,在精灵移动期间,动画无法正常工作。 下面是我正在使用的代码 - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { [selSprite resumeSchedulerAndActions]; CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; [self selectSpriteForTouch:to
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
[selSprite resumeSchedulerAndActions];
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE;
}
- (void)selectSpriteForTouch:(CGPoint)touchLocation
{
CCSprite * newSprite = nil;
for (CCSprite *sprite in movableSprite) {
if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) {
newSprite = sprite;
break;
}
}
if (newSprite != selSprite) {
[selSprite stopAllActions];
selSprite = newSprite;
_MoveableSpritetouch = TRUE;
}
if(_MoveableSpritetouch==TRUE)
{
movement=0;
CGRect selRect=CGRectMake((SpriteX)-20.0,(SpriteY)-20.0,40.0,40.0);
if(CGRectContainsPoint(selRect, touchLocation))
{
[selSprite stopAllActions];
}
if((selSprite==MarshallCar)&& (!(CGRectContainsPoint(selRect, touchLocation))))
{
movement=1;
[self reorderChild:selSprite z:5];
NSMutableArray *MarshallCarWalkAnimFrames = [NSMutableArray array];
for(int i = MarshallCarTouchStartFrameIndex; i <= MarshallCarTouchEndFrameIndex; ++i) {
[MarshallCarWalkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"mcar_move_%d.png", i]]];
}
MarshallCarWalkAnim = [CCAnimation animationWithFrames:MarshallCarWalkAnimFrames delay:MarshallCarTouchFrameDelay];
walkMarshallCar = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:MarshallCarWalkAnim restoreOriginalFrame:NO]];
[selSprite runAction:walkMarshallCar];
}
}
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
if(gameState == TRUE){
CGPoint point = [touch locationInView:[touch view]];
point = [[CCDirector sharedDirector] convertToGL:point];
if (moveDifference.x>0)
{
selSprite.flipX = YES;
}
else {
selSprite.flipX = NO;
}
[selSprite setPosition:point];
}
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
movement=0;
if(selSprite==MarshallCar)
{
[selSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"mcar_idle.png"]];
}
[selSprite pauseSchedulerAndActions];
}
-(BOOL)cctouch开始:(UITouch*)触摸事件:(UIEvent*)事件{
[selSprite resumeSchedulerAndActions];
CGPoint touchLocation=[自转换touchtonodespace:touch];
[自选SpriteForTouch:触摸位置];
返回TRUE;
}
-(无效)选择spritefortouch:(CGPoint)触摸位置
{
CCSprite*newSprite=nil;
用于(CCSprite*可移动sprite中的sprite){
如果(CGRectContainsPoint(sprite.boundingBox,touchLocation)){
纽斯普里特=雪碧;
打破
}
}
if(newSprite!=selSprite){
[selSprite stopAllActions];
selSprite=newSprite;
_MoveableSpritetouch=TRUE;
}
if(_MoveableSpritetouch==TRUE)
{
移动=0;
CGRect selRect=CGRectMake((SpriteX)-20.0,(SpriteY)-20.0,40.0,40.0);
if(CGRectContainsPoint(selRect,touchLocation))
{
[selSprite stopAllActions];
}
如果((selSprite==MarshallCar)和(!(CGRectContainsPoint(selcert,touchLocation)))
{
运动=1;
[self-reordchild:selSprite z:5];
NSMutableArray*MarshallCarWalkAnimFrames=[NSMutableArray];
对于(int i=MarshallCarTouchStartFrameIndex;i 0)
{
selSprite.flipX=是;
}
否则{
selSprite.flipX=否;
}
[selSprite设置位置:点];
}
}
-(无效)ccTouchEnded:(UITouch*)触摸事件:(UIEvent*)事件
{
移动=0;
if(selSprite==MarshallCar)
{
[selSprite setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@“mcar_idle.png”];
}
[selSprite pauseSchedulerAndActions];
}
在移动过程中,移动的动画帧不是每次都播放,有时播放,有时不播放。当您第一次触摸并移动精灵时,动画帧可以正常播放,但如果触摸另一个精灵,然后再次移动前一个精灵,则移动的动画将不会播放
有人知道为什么会这样吗?
请告诉我删除此错误的正确代码。
谢谢!!!我相信您的问题在于if/else if构造:
if (_MoveableSpritetouch==TRUE)
{
CGRect selRect = CGRectMake(SpriteX - 20, SpriteY - 20, 40, 40);
if(CGRectContainsPoint(selRect, touchLocation))
{
[selSprite stopAllActions];
}
else if(...)
{
...
[selSprite runAction:walkMarshallCar];
}
}
如果您没有立即看到:如果触摸位置在selRect内,则对选定(新)精灵调用stopAllActions,而不执行其他操作。只有当触摸位置不在该矩形内时,才会运行动画操作
我认为“矩形内”检查是多余的,因为上面几行您已经调用了stopAllActions
请允许我对您的代码作一些一般性评论:
“selectSpriteForTouch”方法告诉我您正在选择一个新的精灵。该函数会这样做。但它不会宣传播放动画。您可能希望将其重构为单独的“PlayAnimationSelectedSprite”方法
您多次编写了20.0和40.0。这意味着您实际上是在将一个双精度(8字节浮点数据类型)传递给一个采用浮点(4字节浮点)的CGPoint。严格地说,可以使用带有后缀“f”的20.0f将其表示为浮点数据类型,也可以使用整数,因为您不使用浮点部分
我不清楚为什么要将(SpriteX)放在括号中,如果想提高可读性,可以在逗号和操作数后添加空格
在Objective-C中,使用YES和NO宏,而不是TRUE和FALSE
bool变量_MoveableSpritetouch似乎是多余的,除非需要其他地方。在任何情况下,您都应该将以下if(_MoveableSpritetouch==TRUE)块移动到您将_MoveableSpritetouch变量设置为TRUE的位置,因为它只会通过设置一个变量使代码更难读取,而保留您所在的代码块(if(selSprite!=newSprite))只需跳转到另一个代码块(如果(_MoveableSpritetouch==TRUE)),您已经知道您无论如何都要运行该代码块。if((selSprite==MarshallCar)&&(!(CGRectContainsPoint(selcert,touchLocation)))
if((selSprite==MarshallCar)&& (!(CGRectContainsPoint(selRect, touchLocation))))
{
movement=1;
[self reorderChild:selSprite z:5];
NSMutableArray *MarshallCarWalkAnimFrames = [NSMutableArray array];
for(int i = MarshallCarTouchStartFrameIndex; i <= MarshallCarTouchEndFrameIndex; ++i) {
[MarshallCarWalkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"mcar_move_%d.png", i]]];
}
MarshallCarWalkAnim = [CCAnimation animationWithFrames:MarshallCarWalkAnimFrames delay:MarshallCarTouchFrameDelay];
walkMarshallCar = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:MarshallCarWalkAnim restoreOriginalFrame:NO]];
[selSprite runAction:walkMarshallCar];
}
{
运动=1;
[self-reordchild:selSprite z:5];
NSMutableArray*MarshallCarWalkAnimFrames=[NSMutableArray];
对于(int i=MarshallCarTouchStartFrameIndex;我已经通过[if((selSprite==MarshallCar)&&(!(cRectContainsPoint(selRect,touchLocation)]编辑了代码,现在告诉我问题出在哪里。