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