0
私はiPhoneゲームを作ろうとしていますが、ボールを特定のパスで転がし、特定のオブジェクトに触れることはできません。加速度計でプレイヤーの動きに合わせてタイルセットのコライダーを作る方法iOS、cocos2d
私はタイルコライダーの作り方を教えてくれたが、isTouchEnabledを使ってボールを動かすと動くが、同じロジックを加速度計に適用すると登録されていないようだ。
これはタッチプレイヤーの移動方法です。ちょうどあなたがどのように動作するかを見ることができます。以下は
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
CGPoint playerPos = ball.position;
CGPoint diff = ccpSub(touchLocation, playerPos);
//move horizontal or vertical?
if(abs(diff.x) > abs(diff.y)){
if(diff.x > 0)
{
playerPos.x += theMap.tileSize.width;
}else{
playerPos.x -= theMap.tileSize.width;
}
}else{
if(diff.y > 0){
playerPos.y += theMap.tileSize.height;
}else{
playerPos.y -= theMap.tileSize.height;
}
}
//make sure the player isnt off the map
if(playerPos.x <= (theMap.mapSize.width * theMap.tileSize.width) &&
playerPos.y <= (theMap.mapSize.height * theMap.tileSize.height) &&
playerPos.x >=0 &&
playerPos.y >=0)
{
[self setPlayerPostion:playerPos];
}
[self setCenterOfScreen:ball.position];
}
それはボールの位置を取得し、ここで加速度計
-(void)updateBall:(ccTime)delta{
//ball.position = ccp(ball.position.x, ball.position.y + tiltVertical);
//ball.position = ccp(ball.position.x + tiltHorizontal, ball.position.y);
CGPoint playerPos = ball.position;
playerPos.x += tiltHorizontal;
playerPos.y += tiltVertical;
ball.position = playerPos;
//make sure the player isnt off the map
if(playerPos.x <= (theMap.mapSize.width * theMap.tileSize.width) &&
playerPos.y <= (theMap.mapSize.height * theMap.tileSize.height) &&
playerPos.x >=0 &&
playerPos.y >=0)
{
ball.position = playerPos;
CCLOG(@"testing for off screen");
[self setPlayerPostion:playerPos];
ball.position = playerPos;
}
ball.position = playerPos;
[self setCenterOfScreen:ball.position];
}
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
tiltHorizontal = (acceleration.y* (-20));
tiltVertical = (acceleration.x) * 20;
}
を使用してそれを移動する方法であるタイルの位置を取得し、タイルの性質あれば見に使用されている2つの方法ですCollidableとtrueです(マップを作成したときにタイル表示されます)。
-(void)setPlayerPostion:(CGPoint)position{
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [stLayer tileGIDAt:tileCoord];
if(tileGid)
{
NSDictionary *properties = [theMap propertiesForGID:tileGid];
if(properties){
NSString *collision = [properties valueForKey:@"Collidable"];
if(collision && [collision compare:@"True"] ==NSOrderedSame){
return;
}
}
}
ball.position = position;
}
-(CGPoint)tileCoordForPosition:(CGPoint)position{
int x = position.x/theMap.tileSize.width;
int y = ((theMap.mapSize.height * theMap.tileSize.height)- position.y)/theMap.tileSize.height;
return ccp(x,y);
}
ロジックに問題はありますか?または任意の提案をいただければ幸いです。
おかげ
ええ、私はそれを持っています、そしてボールは私の地図の周りを転がり、それが衝突可能なタイルを検出しないと予想します。 – Peerkon
ステップバイステップでデバッガを追加しようとしましたか?「プロパティ」に問題がありますか? – tams