如何通过加速度计iOS,cocos2d制作与玩家移动的瓷砖碰撞器

时间:2012-03-10 19:06:09

标签: objective-c ios cocos2d-iphone collision-detection accelerometer

我正在尝试使用cocos2d制作iPhone游戏,您可以在指定的路径上滚动球并且无法触摸某些物体。

我做了一个RPG教程,教我如何制作瓷砖碰撞器,当我使用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;


 }

以下是两种方法,用于获取图块位置并查看图块的属性是否为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); 
   }

我的逻辑有问题吗?或任何建议将不胜感激。

由于

1 个答案:

答案 0 :(得分:0)

确保在init方法中有行self.isAccelerometerEnabled = YES