java - Trying to make a treasure hunter game, but having issues with the surview script -
the general idea app allows user click on field , find items. item definitions come excel file. items scatter across field in random positions. when user click on screen create holes. items found display in list when pressing inventory button. having trouble drawsurface script.
xml:
<android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/mfield" /> <linearlayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50dp"> <button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="view inventory" android:id="@+id/btninventory"/> <button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="credits" android:id="@+id/btncredits"/> </linearlayout> </linearlayout>
mainactivity:
import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.view; import android.app.alertdialog; import android.content.dialoginterface; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; public class mainactivity extends appcompatactivityimplementsview.onclicklistener { arraylist<item> inv = drawsurface.getinventory(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); findviewbyid(r.id.btninventory).setonclicklistener(this); findviewbyid(r.id.btncredits).setonclicklistener(this); } @override public void onclick(view view) { if (view.getid() == r.id.btncredits) { alertdialog.builder dialog = new alertdialog.builder(this); dialog.settitle("made by"); dialog.setmessage("djon archer" + "\nmgms | cse\n08/10/2017"); dialog.setpositivebutton(" ok ", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { dialog.dismiss(); } }); dialog.show(); } else if (view.getid() == r.id.btninventory) { loaditems(); } } private arraylist<item> loaditems() { inputstream input = getresources().openrawresource(r.raw.items); bufferedreader reader = null; arraylist<item> items = new arraylist<item>(); string line; try { reader = new bufferedreader(new inputstreamreader(input)); while ((line = reader.readline()) !=null) { items.add(new item(line)); } } catch (exception e) { log.e("mainactivity", "reading list of items failed!", e); } { try { if (reader != null) reader.close(); } catch (exception e) { log.e("mainactivity", "error closing file reader.", e); } } return items; } }
item:
public class item { public string name; public int x; public int y; //constructor public item(string n) { name = n; } }
drawsurface:
import android.content.context; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.surfaceholder;uu import android.view.surfaceview; import android.view.view; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; abstract class drawsurface extends surfaceview implements surfaceholder.callback, view.ontouchlistener { surfaceholder holder; bitmap mbmpfield; bitmap mbmphole; private static arraylist<item> mitems; public drawsurface(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // getting holder holder = getholder(); holder.addcallback(this); setontouchlistener(this); findviewbyid(r.id.mfield); } int radius = math.max(mbmphole.getheight(),mbmphole.getwidth()) / 2; public static arraylist<item> getinventory() { //called mainactivity.java return mitems; } @override public boolean ontouch(view v, motionevent event) { if(event.getaction()== motionevent.action_down) { (int i=mitems.size()-1; i>0; i--) mitems.get(i); dx = mitems.x - center.x; dy = mitems.y - center.y; if((dx)*(dx) + (dy)*(dy) < radius*radius) { mitems.remove(i); } } setwillnotdraw(false); } //surface holder allows control , monitor surface @override public void surfacecreated(surfaceholder holder) { canvas c = holder.lockcanvas(); if (c!=null) mfield.dim.set(0,0, c.getwidth(),c.getheight()); holder.unlockcanvasandpost(c); invalidate(); (int i=0; i<mitems.size(); i++) { mitems.get(i).x = (int)(math.random() * (float)mfielddim.width()); mitems.get(i).y = (int)(math.random() * (float)mfielddim.height()); } } @override public void ondraw(canvas canvas) { drawbitmap(); } void drawbitmap() { mbmpfield = bitmapfactory.decoderesource(getresources(), r.drawable.field); mbmphole = bitmapfactory.decoderesource(getresources(), r.drawable.hole); } }
wiki
Comments
Post a Comment