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