根据加速度值在横向模式下移动CCSprite(cocos2d)对象
我正在准备一款游戏,我想根据加速度值移动一个对象,游戏处于横向模式。
对于游戏,我使用 cocos2d 框架,并且根据加速度值更改精灵位置,这是我的加速度计代码,
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{
static float prevX=0, prevY=0, prevZ=0;
float accelX = acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
float accelZ = acceleration.z * kFilterFactor + (1- kFilterFactor)*prevZ;
prevX = accelX;
prevY = accelY;
prevZ = accelZ;
NSLog(@"x:%.2f,y:%.2f,z:%.2f",accelX, accelY, accelZ);
if ( ((player.position.x + (-accelY*kSpeed)) >0 && (player.position.x + (-accelY*kSpeed))<480)||
((player.position.y + (accelX*kSpeed)) >0 && (player.position.y + (accelX*kSpeed))<320)){
player.position = ccp(player.position.x + (-accelY*kSpeed), player.position.y + (accelX*kSpeed));
}
CGPoint converted = ccp( (float)-acceleration.y, (float)acceleration.x);
// update the rotation based on the z-rotation
// the sprite will always be 'standing up'
player.rotation = (float) CC_RADIANS_TO_DEGREES( atan2f( converted.x, converted.y) + M_PI );
}
其中 player
是 CCSprite 对象,Player 根据设备方向旋转,但它确实不根据设备方向改变位置。我做错了什么?是不是在横向模式下,x 轴的行为与 y 轴的行为相同,y 轴的行为与 x 的行为相同?
I am preparing one game for which I want to move one object according to acceleration values, Game is in landscape mode.
For game, I am using cocos2d framework, and I am changing sprite position as per acceleration values, Here is my code of accelerometer
- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{
static float prevX=0, prevY=0, prevZ=0;
float accelX = acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
float accelZ = acceleration.z * kFilterFactor + (1- kFilterFactor)*prevZ;
prevX = accelX;
prevY = accelY;
prevZ = accelZ;
NSLog(@"x:%.2f,y:%.2f,z:%.2f",accelX, accelY, accelZ);
if ( ((player.position.x + (-accelY*kSpeed)) >0 && (player.position.x + (-accelY*kSpeed))<480)||
((player.position.y + (accelX*kSpeed)) >0 && (player.position.y + (accelX*kSpeed))<320)){
player.position = ccp(player.position.x + (-accelY*kSpeed), player.position.y + (accelX*kSpeed));
}
CGPoint converted = ccp( (float)-acceleration.y, (float)acceleration.x);
// update the rotation based on the z-rotation
// the sprite will always be 'standing up'
player.rotation = (float) CC_RADIANS_TO_DEGREES( atan2f( converted.x, converted.y) + M_PI );
}
where player
is CCSprite object, Player is rotated as per device orientation, but it does not change position as per device orientation. What am I doing wrong? Is it that in landscape mode, x axis behaves as y and y axis behaves as x?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
加速度 x、y、z 值与 iPhone 的描绘模式一致。在其他模式下,您必须根据以下代码重新映射轴:
在 Kobold2D 中加速度值已经映射到当前设备方向,因此您不必考虑这一点。
The acceleration x, y, z values are aligned to the iPhone's portray mode. In other modes you will have to remap the axes according to this code:
In Kobold2D acceleration values are already mapped to the current device orientation, so that you don't have to consider this.