0

应用程序使用以下方法响应触摸 - 调用 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 这样运行动作的每一帧的东西。

4

1 回答 1

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;

同上。没试过,ymmv

于 2013-06-30T23:12:31.540 回答