这是我的Player类
public class Player {
private double xCor;
private double yCor;
public Player(double xCor, double yCor)
{
this.xCor = xCor;
this.yCor = yCor;
}
public void setX(double latitude)
{
this.xCor = latitude;
}
public void setY(double longitude)
{
this.yCor = longitude;
}
public double getX()
{
return xCor;
}
public double getY()
{
return xCor;
}
}
这是我的GameSurface
public class GameSurface extends View implements Runnable{
private ArrayList<Ghost> ghostList;
private Paint paint;
private Player pl;
private LocationManager lm;
private MyLocationListener ml;
public GameSurface(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
pl = new Player(300,300);
lm = (LocationManager) context.getSystemService((Context.LOCATION_SERVICE));
ml = new MyLocationListener();
}
public GameSurface(Context context, AttributeSet attrs) {
super(context, attrs);
init();
pl = new Player(300,300);
lm = (LocationManager) context.getSystemService((Context.LOCATION_SERVICE));
ml = new MyLocationListener();
}
public GameSurface(Context context) {
super(context);
init();
pl = new Player(300,300);
lm = (LocationManager) context.getSystemService((Context.LOCATION_SERVICE));
ml = new MyLocationListener();
}
public void init() {
paint = new Paint();
paint.setColor(0xff00ff00);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(3);
}
public Player getPlayer()
{
return pl;
}
@Override
public void onDraw(Canvas c) {
super.onDraw(c);
c.drawCircle((float) pl.getX(), (float) pl.getY(),20,paint);
postDelayed(this, 16);
}
@Override
public void run() {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, ml);
pl.setX(ml.latitude*10);
pl.setY(ml.longitude*10);
invalidate();
}
}
这是MyLocationListener
public class MyLocationListener implements LocationListener {
public double latitude;
public double longitude;
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
当我拿着我的设备并尝试物理移动时,圆圈不会相应移动。我的LocationListener实现有什么问题吗?
答案 0 :(得分:0)
您应该只注册一次位置更新。尝试将lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, ml);
移动到构造函数方法的最后一行,以便它仅在启动时执行。
另外,请注意,根据您的设备,您可能需要在外面走动才能返回GPS位置。