1. Are you ready for the Galaxy S20? Here is everything we know so far!

Help with a Level Format

Discussion in 'Android Development' started by Gravity Games, Aug 21, 2012.

  1. Gravity Games

    Gravity Games Newbie
    Thread Starter

    Hi, Gravity Games here, and I'm working on a simple platform game for android. Unfortunately however, I'm having trouble implementing a level format that spawns objects. I'm trying to read a .txt file, then write integers into an arraylist, and finally using the appropriate line in the array for a variable in the object (x and y position, and later on which graphic file it uses)

    Here's what I've got:

    gameView:

    Code (Text):
    1. package com.gravitygamesinteractive.mysticmaze;
    2.  
    3. import android.view.*;
    4. import android.content.*;
    5. import android.graphics.*;
    6. import javax.security.auth.callback.*;
    7. import android.view.SurfaceHolder.Callback;
    8. import java.util.*;
    9. import java.io.File;
    10.  
    11. public class gameView extends SurfaceView
    12. {
    13.  
    14. private Bitmap bmp;
    15. private Bitmap tilebmp;
    16. private SurfaceHolder holder;
    17. private GameLoopThread gameLoopThread;
    18. private Sprite sprite;
    19. private List<Tile> tiles=new ArrayList<Tile>();
    20. private ArrayList<String> tilexarray=new ArrayList<String>();
    21. private ArrayList<String> tileyarray=new ArrayList<String>();
    22. private ArrayList<String> tileidarray=new ArrayList<String>();
    23. private Scanner tilescan;
    24. private int tileobjid;
    25. private int tilex;
    26. private int tiley;
    27. private String tileobjidstr;
    28. private String tilexstr;
    29. private String tileystr;
    30. private int b=0;
    31. private int ta=0;
    32.  
    33. public gameView(Context context){
    34. super(context);
    35. gameLoopThread=new GameLoopThread(this);
    36. holder=getHolder();
    37. holder.addCallback(new Callback() {
    38.  
    39. @Override
    40. public void surfaceDestroyed(SurfaceHolder holder){
    41. gameLoopThread.setRunning(false);
    42. gameLoopThread.stop();
    43. }
    44. @Override
    45. public void surfaceCreated(SurfaceHolder holder){
    46. gameLoopThread.setRunning(true);
    47. gameLoopThread.start();
    48.  
    49. }
    50. @Override
    51. public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
    52.  
    53. }
    54. });
    55. bmp=BitmapFactory.decodeResource(getResources(), R.drawable.kyle);
    56. sprite=new Sprite(this,bmp);
    57. levelFile();
    58. tilebmp=BitmapFactory.decodeResource(getResources(), R.drawable.ttile);
    59. for(ta=0; ta<tilexarray.size(); ta++){
    60. tiles.add(createTile(R.drawable.ttile));
    61. }
    62. }
    63.  
    64. private Tile createTile(int resource){
    65.     Bitmap tilebmp=BitmapFactory.decodeResource(getResources(),resource);
    66. return new Tile(this,tilebmp,Integer.parseInt(tilexarray.get(ta)),Integer.parseInt(tileyarray.get(ta)));
    67. }
    68.  
    69. @Override
    70. protected void onDraw(Canvas canvas){
    71. canvas.drawColor(Color.BLACK);
    72. sprite.onDraw(canvas);
    73. for (int tac=0;tac<tilexarray.size();tac++){
    74. Tile tile=new Tile(tilebmp,10,40);
    75.     tile.onDraw(canvas);
    76. }
    77.     }
    78.  
    79. public void levelFile(){
    80. try{
    81. tilescan=new Scanner(new File("assets//levels//w1-1.txt"));
    82. b=0;
    83. while(tilescan.hasNextLine()){
    84. tilex=tilescan.nextInt();
    85. tilexstr=Integer.toString(tilex);
    86. tilexarray.add(b,tilexstr);
    87. tiley=tilescan.nextInt();
    88. tileystr=Integer.toString(tiley);
    89. tileyarray.add(b,tileystr);
    90.     b++;
    91. }
    92. }catch(Exception e){}
    93. }
    94.     public ArrayList getTarrx(){
    95.         return tilexarray;
    96.     }
    97.     public ArrayList getTarry(){
    98.         return tileyarray;
    99.     }
    100. }
    101.  
    Tile:
    Code (Text):
    1. package com.gravitygamesinteractive.mysticmaze;
    2.  
    3. import android.graphics.*;
    4.  
    5. public class Tile
    6. {
    7. private int x;
    8. private int y;
    9. private gameView gameview;
    10. private Bitmap bmp;
    11. private int altx;
    12. private int alty;
    13.  
    14. public Tile(gameView gameview, Bitmap bmp, int x, int y){
    15. this.gameview=gameview;
    16. this.bmp=bmp;
    17. this.x=x;
    18. this.y=y;
    19. altx=x;
    20. alty=y;
    21. }
    22.  
    23. private void update(){
    24.  
    25. }
    26. public void onDraw(Canvas canvas){
    27. update();
    28. canvas.drawBitmap(bmp,x,y,null);
    29. }
    30. }
    31.  
    and here's the error I keep getting:
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    I've moved this to the Application Development forum so you can get the help you need.:)
     
    Gravity Games likes this.
  3. Gravity Games

    Gravity Games Newbie
    Thread Starter

    Ah thanks, I had seen that there was another help thread in the Android games section and assumed it was the right place...
     
  4. Unforgiven

    Unforgiven ...eschew obfuscation...
    Moderator

    Nah, if you look now that one is here too.;) Happens all the time, no worries.:D
     
  5. jonbonazza

    jonbonazza Android Expert

    I highly recommend that you forego the txt file in favor of some kind of data store, such as XML, or a database file. they are much more accessible than an txt file. That should make your scene loading easier.
     
Loading...
Similar Threads - Help Level Format
  1. Sebastian huth
    Replies:
    0
    Views:
    160
  2. kylabeardad
    Replies:
    6
    Views:
    503
  3. FranzKafkaYs
    Replies:
    0
    Views:
    208
  4. EvasiveEmanual
    Replies:
    4
    Views:
    590
  5. ChristineDorotheaRickner
    Replies:
    26
    Views:
    668
  6. FingleFangle
    Replies:
    1
    Views:
    739
  7. John9870
    Replies:
    6
    Views:
    400
  8. Gihansurendra
    Replies:
    3
    Views:
    365
  9. Scottv1982
    Replies:
    1
    Views:
    345
  10. tragicallyblazed
    Replies:
    4
    Views:
    507

Share This Page

Loading...