对这篇文章有点困惑,所以我想我还是发帖吧。抱歉,如果我的标题不清楚,非常新的java和不确定如何解释它。不管怎样,这里是我目前为止的代码集(我猜是有问题的部分)
int currentImageIndex = 0; // Assuming [0] is your first image.
int[] nextImageList = { 2, 4, 5, 4, 5, 4, 5, 0, 1 };
public void nekoRun() {
moveIn();
scratch();
moveOut();
private void moveIn() {
for (int i = 0; i < getWidth()/2; i+=10) {
xPos = i;
// swap images
if (currentImage == nekoPics[0])
currentImage = nekoPics[1];
else
currentImage = nekoPics[0];
repaint();
pause(150);
private void scratch() {
for (int i = xPos; i < getWidth();) {
xPos = i;
// Swap images.
currentImageIndex = nextImageList[currentImageIndex];
currentImage = nekoPics[currentImageIndex];
repaint();
pause(150);
}
}
private void moveOut() {
for (int i = xPos; i < getWidth(); i+=10) {
xPos = i;
// swap images
if (currentImage == nekoPics[0])
currentImage = nekoPics[1];
else
currentImage = nekoPics[0];
repaint();
pause(150);
}
}所以基本上发生了什么(这不是所有的代码,只是“有趣的部分”,一只猫会跑过屏幕,然后坐着,它应该抓两次,我得到了一些关于数组的帮助,因为我只是使用了一大堆else if语句,我知道这是多余的。猫会跑到中心,它会抓挠,然后一直抓,在一个循环中,由于显而易见的原因,我只是困惑于如何才能让它转移到moveOut方法上,而不是一直循环抓挠。如果这有点不清楚,很抱歉,我是个新手,所以请耐心听我说。
提前感谢
发布于 2013-02-22 21:47:20
你的问题叫做无限循环...而不是
private void scratch() {
for (int i = xPos; i < getWidth();) {这应该是
private void scratch() {
for (int i = 0; i < 2*<how many frames the scratching takes>; i++) {
// **UPDATE** the xPos=i; shouldn't be here!!!解释:
您似乎从moveIn()函数复制了循环定义,在这种情况下这似乎是合法的。发生了一些事情,直到它到达屏幕的中间。但是在scratch()函数中,精灵不会移动,它永远不会到达屏幕的末端……因此,小津必须重复两次绘制步骤,即划痕跨度为多少帧。您必须将该数字输入到<how many frames the scratching takes>占位符中,它应该可以工作。
编辑 xPos=i;不应出现在scratch()中...
发布于 2013-02-22 21:47:14
您没有在scratch()中将i++放在for语句的末尾递增i。
for (int i = xPos; i < getWidth(); i++)https://stackoverflow.com/questions/15025603
复制相似问题