使用 \ifnum 制作动画时无法在两个条件之间切换

使用 \ifnum 制作动画时无法在两个条件之间切换

我正在尝试制作一个在 120 和 80 之间切换的标志动画,文本也会相应地切换。但我只得到了 120 和

\begin{animateinline}[autoplay,loop]{2}
    \multiframe{3}{n=1+1}{
        \pgfmathsetmacro\tcolor{ifthenelse(\n==3,100,20)}        
        \begin{tikzpicture}        
        \foreach \i in {0,40,80,120,...,360}{% 
\filldraw [fill=red!\tcolor]  (\i:0.25cm) circle (1.5pt);
\node[draw=black,thick,shape=circle, minimum size =6pt] (rect) at (0,0) {
\ifnum\n = 100  
80
\else
120
\fi};
\node[black, right of = rect, align = center, xshift=1cm] (note) at (0,0) {
\ifnum\n = 100 
\tiny Hazardous \\ \tiny driving conditions
\else
\tiny Normal \\ \tiny driving conditions
\fi
};}
\shade[left color=black, right color=white, draw] ([yshift=-0.5cm] rect) rectangle ++(0.1,-1.2);
        \end{tikzpicture}


    }
\end{animateinline}

更新:当我修复 n 时遇到一个问题,红灯切换到 100 后文本切换的方式存在延迟。我试图修复它,但出现了一个 ew 错误,第一帧的内容不能有零宽度。

\begin{animateinline}[autoplay,loop]{2}
    \multiframe{3}{n=1+1}{
        \pgfmathsetmacro\tcolor{ifthenelse(\n==3,100,20)}        
        \begin{tikzpicture}        
        \foreach \i in {0,40,80,120,...,360}{% 
\filldraw [fill=red!\tcolor]  (\i:0.25cm) circle (1.5pt);
\ifnum\n = 2 
\node[draw=black,thick,shape=circle, minimum size =6pt, label={Hazardous conditions}] (rect) at (0,0) {
80};
\shade[left color=black, right color=white, draw] ([yshift=-0.32cm,xshift=-0.06cm] rect) rectangle ++(0.1,-1);
\else 
\node[draw=black,thick,shape=circle, minimum size =6pt, label={Normal conditions}] (rect) at (0,0) {
120};
\shade[left color=black, right color=white, draw] ([yshift=-0.32cm,xshift=-0.06cm] rect) rectangle ++(0.1,-1);
\fi
};}

        \end{tikzpicture}
            }
\end{animateinline}

在这一个中,我尝试让灯光始终闪烁。

\begin{animateinline}[autoplay,loop]{1}
\multiframe{6}{iFrame=1+1}{%
  \begin{tikzpicture}
  \pgfmathsetmacro{\tcolor}{ifthenelse(\iFrame == 1, 20, 60, 80, 100)}

  \foreach \angle in {0,40,...,320} {
    \filldraw[fill=red!\tcolor] (\angle:0.34cm) circle[radius=1.5pt];
  }

  \ifnum\iFrame < 3
    \drawSign{Normal conditions}{120}
  \else
    \drawSign{Hazardous conditions}{80}
  \fi

\shade[left color=black, right color=white, draw] ([yshift=-0.5cm,xshift=-0.05cm] rect) rectangle ++(0.1,-1)node[right,yshift=0.5cm,xshift=1.5cm](col){
\includegraphics[scale=0.25]{/Users/diana/Documents/Research/Papers/WFS/Figures/Animate/Sprite-car-front.pdf}};
\node[right,waves,xshift=0.15cm]at(rect){};
  \end{tikzpicture}%
}%
\end{animateinline}

答案1

如上所述,该n变量被错误地命名为整数类型(它应该以i或开头I),并且您的代码具有很多错误。下次不会最小工作示例(MWE),恐怕我不会尝试调试你的代码。

报告的错误animate

第一帧的内容不能有零宽度。

源于你确实做了以下事情:

\multiframe{3}{n=1+1}{%
  % [irrelevant \pgfmathsetmacro snipped]
  \begin{tikzpicture}
  ...
}

\end{tikzpicture}

除了语法错误之外,第一个框架实际上是空的,因为它tikzpicture无法被创建,因为它\end{tikzpicture}来自的最后一个结束括号\multiframe{...}{...}{...}

此外,您的\foreach循环非常浪费,因为它绘制了整个图形的十次。以下代码修复了这些错误和其他问题。

\documentclass{article}
\usepackage{tikz}
\usepackage{animate}
\usepackage{graphicx}

% The 'minimum width' must be chosen wide enough for both labels, or else
% there will be undesirable scaling.
\newcommand*{\drawSign}[2]{%
  \node[circle, draw, thick, minimum size=1cm,
        label={[minimum width=3.7cm]above:{#1}}] (rect) at (0,0) {#2};
}

\begin{document}
\begin{animateinline}[autoplay,loop]{1}
\multiframe{2}{iFrame=1+1}{%
  \begin{tikzpicture}
  \pgfmathsetmacro{\tcolor}{ifthenelse(\iFrame == 1, 20, 100)}

  \foreach \angle in {0,40,...,320} {
    \filldraw[fill=red!\tcolor] (\angle:0.34cm) circle[radius=1.5pt];
  }

  \ifnum\iFrame=1
    \drawSign{Normal conditions}{120}
  \else
    \drawSign{Hazardous conditions}{80}
  \fi

  \shade[left color=black, right color=white, draw]
    ([xshift=-0.05cm, yshift=-0.42cm] rect) rectangle ++(0.1,-1);
  \end{tikzpicture}%
}%
\end{animateinline}
\end{document}

在此处输入图片描述

记住:在修复所有报告的错误(例如,不匹配的括号)之前,请勿尝试解释 TeX 的输出。

相关内容