GetWorld()->GetTimerManager().SetTimer 第二次之下后计时器会重置吗
时间: 2024-04-30 12:18:25 浏览: 8
不会,使用`SetTimer`函数设置的计时器不会重置。每次调用`SetTimer`函数都会创建一个新的计时器,并将其添加到计时器管理器中。如果你想要重置计时器,请使用计时器的`Restart`函数。例如:
```cpp
// 声明计时器变量
FTimerHandle MyTimerHandle;
// 设置计时器
GetWorld()->GetTimerManager().SetTimer(MyTimerHandle, this, &AMyActor::MyFunction, 10.0f, false);
// 重置计时器
GetWorld()->GetTimerManager().ClearTimer(MyTimerHandle);
GetWorld()->GetTimerManager().SetTimer(MyTimerHandle, this, &AMyActor::MyFunction, 10.0f, false);
```
在上面的例子中,我们首先声明了一个名为`MyTimerHandle`的计时器变量。然后,我们使用`SetTimer`函数设置了一个10秒的计时器,它将调用`MyFunction`函数。最后,我们使用`ClearTimer`函数清除了计时器,然后再次使用`SetTimer`函数设置了一个新的10秒计时器。这样就相当于重置了计时器。
相关问题
void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default pDC=GetDC(); center=point; r=rand()%46+5;//r=5~50 color=RGB(rand()%256,rand()%256,rand()%256); br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(center.x-r,center.y-r,center.x+r,center.y+r); br.DeleteObject(); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default SetTimer(1,200,NULL); CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { center.y-=5; pDC=GetDC(); br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(center.x-r,center.y-r,center.x+r,center.y+r); br.DeleteObject(); } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if (nIDEvent==1){ CDC *pDC=GetDC(); CRect rect; GetClientRect(&rect); for(int i=0;i<5;i++){ br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(center.x-r,center.y-r,center.x+r,center.y+r); br.DeleteObject();} ReleaseDC(pDC); center.y-=5; }怎么改能让这个圆没有运动轨迹
要让圆没有运动轨迹,可以在每次重绘之前,先用背景色覆盖整个视图区域,然后再绘制圆。修改代码如下:
```
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
pDC = GetDC();
center = point;
r = rand() % 46 + 5; //r=5~50
color = RGB(rand() % 256, rand() % 256, rand() % 256);
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
br.DeleteObject();
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
SetTimer(1, 200, NULL);
CView::OnLButtonUp(nFlags, point);
}
void CMyView::rise()
{
center.y -= 5;
pDC = GetDC();
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
br.DeleteObject();
}
void CMyView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent == 1) {
CDC* pDC = GetDC();
CRect rect;
GetClientRect(&rect);
pDC->FillSolidRect(rect, GetSysColor(COLOR_WINDOW)); // 用背景色覆盖整个视图区域
for (int i = 0; i < 5; i++) {
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
br.DeleteObject();
}
ReleaseDC(pDC);
center.y -= 5;
}
}
```
ue中这段代码什么意思//设置武器投掷时旋转是与人物水平的 FRotator MeshRotation(0.f, GetItemMesh()->GetComponentRotation().Yaw, 0.f); GetItemMesh()->SetWorldRotation(MeshRotation, false, nullptr, ETeleportType::TeleportPhysics); //获取向前和向右向量 FVector const MeshForward{ GetItemMesh()->GetForwardVector() }; FVector MeshRight{ GetItemMesh()->GetRightVector() }; //设置冲力的矢量 FVector ImpulseDirection = MeshRight.RotateAngleAxis(-20.f, MeshForward); float RandomRotation = FMath::FRandRange(15.f, 30.f); ImpulseDirection = ImpulseDirection.RotateAngleAxis(RandomRotation, FVector(0.f, 0.f, 1.f)); //设置冲量的力 ImpulseDirection *= 20000.f; GetItemMesh()->AddImpulse(ImpulseDirection); bFalling = true; GetWorldTimerManager().SetTimer(ThrowWeaponTimer,this,&AWeapon::StopFalling,ThrowWeaponTime);
这段代码的作用是设置武器投掷时的旋转和施加冲量的力。
首先,通过获取物品的Mesh组件的旋转角度,创建一个新的FRotator对象MeshRotation,并将其设置为只有Yaw轴方向有值,其他轴方向为0,这样可以保持武器的旋转与人物水平。
然后,获取Mesh组件的向前向量(MeshForward)和向右向量(MeshRight)。
接下来,通过将MeshRight向量绕MeshForward向量旋转-20度,得到一个新的ImpulseDirection矢量,用于设置投掷时的冲力的方向。
随后,通过使用FMath::FRandRange函数生成一个随机的旋转角度(RandomRotation),并将ImpulseDirection矢量绕Z轴旋转RandomRotation度。
然后,将ImpulseDirection矢量乘以20000.f,以设置冲量的力大小。
接下来,使用AddImpulse函数将计算得到的ImpulseDirection矢量施加到物品的Mesh组件上,以产生冲力使武器投掷起来。
最后,设置一个定时器(ThrowWeaponTimer),在一定时间后调用StopFalling函数停止物品的下落状态(bFalling)。
总结起来,这段代码的作用是在物品投掷时设置旋转角度,计算并施加冲量力以实现投掷效果,并在一定时间后停止物品的下落状态。