应用程序使用以下方法响应触摸 - 调用 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/