为什么这个count再for循环里,却没有重置呢?
package ciel;import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.util.Scanner;
import com.rupeng.game.GameCore;
import java.awt.event.KeyEvent;
public class game2 implements Runnable
{
public static void main(String[] args)
{
GameCore.start(new game2());
}
public void run()
{
int [] x_pos = {101,256,355,472,564,694,782};
int [] y_pos = {328,171,524,219,475,235,111};
boolean [] g_tf = new boolean ;
GameCore.loadBgView("46905.jpg");
GameCore.setGameTitle("夏培鑫的第一个游戏");
for (int i=0;i<g_tf.length;i++)
{
GameCore.createSprite(i, "coin");
GameCore.setSpritePosition(i,x_pos,y_pos);
GameCore.playSpriteAnimate(i,"rotate",true);
}
int mlid = 999; int txid = 777;
GameCore.createSprite(mlid, "mario");
GameCore.setSpritePosition(mlid,50,20);
GameCore.playSpriteAnimate(mlid,"walk",true);
Dimension mlbig =GameCore.getSpriteSize(mlid);
int mlw = mlbig.width;
int mlh = mlbig.height;
GameCore.createText(txid,"金币数量:0");
GameCore.setTextPosition(txid,777,20);
GameCore.setTextColor(txid,Color.yellow);
for (;;)
{
int kc = GameCore.getPressedKeyCode();
Point pos = GameCore.getSpritePosition(mlid);
if (kc==KeyEvent.VK_UP)
{
GameCore.setSpritePosition(mlid,pos.x,pos.y-20);
}
if (kc==KeyEvent.VK_DOWN)
{
GameCore.setSpritePosition(mlid,pos.x,pos.y+20);
}
if (kc==KeyEvent.VK_LEFT)
{
GameCore.setSpriteFlipX(mlid,false);
GameCore.setSpritePosition(mlid,pos.x-20,pos.y);
}
if (kc==KeyEvent.VK_RIGHT)
{
GameCore.setSpriteFlipX(mlid,true);
GameCore.setSpritePosition(mlid,pos.x+20,pos.y);
}
GameCore.pause(50);
for (int i=0;i<g_tf.length;i++)
{
if (g_tf)
{
continue;
}
Dimension gbig =GameCore.getSpriteSize(i);
int gw = gbig.width;
int gh = gbig.height;
int gmidx = x_pos+gw/2;
int gmidy = y_pos+gh/2;
int mlmidx =pos.x+mlw/2;
int mlmidy =pos.y+mlh/2;
double magjl =Math.pow((gmidx-mlmidx)*(gmidx-mlmidx)+(gmidy-mlmidy)*(gmidy-mlmidy),0.5);
if (magjl<30)
{
GameCore.hideSprite(i);
g_tf=true;
int count = 0;
for (int s=0;s<g_tf.length;s++)
{
if (g_tf)
{
count++;
}
}
GameCore.setText(txid,"金币数量:"+count);
if (count==7)
{
GameCore.alert("游戏结束");
break;
}
}
}
}
int count = 0;这个应该是每次循环都变成0了啊,但是实际运行不是这样的.会一直自增. count,在循环外面,当然每次循环都不会置0了,g_tf在循环之前已经为true了,那么你的if(g_tf){
count++;
}
自然每次循环都会递增了,因为你的代码太长了,我只看了你划线的哪里,只看到了这个问题。 count和for基本没有关联 除了count++之外
每次循环条件成立 迭代count
所以count并不归零 最终的执念 发表于 2017-9-5 10:16
count和for基本没有关联 除了count++之外
每次循环条件成立 迭代count
count 再 for(;;) 里面啊 小气鬼 发表于 2017-9-5 09:00
count,在循环外面,当然每次循环都不会置0了,g_tf在循环之前已经为true了,那么你的if(g_tf){
count++ ...
这个count 再for(;;)这个死循环里面啊 xiazhianran6 发表于 2017-9-7 18:48
count 再 for(;;) 里面啊
count初始化之后就没有归零的代码 如何重置 如果你是想在每次循环完之后重置为0,那把count放在for循环里面的第一行就行了。
页:
[1]