所以我每秒在随机位置创建一个精灵。我想要的是精灵朝向屏幕的中心。例如,如果我每秒在随机位置创建一个人体,我希望他的头部朝向屏幕中心。谢谢 :) 对不起我的英语我是法国人 :/
问问题
479 次
2 回答
0
CGPoint screenCenter = ...; // set it manually or from device screen size
CGPoint direction = ccpSub (screenCenter, yourHuman.position);
yourHuman.rotation = CC_RADIANS_TO_DEGREE (ccpToAngle (direction));
我不确定旋转角度,所以也许你需要
yourHuman.rotation = -CC_RADIANS_TO_DEGREE (ccpToAngle (direction));
于 2012-05-02T14:49:23.267 回答
0
CCPoint pos1 = [yourHuman position];
CCPoint pos2 = screenCenter;
float theta = atan((pos1.y-pos2.y)/(pos1.x-pos2.x)) * 180 * 7 /22;
if(pos1.y - pos2.y > 0)
{
if(pos1.x - pos2.x < 0)
{
[yourHuman setRotation:(-90-theta)];
}
else if(pos1.x - pos2.x > 0)
{
[yourHuman setRotation:(90-theta)];
}
}
else if(pos1.y - pos2.y < 0)
{
if(pos1.x - pos2.x < 0)
{
[yourHuman setRotation:(270-theta)];
}
else if(pos1.x - pos2.x > 0)
{
[yourHuman setRotation:(90-theta)];
}
}
这个完成了。将此代码保存在方法中。并使用它.. :) 希望这会有所帮助..
于 2012-05-02T18:29:30.617 回答