ios - Cocos2d - 在 CCSprite 实例上调用 CCMoveTo 操作期间进行 TMX 碰撞检查

标签 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 将 Sprite 移动到触摸处。

- (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];
        }
    }

每当 Sprite 触及边界时, Action 就会停止, Sprite 就会被卡在那里,因为当 Sprite 仍在边界内时,无论何时开始, Action 都会立即停止。 我尝试设置碰撞方法以返回一个 bool 值,然后在 CCMoveTo 中对其进行测试。有没有办法在 CCAction 的每次迭代中调用选择器?像 CCCallBlockN 这样的东西运行每一帧的 Action 。

最佳答案

嗯,我可能会在 CCAction 持续时间内安排一个选择器,以进行每帧回调。假设您还在玩家 Sprite 上运行某种动画,并且是 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 引用。没试过,ymmv

关于ios - Cocos2d - 在 CCSprite 实例上调用 CCMoveTo 操作期间进行 TMX 碰撞检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17395093/

相关文章:

iOS:在不请求位置授权的情况下使 "Location"出现在应用程序设置中(Tile 应用程序示例)

ios - swift 3 谷歌地图 : _OBJC_CLASS_$_GMSServices error

iphone - 按下 Home 时使用 CCGLView 的 EXC_BAD_ACCESS

iphone - 如何更改 Cocos2d MenuItem 的颜色?

ios - HttpClient setReachabilityStatusChangedBlock 声明没有接口(interface)

objective-c - UIPickerView 中不同大小的多个组件/列

objective-c - IOS 与 DB 在线身份验证

ios - Sharekit+Cocos2d Facebook 图像共享屏幕在横向模式下位置不正确

python - cocos.layer 包在 PyDev 中被报告为不存在

ios - Cocos2D 2.0 提高调度精度