Cocos2d - 在CCSprite实例上调用的CCMoveTo操作期间的TMX冲突检查

时间:2013-06-30 21:36:00

标签: ios cocos2d-iphone

应用程序使用以下方法响应触摸 - 调用movePlayer:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.player stopAllActions];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint diff = ccpSub(touchLocation, self.player.position);
self.distanceToMovePlayer = sqrtf((diff.x*diff.x)+(diff.y*diff.y));
self.playerDestination = touchLocation;
[self movePlayer];
}

movePlayer在这里定义。它运行CCAction,将精灵移动到触摸状态。

- (void)movePlayer{
CCAction *movePlayer = [CCMoveTo actionWithDuration:self.distanceToMovePlayer/100 position:self.playerDestination];
self.playerMovement = movePlayer;
[self.player runAction:self.playerMovement];

}

我在TMXTileMap上有一个名为meta的不可见的TMX图层,它使用以下每个帧运行的方法指示墙或边界:

- (void)checkCollisions:(CGPoint)position{
CGPoint tileCoordinate = [self tileCoordForPosition:position];
int tileGID = [self.meta tileGIDAt:tileCoordinate];
if(tileGID == 49){
    NSDictionary *properties = [self.meta properties];
    if(properties){
        NSString *collision = properties[@"Collidable"];
        if(collision && [collision isEqualToString:@"True"]){
            [self.player stopAction:self.playerMovement];
        }
    }

每当精灵触及一个边界时,动作就会停止,精灵就会停留在那里,因为当精灵仍然在边界时,动作会立即停止。 我已经尝试设置碰撞方法返回一个布尔值,然后在CCMoveTo中进行测试。有没有办法在每次CCAction迭代时调用选择器?像CCCallBlockN那样运行动作的每一帧。

1 个答案:

答案 0 :(得分:1)

好吧,我可能会为CCAction持续时间安排一个选择器,用于每帧回调。假设您还在播放器精灵上运行某种动画,并在cocos2d 2.0+中运行新动画,则可以使用CCAnimation,从而可以注册为每个帧提供一些用户数据的通知。

来自CCAnimation.h文件的

/**  A CCAnimationFrameDisplayedNotification notification will be broadcasted 
 *   when the frame is displayed with this dictionary as UserInfo. If UserInfo is nil, 
 *   then no notification will be broadcasted. */

@property (nonatomic, readwrite, retain) NSDictionary *userInfo;

ob cit。没试过,ymmv