小编Ach*_*hta的帖子

noskipws对cin的影响>>

据我所知,提取运算符在开始时跳过空白,并在遇到空白或流结束时停止.noskipws可用于停止忽略前导空格.

我有以下程序,我使用过noskipws.

#include <iostream>
using namespace std;

int main()
{
    char name[128];

    cout<<"Enter a name ";
    cin>>noskipws>>name;
    cout<<"You entered "<<name<<"\n";

    cout<<"Enter another name ";
    cin>>name;
    cout<<"You entered "<<(int)name[0]<<"\n";

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我的疑问是:

  1. 如果我输入"John"作为第一个输入,那么第二个cin >>操作不会等待输入,也不会将任何内容复制到目标,即名称数组.我希望第二个cin >>至少传输一个换行符或者一个换行符,而不是仅仅将目标字符串设置为空.为什么会这样?

  2. 当我输入"John Smith"作为第一个cin >>语句的输入时,会观察到同样的事情.为什么第二个cin >>语句没有将空格或"Smith"复制到目标变量?

以下是该计划的输出:

Enter a name John
You entered John
Enter another name You entered 0


Enter a name John Smith
You entered John
Enter another name You entered 0
Run Code Online (Sandbox Code Playgroud)

谢谢!!!

c++ stream manipulators

6
推荐指数
1
解决办法
8428
查看次数

在其中心周围旋转精灵

我试图弄清楚如何在Draw方法中使用原点来围绕其中心旋转精灵.我希望有人可以解释Draw方法中origin参数的正确用法.

如果我使用以下Draw方法(没有指定任何旋转和原点),则在正确/预期的位置绘制对象:

spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

但是,如果我使用如下所示的原点和旋转,则对象围绕中心旋转但对象浮动在预期位置上方(大约20像素).

Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 );
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

即使我将ballRotation设置为0,对象仍然被绘制在预期位置之上

spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, 0.0f, origin, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

似乎只是通过设置原点,对象的位置会发生变化.

有人可以告诉我如何正确使用origin参数.


解:

Davor的回应使原点的使用变得清晰.代码中需要进行以下更改才能使其正常工作:

Vector2 origin = new Vector2(myTexture.Width / 2 , myTexture.Height / 2 );
destinationRectangle.X += destinationRectangle.Width/2;
destinationRectangle.Y += destinationRectangle.Height / 2;
spriteBatch.Draw(myTexture, destinationRectangle, null, Color.White, ballRotation, origin, SpriteEffects.None, 0);
Run Code Online (Sandbox Code Playgroud)

xna xna-4.0

5
推荐指数
1
解决办法
6849
查看次数

标签 统计

c++ ×1

manipulators ×1

stream ×1

xna ×1

xna-4.0 ×1