所以我们想制作一个游戏,用Android手机的加速度计来移动气球。可能会有使用ImageView的障碍,导致气球在碰撞时爆裂。到目前为止,我们已经让ImageView气球移动了,但是似乎只有在ImageView气球的布局中才能看到可绘制的(称为yellow_balloon)。
java代码:
package com.ec327.ballon;
import android.os.Bundle;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.util.Log;
public class GameScreen extends Activity implements SensorEventListener {
private ImageView balloon=null;
final String tag = "AccLogger";
SensorManager sensore=null;
int xa=0;
int ya=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_screen);
sensore = (SensorManager) getSystemService(SENSOR_SERVICE);
balloon = (ImageView)findViewById(R.id.balloon);
balloon.scrollTo(xa, ya);
//balloon.offsetLeftAndRight(50);
//balloon.offsetTopAndBottom(50);
//balloon.setImageResource(R.drawable.yellow_balloon);
}
public void onSensorChanged(SensorEvent event){
Sensor sensor = event.sensor;
float [] values = event.values;
synchronized (this) {
Log.d(tag, "onSensorChanged: " + sensor + ", x: " +
values[0] + ", y: " + values[1] + ", z: " + values[2]);
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER ) {
xa=(int)values[0];// this part of code is only test to see int x and y on Activity
ya=(int)values[1];
}
}
balloon.scrollBy(xa, ya);
balloon.invalidate();
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
}
protected void onResume() {
super.onResume();
Sensor Accel = sensore.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// register this class as a listener for the orientation and accelerometer sensors
sensore.registerListener((SensorEventListener) this, Accel, SensorManager.SENSOR_DELAY_FASTEST);
}
}XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/blank_screen">
<ImageView
android:id="@+id/balloon"
android:layout_width="41dp"
android:layout_height="142dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="@drawable/yellow_balloon" />
</RelativeLayout>在xml文件中,ImageView气球有一个layout_width=41dp和layout_height=142dp。当程序运行时,yellow_balloon会四处移动,但是一旦它超出了ImageView气球的布局维度,就不能再看到它了。
scrollBy()是否只移动可绘制的yellow_balloon而不移动实际的气球布局的位置?我们和layout.setMargins一样尝试过使用它,但两者都不起作用。我们也尝试过使用setOffsetTopAndBottom和setOffsetLeftAndRight,但这并不能移动气球的ImageView。是什么导致了这个问题,我们如何解决它?
发布于 2013-12-07 01:35:25
我不完全确定使用imageViews是最简单或最有效的方法来完成您正在做的事情,特别是如果您想为气球碰撞和爆炸设置障碍的话。
我的建议是,您的游戏完全在SurfaceView内完成。从这里开始,您将可以添加"Sprites“,这将是您的所有对象(您的气球,您的障碍),您可以让它检查精灵碰撞(您的情况下气球会爆炸)。
图像视图的问题是,您需要移动整个图像,并且一旦您引入多个图像,除非图像周围的框完全满了图像,否则在实际碰撞发生之前,您将开始得到图像重叠或碰撞。
https://stackoverflow.com/questions/20435058
复制相似问题