我正在尝试在android中创建一个基本的棒球游戏。背景图像被设置为xml文件,并且我想在本垒板上设置基本球图像以在触摸屏幕时移动。问题是我无法显示棒球图像。该应用程序本身有5个活动,游戏是应用程序内的一个活动。我是Android的新手并开发了一款基本游戏。任何帮助,将不胜感激。提前致谢。 ` 要开始这是我的清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp.ron.drinkingbaseball"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.myapp.ron.drinkingbaseball.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.myapp.ron.drinkingbaseball.Stats"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.myapp.ron.drinkingbaseball.STATS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.myapp.ron.drinkingbaseball.DrinkingBaseballGame"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.myapp.ron.drinkingbaseball.DRINKINGBASEBALLGAME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.myapp.ron.drinkingbaseball.MyGameSurface"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.myapp.ron.drinkingbaseball.ShowStatistics"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.myapp.ron.drinkingbaseball.SHOWSTATISTICS" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的主要游戏java代码
public class DrinkingBaseballGame extends Activity implements OnTouchListener{
MyGameSurface mySurface;
float x, y, sx, sy, fx, fy, dx, dy, anix, aniy, scaledX, scaledY;
Bitmap ball, background;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mySurface = new MyGameSurface(this);
x = 0;
y = 0;
sx = 0;
sy = 0;
fx = 0;
fy = 0;
dx = 0;
dy = 0;
anix = 0;
aniy = 0;
scaledX = 0;
scaledY = 0;
ball = BitmapFactory.decodeResource(getResources(), R.drawable.baseball);
background = BitmapFactory.decodeResource(getResources(), R.drawable.baseball_field_background);
setContentView(R.layout.drinkingbaseball);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mySurface.pause();
}
@Override`enter code here`
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mySurface.resume();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
x = event.getX();
y = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
sx = event.getX();
sy = event.getY();
dx = dy = anix = aniy = scaledX = scaledY = fx = fy = 0;
break;
case MotionEvent.ACTION_UP:
fx = event.getX();
fy = event.getY();
dx = fx - sx;
dy = fy - sy;
scaledX = dx/30;
scaledY = dy/30;
x = y = 0;
break;
}
return true;
}
public class MyGameSurface extends SurfaceView implements Runnable{
SurfaceHolder myHolder;
Thread myThread = null;
boolean isRunning = false;
public MyGameSurface(Context context) {
super(context);
myHolder = getHolder();
}
public void pause(){
isRunning = false;
while(true){
try{
myThread.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
myThread = null;
}
public void resume(){
isRunning = true;
myThread = new Thread(this);
myThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
while(isRunning){
if(!myHolder.getSurface().isValid())
continue;
Canvas canvas = myHolder.lockCanvas();
canvas.drawBitmap(ball, canvas.getWidth()/2, canvas.getHeight()/4, null);
myHolder.unlockCanvasAndPost(canvas);
}
}
}
}
这是我的表面代码
public class MyGameSurface extends View{
Bitmap background, ball;
float changingY;
public MyGameSurface(Context context) {
// TODO Auto-generated constructor stub
super(context);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.baseball);
changingY = 0;
background = BitmapFactory.decodeResource(getResources(), R.drawable.baseball_field_background);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(ball, canvas.getWidth()/4, changingY, null);
if(changingY < canvas.getHeight()){
changingY += 10;
}else{
changingY = 0;
}
invalidate();
}
}
`
答案 0 :(得分:0)
如果您希望此代码可以使用
替换此行setContentView(R.layout.drinkingbaseball);
这一个
setContentView(mySurface);
但我仍然坚持认为你的观念并不好。
希望这有助于并享受您的工作。