Cocos2d - 平铺地图RPG中的平滑精灵移动

时间:2012-11-21 04:48:10

标签: iphone objective-c animation cocos2d-iphone sprite

我现在已经在制作2-D Gameboy风格的RPG了一段时间,游戏逻辑大部分都已完成,所以我开始尝试让事情看起来不错。我注意到的一件事是步行运动/屏幕运动有点不稳定。从技术上讲,它应该可以正常工作,但要么它似乎有一些怪癖,要么是由于占用了大量的处理能力,要么只是在移动屏幕和移动精灵之间的时间不一致。

要移动精灵,一旦我知道我想要移动它,我就打电话:

tempPos.y += 3*theHKMap.tileSize.width/5; 
id actionMove = [CCMoveTo actionWithDuration:0.1 position:tempPos]; 
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(orientOneMove)];
[guy runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
[self setCenterOfScreen:position];

然后,在orientOneMove中,我打电话给:

[self.guy setTexture:[[CCTextureCache sharedTextureCache] addImage:@"guysprite07.png"]]; //the walking picture-I change texture back at the end of the movement
id actionMove = [CCMoveTo actionWithDuration:0.15 position:self.tempLocation2];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(toggleTouchEnabled)];
[guy runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];

同时运行的setCenterOfScreen:position方法的代码是:

id actionMove = [CCMoveTo actionWithDuration:0.25 position:difference]; [self runAction:
[CCSequence actions:actionMove, nil, nil]];

所以setCenterOfScreen移动相机一个干净的移动,而移动的人被切成两个动作来动画它(我相信这可能是低效的。) 很难说是什么让运动看起来并不完全干净,但基本上这个人并不总是完美地位于屏幕的中央 - 在运动过程中,他经常会一瞬间的一两个像素。任何想法/解决方案?

1 个答案:

答案 0 :(得分:1)

使用可以解决问题的线程加载。请参阅此帖子:cocos2d-continuously-scrolling-tile-based-game可能是回答中的有用提示

[NSThread detachNewThreadSelector:@selector(loadTileMapInThread:) toTarget:self withObject:nil];


-(void)loadTileMapInThread:(id)argument
{
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
    CCGLView *view = (CCGLView*)[[CCDirector sharedDirector] view];
    EAGLContext *auxGLcontext = [[EAGLContext alloc]
                                 initWithAPI:kEAGLRenderingAPIOpenGLES2
                                 sharegroup:[[view context] sharegroup]];

    if( [EAGLContext setCurrentContext:auxGLcontext] ) {

        [self LoadTilesMap];

        glFlush(); 

        [EAGLContext setCurrentContext:nil];
    } else {
        CCLOG(@"cocos2d: ERROR: TetureCache: Could not set EAGLContext");
    }

    [auxGLcontext release];

    [autoreleasepool release];
}