Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Yet another level format issue...

  1. #1
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Yet another level format issue...

    Hi Gravity Games here and I'm having a little trouble with making a level format. I have an Activity, a custom view, and two classes (the player and the tile object I want to place). First of all, where do I put the code that sets specific x and y coordinates for each object? Second, how exactly do I do it?
    Last edited by Gravity Games; August 18th, 2012 at 03:30 PM.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Yet another level format issue...

    Are you using java or xml for the layout? (Android works either way but xml would most of the time be top choice)

    With that, your x and y would go in the xml. As far as exactly how to do it, i would need to see more about your two classes and the activity which uses them.

  3. #3
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    I'm using java for the layout, it works better for game development from what I've heard, and when I tried to do a game layout in xml, I failed miserably. Oh, and as for the code I coded it on my droid using an app, so I'll see if there's a way I can copy and paste it here.

    Edit:
    MainActivity.java:
     package com.gravitygamesinteractive.mysticmaze;
     
    import android.app.*;
    import android.os.*;
    import android.view.*;
    import android.widget.*;
    import android.graphics.*;
    import android.graphics.drawable.shapes.*;
    import java.awt.*;
    import java.util.*;
    import android.graphics.drawable.*;
     
    public class MainActivity extends Activity implements Runnable 
    {
       public static boolean isRunning=false;
     
       private Bitmap b=Bitmap.createBitmap(100,100,Bitmap.Config.ARGB_8888);
       private Canvas c=new Canvas(b);
       private Rect r=new Rect(20,20,40,40);
       /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState)
    	{
            super.onCreate(savedInstanceState);
    		requestWindowFeature(Window.FEATURE_NO_TITLE);
    		setContentView(new gameView(this));
        }
    	@Override
    	public void onStart(){
    		super.onStart();
     
    		isRunning=true;
    		new Thread(this).start();
    	}
    	@Override
    	public void onStop(){
    		super.onStop();
     
    		isRunning=false;
    	}
    	@Override
    	public void onDraw(){
     
    	}
     
    public void tick(){
     
    	}
     
    	public void Paint(Canvas c){
     
    	}
     
    	public void run(){
    		while(isRunning){
    			tick();
    			Paint(c);
     
    			try{
    				Thread.sleep(16);
    			}catch(Exception e){ }
    		}
    	}
    }

    gameView.java:
     package com.gravitygamesinteractive.mysticmaze;
     
    import android.view.*;
    import android.content.*;
    import android.graphics.*;
    import javax.security.auth.callback.*;
    import android.view.SurfaceHolder.Callback;
    import java.util.*;
     
    public class gameView extends SurfaceView
    {
     
    private Bitmap bmp;
    private Bitmap tilebmp;
    private SurfaceHolder holder; 
    private GameLoopThread gameLoopThread;
    private Sprite sprite;
    private List<Tile> tiles=new ArrayList<Tile>();
     
    public gameView(Context context){
    super(context);
    gameLoopThread=new GameLoopThread(this);
    holder=getHolder();
    holder.addCallback(new Callback() {
     
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    gameLoopThread.setRunning(false);
    gameLoopThread.stop();
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder){
    gameLoopThread.setRunning(true);
    gameLoopThread.start();
     
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
     
    }
    });
    bmp=BitmapFactory.decodeResource(getResources(), R.drawable.kyle);
    sprite=new Sprite(this,bmp);
    tiles.add(createTile(R.drawable.ttile));
    }
     
    private Tile createTile(int resource){
    Bitmap tilebmp=BitmapFactory.decodeResource(getResources(),resource);
    return new Tile(this,tilebmp);
    }
     
    @Override
    protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
    for (Tile tile : tiles){
    tile.onDraw(canvas);
    }
    }
    }

    GameLoopThread.java:
     package com.gravitygamesinteractive.mysticmaze;
     
    import android.graphics.*;
     
    public class GameLoopThread extends Thread
    {
    private gameView view;
    private boolean running=false;
     
    public GameLoopThread(gameView view){
    this.view=view;
    }
     
    public void setRunning(boolean run){
    running=run;
    }
    @Override
    public void run(){
     
    while(running){
    Canvas c=null;
    try{
    c=view.getHolder().lockCanvas();
    synchronized(view.getHolder()){
    view.onDraw(c);
    }
    }finally{
    if (c!=null){
    view.getHolder().unlockCanvasAndPost(c);
    }
    }
    try{
    sleep(16);
    }catch(Exception e){}
    }
    }
    }
    Last edited by Gravity Games; August 17th, 2012 at 06:15 PM.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  4. #4
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    ...And Tile.java (object):

     package com.gravitygamesinteractive.mysticmaze;
     
    import android.graphics.*;
     
    public class Tile
    {
    private int x;
    private int y;
    private gameView gameview;
    private Bitmap bmp;
     
    public Tile(gameView gameview, Bitmap bmp){
    this.gameview=gameview;
    this.bmp=bmp;
    }
     
    private void update(){
     
    }
    public void onDraw(Canvas canvas){
    update();
    canvas.drawBitmap(bmp,x,y,null);
    }
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Yet another level format issue...

    I'm using java for the layout, it works better for game development from what I've heard
    When it all boils down, xml is turned into java (and finally dalvik). The advantages to using xml come in how much easier it is to use. Really the only time you wouldn't want to use xml, is when you are porting existing java to droid. I almost always go the xml route just because it is much less to type. But that is a story for another day.

    On to your original question, where do you put the code to ....
    Android has built in life cycles as you see in your main activity, onStart, onStop etc.. When the app is active you are setting your isRunning boolean to true and back to false with onStop. All of that code is not necessary as this is the purpose of the built in life cycles. You can minimize your code by taking advantage of these cycles. If you do some research on android life cycles you will see other methods you can override and use, onPause, onResume etc...

    It looks to me like you could use a restart with a better plan of attack, and allow android to take care of the life cycles of the app. Also use the savedInstanceState to reload. I would suggest taking a detour from the project and toy with android built in functionality a bit. Make a hello world app. Then create another hello world app and call one activity from another. Then set a string variable to hold hello world in onStart. Save the value to savedInstanceState in onPause, and reload it in onResume. Once you get the hang of how android runs through the life cycles, it will be much easier to add your java code to the app.

  6. #6
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    I've already made several simpler apps while I was researching android development. The reason I'm not taking full advantage of the android life cycles is because I plan to make both an Android and Java (and maybe other systems eventually) version of the game, hence the usage of isRunning, etc. I'm trying to make it easier on myself by using (for the most part, gosh are Android graphics routines annoying...) the same code for each. If I wind up needing optimization, I'll probably do it after I have a functional engine.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Yet another level format issue...

    You can make updates to the screen inside the gameView class with the onDraw method.
    Inside the for loop each of your tiles are being called to redraw themselves. You can make changes to the way they are drawn as a group using the for loop, ie calling a method of the class on each object, like .move(dX, dY);
    ...or what ever methods you happen to make within the tile class...

    You can also put method calls in any of your running loops just depending on what you are trying to do. You might want a whole new Activity to take over when a menu button is pressed, for example. This would pause your game loops and the entire Activity. (That is how I do most of my larger menus in my projects.)

    Good luck with it. Nice to see more active android chatter in here.


    oh and: gameView should be renamed to GameView

  8. #8
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    Quote Originally Posted by jps View Post
    gameView should be renamed to GameView
    Yeah, I noticed that but never got a chance to change it. I'll probably do that when I get Eclipse set up with my new computer (I already copied the java source from my android). Also, I'll try using methods, since I think I thought of something that might just work.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  9. #9
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    I'm having more trouble with this. I can't seem to spawn objects with an array like I did for CWW...

    package com.gravitygamesinteractive.mysticmaze;
     
    import android.view.*;
    import android.content.*;
    import android.graphics.*;
    import javax.security.auth.callback.*;
    import android.view.SurfaceHolder.Callback;
    import java.util.*;
    import java.io.File;
     
    public class gameView extends SurfaceView
    {
     
    private Bitmap bmp;
    private Bitmap tilebmp;
    private SurfaceHolder holder; 
    private GameLoopThread gameLoopThread;
    private Sprite sprite;
    private List<Tile> tiles=new ArrayList<Tile>();
    private ArrayList<String> tilexarray=new ArrayList<String>();
    private ArrayList<String> tileyarray=new ArrayList<String>();
    private ArrayList<String> tileidarray=new ArrayList<String>();
    private Scanner tilescan;
    private int tileobjid;
    private int tilex;
    private int tiley;
    private String tileobjidstr;
    private String tilexstr;
    private String tileystr;
    private int b=0;
    private int ta=0;
     
    public gameView(Context context){
    super(context);
    gameLoopThread=new GameLoopThread(this);
    holder=getHolder();
    holder.addCallback(new Callback() {
     
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    gameLoopThread.setRunning(false);
    gameLoopThread.stop();
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder){
    gameLoopThread.setRunning(true);
    gameLoopThread.start();
     
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
     
    }
    });
    bmp=BitmapFactory.decodeResource(getResources(), R.drawable.kyle);
    sprite=new Sprite(this,bmp);
    levelFile();
    for(ta=0; ta<tilexarray.size(); ta++){
    tiles.add(createTile(R.drawable.ttile));
    ta++;
    }
    }
     
    private Tile createTile(int resource){
    	Bitmap tilebmp=BitmapFactory.decodeResource(getResources(),resource);
    return new Tile(this,tilebmp,Integer.parseInt(tilexarray.get(ta)),Integer.parseInt(tileyarray.get(ta)));
    }
     
    @Override
    protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
    for (Tile tile : tiles){
    tile.onDraw(canvas);
    }
    }
     
    public void levelFile(){
    try{
    tilescan=new Scanner(new File("assets//levels//w1-1.txt"));
    b=0;
    while(tilescan.hasNextLine()){
    tilex=tilescan.nextInt();
    tilexstr=Integer.toString(tilex);
    tilexarray.add(b,tilexstr);
    tiley=tilescan.nextInt();
    tileystr=Integer.toString(tiley);
    tileyarray.add(b,tileystr);
    	b++;
    }
    }catch(Exception e){}
    }
    }
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Yet another level format issue...

    What do you mean you can't seem to spawn objects, do you mean draw them or make them exist as objects?
    If you mean to draw them use the onDraw method much like you would use the paint method in java.
    If that is not what you are wanting, tell me more about which object(s) in your code you have questions about and more specifically what you want them to do.

  11. #11
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    I can't get them to draw. I'm assuming that its the only thing wrong, but until I know if the onDraw is working right, I can't be sure.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  12. #12
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Yet another level format issue...

    Quote Originally Posted by Gravity Games View Post
    I can't get them to draw. I'm assuming that its the only thing wrong, but until I know if the onDraw is working right, I can't be sure.
    What happens when you run the code? Do the tiles from the loop show up?

    Have you tried to add a test inside the onDraw method? For something called multiple times I sometimes add a counter and display the counter value. Then you can see a little more about what is going on. You could change the background color to a random color every time the method is called.

  13. #13
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    No, the tiles don't even show up. I'll try changing the background color in onDraw and see if it works.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  14. #14
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    Yep, its a problem with onDraw. The part that draws the tiles apparantly doesn't work right for some reason.
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

  15. #15
    Member Gravity Games's Avatar
    Join Date
    May 2012
    Posts
    152
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Yet another level format issue...

    I tried changing gameView a bit, but I still can't get it to work...

    gameView:

    package com.gravitygamesinteractive.mysticmaze;
     
    import android.view.*;
    import android.content.*;
    import android.graphics.*;
    import javax.security.auth.callback.*;
    import android.view.SurfaceHolder.Callback;
    import java.util.*;
    import java.io.File;
     
    public class gameView extends SurfaceView
    {
     
    private Bitmap bmp;
    private Bitmap tilebmp;
    private SurfaceHolder holder; 
    private GameLoopThread gameLoopThread;
    private Sprite sprite;
    private List<Tile> tiles=new ArrayList<Tile>();
    private ArrayList<String> tilexarray=new ArrayList<String>();
    private ArrayList<String> tileyarray=new ArrayList<String>();
    private ArrayList<String> tileidarray=new ArrayList<String>();
    private Scanner tilescan;
    private int tileobjid;
    private int tilex;
    private int tiley;
    private String tileobjidstr;
    private String tilexstr;
    private String tileystr;
    private int b=0;
    private int ta=0;
     
    public gameView(Context context){
    super(context);
    gameLoopThread=new GameLoopThread(this);
    holder=getHolder();
    holder.addCallback(new Callback() {
     
    @Override
    public void surfaceDestroyed(SurfaceHolder holder){
    gameLoopThread.setRunning(false);
    gameLoopThread.stop();
    }
    @Override
    public void surfaceCreated(SurfaceHolder holder){
    gameLoopThread.setRunning(true);
    gameLoopThread.start();
     
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
     
    }
    });
    bmp=BitmapFactory.decodeResource(getResources(), R.drawable.kyle);
    sprite=new Sprite(this,bmp);
    levelFile();
    tilebmp=BitmapFactory.decodeResource(getResources(), R.drawable.ttile);
    for(ta=0; ta<tilexarray.size(); ta++){
    tiles.add(createTile(R.drawable.ttile));
    }
    }
     
    private Tile createTile(int resource){
    	Bitmap tilebmp=BitmapFactory.decodeResource(getResources(),resource);
    return new Tile(this,tilebmp,Integer.parseInt(tilexarray.get(ta)),Integer.parseInt(tileyarray.get(ta)));
    }
     
    @Override
    protected void onDraw(Canvas canvas){
    canvas.drawColor(Color.BLACK);
    sprite.onDraw(canvas);
    for (int tac=0;tac<tilexarray.size();tac++){
    Tile tile=new Tile(tilebmp,10,40);
    	tile.onDraw(canvas);
    }
    	}
     
    public void levelFile(){
    try{
    tilescan=new Scanner(new File("assets//levels//w1-1.txt"));
    b=0;
    while(tilescan.hasNextLine()){
    tilex=tilescan.nextInt();
    tilexstr=Integer.toString(tilex);
    tilexarray.add(b,tilexstr);
    tiley=tilescan.nextInt();
    tileystr=Integer.toString(tiley);
    tileyarray.add(b,tileystr);
    	b++;
    }
    }catch(Exception e){}
    }
    	public ArrayList getTarrx(){
    		return tilexarray;
    	}
    	public ArrayList getTarry(){
    		return tileyarray;
    	}
    }

    Tile:
    package com.gravitygamesinteractive.mysticmaze;
     
    import android.graphics.*;
     
    public class Tile
    {
    private int x;
    private int y;
    private gameView gameview;
    private Bitmap bmp;
    private int altx;
    private int alty;
     
    public Tile(gameView gameview, Bitmap bmp, int x, int y){
    this.gameview=gameview;
    this.bmp=bmp;
    this.x=x;
    this.y=y;
    altx=x;
    alty=y;
    }
     
    private void update(){
     
    }
    public void onDraw(Canvas canvas){
    update();
    canvas.drawBitmap(bmp,x,y,null);
    }
    }

    and here's the error I keep getting:
    Constructor 'com.gravitygamesinteractive.mysticmaze.Tile.Tile( com.gravitygamesinteractive.mysticmaze.gameView,an droid.graphics.Bitmap,int,int)' can not be applied to '(android.graphics.Bitmap,int,int)'
    Current Projects (and planned release dates):

    Chomp's Wacky Worlds [???] (PC, Android and Ouya)
    Kyle the Caiman [???] (PC, Android and Ouya)
    KTC: King Crocko's Mystic Maze [???] (PC, Android and Ouya)

Similar Threads

  1. Help with an object based level format
    By Gravity Games in forum Java Theory & Questions
    Replies: 41
    Last Post: August 3rd, 2012, 12:18 PM
  2. Setting a transparency level of a picture issue
    By Asido in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 18th, 2012, 05:42 AM
  3. A level format problem
    By Gravity Games in forum Object Oriented Programming
    Replies: 8
    Last Post: June 4th, 2012, 02:20 PM
  4. Issue with reverse engineering what format this guy expects. Out of ideas
    By Udustar in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 22nd, 2011, 06:40 AM
  5. Issue with output format
    By mael331 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2011, 02:12 PM