Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocos2d iphone Cocos2d加速度计景观_Cocos2d Iphone_Accelerometer_Landscape - Fatal编程技术网

Cocos2d iphone Cocos2d加速度计景观

Cocos2d iphone Cocos2d加速度计景观,cocos2d-iphone,accelerometer,landscape,Cocos2d Iphone,Accelerometer,Landscape,我很难用cocos2d让加速计在横向模式下正常工作。一切都在肖像中完美地工作,但我不知道如何翻转它以正确地在风景中工作。这是我的密码: -(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { float deceleration = 0.0f; float sensativity = 15.0f; float maxVelocity = 100;

我很难用cocos2d让加速计在横向模式下正常工作。一切都在肖像中完美地工作,但我不知道如何翻转它以正确地在风景中工作。这是我的密码:

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{

float deceleration = 0.0f;
float sensativity = 15.0f;
float maxVelocity = 100;

playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensativity;
playerVelocity.y = playerVelocity.y * deceleration + acceleration.y * sensativity;

if (playerVelocity.x > maxVelocity)
{
    playerVelocity.x = maxVelocity;
}
else if (playerVelocity.x < - maxVelocity)
{
    playerVelocity.x = - maxVelocity;
}

if (playerVelocity.y > maxVelocity)
{
    playerVelocity.y = maxVelocity;
}
else if (playerVelocity.y < - maxVelocity)
{
    playerVelocity.y = - maxVelocity;
}

}

-(void) update:(ccTime) delta
{
CGPoint pos = player.position;

pos.x += playerVelocity.x;
pos.y += playerVelocity.y;

CGSize screeSize = [[CCDirector sharedDirector]winSize];

float imageHalf = [player texture].contentSize.width * 0.5f;

float leftLimit = imageHalf;
float rightLimit = screeSize.width - imageHalf -20;

float bottomLimit = imageHalf;
float topLimit = screeSize.height - imageHalf - 20;

if (pos.y < bottomLimit) {
    pos.y = bottomLimit;
}
else if (pos.y > topLimit){
    pos.y = topLimit;
}

if (pos.x < leftLimit) {
    pos.x = leftLimit;
}
else if (pos.x > rightLimit){
    pos.x = rightLimit;
}

player.position = pos;
}
-(无效)加速计:(UIAccelerator*)加速计DID加速度:(UIAcceleration*)加速度
{
浮动减速度=0.0f;
浮动灵敏度=15.0f;
浮点最大速度=100;
playerVelocity.x=playerVelocity.x*减速+加速.x*灵敏度;
playerVelocity.y=playerVelocity.y*减速+加速.y*灵敏度;
if(playerVelocity.x>maxVelocity)
{
playerVelocity.x=最大速度;
}
else if(playerVelocity.x<-maxVelocity)
{
playerVelocity.x=-maxVelocity;
}
if(playerVelocity.y>maxVelocity)
{
playerVelocity.y=最大速度;
}
else if(playerVelocity.y<-maxVelocity)
{
playerVelocity.y=-maxVelocity;
}
}
-(无效)更新:(ccTime)增量
{
CGPoint pos=玩家位置;
pos.x+=playerVelocity.x;
位置y+=游戏性y;
CGSize screeSize=[[CCDirector sharedDirector]winSize];
float imageHalf=[player纹理].contentSize.width*0.5f;
float leftLimit=imageHalf;
float rightLimit=screeSize.width-imageHalf-20;
浮动下限=图像的一半;
float topLimit=screeSize.height-imageHalf-20;
如果(位置y<底部极限){
位置y=底部极限;
}
否则,如果(位置y>上限){
位置y=上限;
}
如果(位置x<左极限){
位置x=左极限;
}
否则如果(位置x>右侧限制){
位置x=右极限;
}
player.position=pos;
}

在横向中,加速计的x和y值被翻转。此外,在界面方向的横向右侧,x为负值。在左侧横向中,y为负值:

// landscape right:
position.x = -acceleration.y
position.y = acceleration.x

// landscape left:
position.x = acceleration.y
position.y = -acceleration.x

在横向中,加速计x和y值被翻转。此外,在界面方向的横向右侧,x为负值。在左侧横向中,y为负值:

// landscape right:
position.x = -acceleration.y
position.y = acceleration.x

// landscape left:
position.x = acceleration.y
position.y = -acceleration.x
看下面的答案

在cocos2d游戏中使用加速计的最佳方式。

请查看中的答案


在cocos2d游戏中使用加速计的最佳方式。

谢谢,我知道我很接近,我尝试了很多组合来翻转它,结果把自己弄得一团糟!谢谢,我知道我很接近,我尝试了很多组合来翻转它,结果把我自己弄得一团糟!