android - creating a basic baseball game using a surface view i am trying to get my baseball image to show -
i trying create basic baseball game in android. background image set xml file, , want set base ball image on home plate moved on touch of screen. th problem cannot baseball image show. app has 5 activities , game 1 activity inside app. new android , developing basic game. appreciated. in advance. ` start manifest file
<?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>
this main game java code
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); } } } }
here surface code
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(); } } `
if want code of yours work replace line with
setcontentview(r.layout.drinkingbaseball);
with one
setcontentview(mysurface);
but still stand behind words concept not good.
hope helps , enjoy work.
Comments
Post a Comment