Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Drawing/Movement Help  (Read 2041 times)
0 Members and 1 Guest are viewing this topic.
Offline Crisiszone

Senior Newbie





« Posted 2012-03-30 11:52:33 »

EDIT: Cut down on the giant first post. Also the second problem has been fixed. When I moved the loadScene, there was no scene to render in the Draw class, hence the NullPointer. But now that the map has been drawn, I am still unable to move around despite having been able to before I changed how the map was drawn.

Draw.java

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class Draw extends JFrame{
   
   private static final long serialVersionUID = 1L;
   
   JFrame frame;
      Canvas canvas;
      BufferStrategy bufferStrategy;
      Image background;
      Image button;
      JPanel battlePane = (JPanel) getContentPane();
      Scene scene;
      Camera cam;
     
     
     
      final int WIDTH = Settings.width;
      final int HEIGHT = Settings.heigth;
      Draw(){
         frame = new JFrame("PlaceHolder");
           
            JPanel panel = (JPanel) frame.getContentPane();
            panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
            panel.setLayout(null);
           
           canvas = new Canvas();
            canvas.setBounds(0, 0, WIDTH, HEIGHT);
            canvas.setIgnoreRepaint(true);
            frame.setIgnoreRepaint(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.pack();
            frame.setResizable(true);
            frame.setVisible(true);
            panel.add(canvas);
            canvas.createBufferStrategy(2);
            bufferStrategy = canvas.getBufferStrategy();
            canvas.requestFocus();
            canvas.setBackground(Color.black);
            canvas.addKeyListener(new ButtonHandler());      
           
      }

   void render() throws IOException {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      scene = Scene.loadScene("scenes/datTestMap.dat");
      scene.render(g);
      g.dispose();
      bufferStrategy.show();
   }
   void renderMenu(){
      //int flicker = 0;
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      g.drawString(" PRESS ENTER TO START ", WIDTH/2 - 70, HEIGHT/2 - 5);
      g.drawRect(WIDTH/4, HEIGHT/4, WIDTH/2, HEIGHT/2);
      g.dispose();
      bufferStrategy.show();
   }
   
   void renderBattle(){
     
      try{
              File pic = new File("bg.jpg");
              File b = new File("Button.jpg");
              background = ImageIO.read(pic);
              button = ImageIO.read(b);
              }catch (IOException e){
                 System.out.println("!");
              }
   
      //Image image = Toolkit.getDefaultToolkit().getImage("Button.jpg");      
      //image = new ImageIcon("Button.jpg").getImage();
      JButton testButton = new JButton("Fight!");
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      //Draws background
      //g.drawImage(background, 0, 0, null);
      frame.getContentPane().add(testButton);
      testButton.setLocation(300, 300);
      g.drawString(" FIGHT BITCHES! ", HEIGHT-20, 20);
      //Draws Fight button
      g.drawRect(400, 400, Instances.button.getWidth(), Instances.button.getHeight());
      g.drawImage(button, 400, 400, null);
      g.drawString("FIGHT", 425, 415);
      //Draws Flee button
      g.drawRect(400, 425, Instances.button.getWidth(), Instances.button.getHeight());
      g.drawImage(button, 400, 425, null);
      g.drawString("FLEE", 425, 440);
     
      bufferStrategy.show();
      g.dispose();
     

   }
     
   protected void render(Graphics2D g){
      //g.drawImage(Instances.mapMan.getMap(), 0-Instances.camera.getX(), 0-Instances.camera.getY(), null);
   
      try {
         scene = Scene.loadScene("scenes/datTestMap");
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

      //scene.render(g);
     
      g.setColor(Instances.player.getColor());
      g.fillRect(Instances.player.getX()-Instances.camera.getX(), Instances.player.getY()-Instances.camera.getY(), 15, 15);
   }

}


ButtonHandler.java

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
import java.io.IOException;

import javax.sound.sampled.Clip;
imporimport java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class ButtonHandler extends KeyAdapter {

    public ButtonHandler() {
       System.out.println(" Button handler initialized! ");
               
    }

    public void keyPressed(KeyEvent key) {
       
              switch (key.getKeyCode()) {
                        case KeyEvent.VK_UP:
                           Instances.player.setUp(true);
                             break;
                        case KeyEvent.VK_DOWN:
                           Instances.player.setDown(true);
                            break;
                        case KeyEvent.VK_LEFT:
                           Instances.player.setLeft(true);
                            break;
                        case KeyEvent.VK_RIGHT:
                           Instances.player.setRight(true);
                            break;
                        case KeyEvent.VK_ENTER:
                           if(Instances.GAMESTATE == GameState.INGAME){
                              Instances.GAMESTATE = GameState.BATTLE;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.MENU){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }
                           break;
                        case KeyEvent.VK_SPACE:
                           System.out.println(Instances.GAMESTATE);
                           if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.battle.startFight();


                             
                           }  
                           break;
                        case KeyEvent.VK_1:
                           Instances.mapMan.setMap("map1");
                           break;
                        case KeyEvent.VK_2:
                           Instances.mapMan.setMap("map2");
                           break;
                        case KeyEvent.VK_3:
                           Instances.mapMan.setMap("map3");
                           break;
              }
    }

    public void keyReleased(KeyEvent key) {
        switch (key.getKeyCode()) {
           case KeyEvent.VK_UP:
              Instances.player.setUp(false);
              System.out.println(" Released UP!");
                break;
           case KeyEvent.VK_DOWN:
              System.out.println(" Released DOWN!");
              Instances.player.setDown(false);
               break;
           case KeyEvent.VK_LEFT:
              System.out.println(" Released LEFT!");
              Instances.player.setLeft(false);
               break;
           case KeyEvent.VK_RIGHT:
              System.out.println(" Released RIGHT!");
              Instances.player.setRight(false);
               break;
       
        }
    }

   @Override
   public void keyTyped(KeyEvent arg0) {
      // TODO Auto-generated method stub
     
   }
}



Throwing this link out there as a test, not sure if anybody can access it, but if so then woot!
https://bitbucket.org/DatGameCompany/westernrpg/src/6e7593f88105/src
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2012-03-30 12:38:37 »

Your post is a bit rambling - what, exactly, is the problem you'd like help with?

You mentioned movement problems - what are you expecting to see? What actually happens? Are you receiving key pressed events? Where's your ButtonHandler code?

Help us help you.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline ReBirth
« Reply #2 - Posted 2012-03-30 12:43:11 »

When you call renderMenu and renderBattle?
Please try to narrow down your problem. I gave up 10mins to read your little diary and I can't find out what's wrong with the code. No offense here, but it will help other members. FYI I'm the most idlest member here yet complain.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Crisiszone

Senior Newbie





« Reply #3 - Posted 2012-03-30 19:23:58 »

Sorry about that >.< I do tend to get off track pretty easily.
I'm looking specifically at the render method, renderMenu and renderBattle are working fine atm. I kinda fixed the first problem, the
loadScene method was being called over and over, and then I realized it was inside my game loop. After I moved it, it loaded the scene properly, but when it goes to draw and calls render I get a NullPointerException and the game closes.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299  
300  
301  
302  
303  
304  
305  
306  
307  
308  
309  
310  
311  
import java.awt.*;
import java.util.*;
import java.net.*;
import java.io.*;

/**
 * this class was originally meant to simply hold a reference to a map
 * and a collection of sprites, which it delegated all the work to in the
 * logic and render methods of these objects...<p>
 *
 * so it is reasonably fitting that this class also manage the loading of the
 * maps and sprites it uses from files or URLs.<p>
 *
 * Each scene consists of a Map, and a collection of sprites, as intended,
 * but may also be loaded or saved.
 */

public class Scene
{
   int offsetX = 0;
   int offsetY = 0;
   
   float effect_rScale = 1;
   float effect_gScale = 1;
   float effect_bScale = 1;
   float effect_hue = 0;
   float effect_sat = 1;
   
   Map map;
   ArrayList sprites;
   
   GraphicsBank tileset;
   
   /**
    * creates a scene using the given map and sprites.
    */

   public Scene(Map m, ArrayList s, GraphicsBank gfx)
   {
      this.map = m;
      sprites = s;
      this.tileset = gfx;
     
   }
   
   /* Create a new empty scene */
  public Scene() {
    map    = new Map(10, 10, 32, 32);
    tileset = new GraphicsBank();
  }
   
   
   public GraphicsBank getTileset()
   {
      return tileset;
     
   }
   
   public void setTileset(GraphicsBank gfx) {
      tileset = gfx;
      map.setTileset(gfx);
   }
   
   /**
    * loads a scene from the given URL. takes tiles from the given GraphicsBank.
    */

   static Scene loadScene(File f) throws IOException
   {
      boolean hasColourEffect = false;
      float r = 1;
      float g = 1;
      float b = 1;
      float h = 0;
      float s = 1;
   
      BufferedReader reader = new BufferedReader(new FileReader(f));
     
      String line = reader.readLine();
     
      StringTokenizer tokens = new StringTokenizer(line);
      int width = Integer.parseInt(tokens.nextToken());
      int height = Integer.parseInt(tokens.nextToken());
     
      String tileset = tokens.nextToken();
     
      GraphicsBank gfx = new GraphicsBank();
     
      System.out.println("Attempt to load tileset "+tileset);
     
      System.out.println("Working path is "+f.getParentFile());
     
      File ts = new File(f.getParentFile(), tileset);
      System.out.println("Attempt to load tileset "+ts.getAbsoluteFile());
     
     
      gfx.loadTileset(ts);
     
      Map map = new Map(width, height);
     
      line = reader.readLine();
      tokens = new StringTokenizer(line);
     
      if(tokens.nextToken().equalsIgnoreCase("colorization")) {
         hasColourEffect = true;
         r = Float.parseFloat(tokens.nextToken());
         g = Float.parseFloat(tokens.nextToken());
         b = Float.parseFloat(tokens.nextToken());
         h = Float.parseFloat(tokens.nextToken());
         s = Float.parseFloat(tokens.nextToken());
      }
     
      while(! line.equals("."))
      {
         line = reader.readLine();
      }
     
     
      for(int z=0; z<3; z++)
      {
         line = reader.readLine();
         tokens = new StringTokenizer(line);
         
         for(int y=0; y<height; y++)
         {
            for(int x=0; x<width; x++)
            {
               String code = tokens.nextToken();
               map.setTile(x, y, z, gfx.getTile(Integer.parseInt(code)));
            }
         }
      }
      reader.close();
     
      Scene scene = new Scene(map, new ArrayList(), gfx);
      scene.tileset = gfx;
      if(hasColourEffect) {
         System.out.println("Calling setEffect on scene recently loaded.");
         scene.setEffect(r, g, b, h, s, 1f);
      }
      return scene;
     
   }
   
   
    static Scene loadScene(String filename) throws IOException {
      Scene scene = loadScene(new File(filename));
      return scene;
    }
   /**
    * writes the map only (at the moment) to a file.
    */

  public void saveScene(File file)
  {
    if(tileset.isUnsaved()) {
      throw new RuntimeException("Tileset is unsaved. Cannot save the scene");
    }
     
      try
      {
         PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
         
         String line = "";
         
         int width = map.getWidth();
         int height = map.getHeight();
         
         
     
      File wd = new File(file.getParentFile().getCanonicalFile().toString());
         File ts = new File(tileset.getFile().getCanonicalFile().toString());
         
         String relativePath = RelativePath.getRelativePath(wd, ts);
         

         
      line = width + " " + height + " " + relativePath;
         writer.println(line);
         
         line = "colorization " + effect_rScale +
                            " " + effect_gScale +
                            " " + effect_bScale +
                            " " + effect_hue    +
                            " " + effect_sat;
         writer.println(line);

         System.out.println("Colorization red in save is "+effect_rScale);        
         writer.println(".");
         
         for(int z=0; z<3; z++)
         {
            for(int i=0; i<height; i++)
            {
               for(int j=0; j<width; j++)
               {
                  Tile t = map.getTile(j, i, z);
                  if(t != null)
                     writer.print(t.getNumber()+ " ");
                  else writer.print("0 ");
                 
                 
               }
               
            }
            writer.println();
         }
         
         writer.flush();
         writer.close();
         
         
      }
      catch(IOException e)
      {
         throw new RuntimeException("Could not save the level");
      }
     
      System.err.println("Saved");
   }
   
   /**
    * calls each sprites logic method.
    */

   void logic()
   {
      for(int i=0; i<sprites.size(); i++)
      {
         Sprite s = (Sprite)sprites.get(i);
         s.logic();
      }
   }
   
   /**
    * renders the scene to the graphics context.
    * at the moment, sprites appear above everything else.
    *
    * TODO: Fix this up.
    */

   void render(Graphics g)
   {
      //System.out.println("Render Scene");
      map.render(g, offsetX, offsetY);
     
      for(int i=0; i<sprites.size(); i++)
      {
         Sprite s = (Sprite)sprites.get(i);
         //s.render(g, offsetX, offsetY);
      }
     
   }
   public void render(Graphics g, int offX, int offY)
   {
      map.render(g, offX, offY);
   }
   public void render(Graphics g, Camera c)
   {
      map.render(g, c);
   }
   
   
   public void render(Graphics g, Point origin, Dimension size)
   {
      map.render(g, origin, size);
   }
   
   public void render(Graphics g, Point origin, Dimension size, int layer)
   {
      map.render(g, origin, size, layer);
   }
   
   /**
    * sets the visible area of the scene.
    * This will probably not be used once the game is finished.
    */

   void setViewSize(int width, int height)
   {
      map.setViewSize(width, height);
     
   }
   
   /**
    * sets the screen offset. Screen offset can be anywhere, not just
    * following a character around. This is good for cut scenes.
    */

   void setOffset(int x, int y)
   {
      offsetX = x;
      offsetY = y;
   }
   
   /**
    * Apply RGB scalars and hue/sat adjustments to the tiles and characters
    * in the scene. The hue and sat are always applied first
    **/

   public void setEffect(float r, float g, float b, float h, float s, float z) {
      System.out.println("Scene setEffect called. will call for the gfx bank...r" +r+" g"+g+" b"+b+" z"+z);
      effect_rScale = r;
      effect_gScale = g;
      effect_bScale = b;
      effect_hue = h;
      effect_sat = s;
      tileset.setEffect(r, g, b, h, s, z);
      map.setZoom(z);
   }
   
   /**
    * returns the map. not really any reason to want to do this except for the
    * level editor.
    */

   public Map getMap()
   {
      return map;
   }
}


1  
2  
3  
4  
5  
6  
7  
8  
void render() {
      Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
      g.clearRect(0, 0, WIDTH, HEIGHT);
      //render(g);
      scene.render(g);
      g.dispose();
      bufferStrategy.show();
   }
Offline ReBirth
« Reply #4 - Posted 2012-03-31 11:14:50 »

NullPointerException is commonly easy to solve. You can post the stacktrace.

Offline Crisiszone

Senior Newbie





« Reply #5 - Posted 2012-03-31 11:17:24 »

xD I knew people wouldn't notice I edited the original post. I solved that problem. Now my whole ordeal is that movement isn't working anymore.

ButtonHandler
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
public void keyPressed(KeyEvent key) {
       
              switch (key.getKeyCode()) {
                        case KeyEvent.VK_UP:
                           Instances.player.setUp(true);
                             break;
                        case KeyEvent.VK_DOWN:
                           Instances.player.setDown(true);
                            break;
                        case KeyEvent.VK_LEFT:
                           Instances.player.setLeft(true);
                            break;
                        case KeyEvent.VK_RIGHT:
                           Instances.player.setRight(true);
                            break;
                        case KeyEvent.VK_ENTER:
                           if(Instances.GAMESTATE == GameState.INGAME){
                              Instances.GAMESTATE = GameState.BATTLE;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.MENU){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }else if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.GAMESTATE = GameState.INGAME;
                              System.out.println(Instances.GAMESTATE);
                           }
                           break;
                        case KeyEvent.VK_SPACE:
                           System.out.println(Instances.GAMESTATE);
                           if(Instances.GAMESTATE == GameState.BATTLE){
                              Instances.battle.startFight();


Player movement methods

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
private void tileMovement(){
      if(moveLeft){
         if(tempX>0){
            x--;
            //Instances.camera.setX(Instances.camera.getX()-1);
            tempX--;
            ready = false;
            System.out.println(getX() + getY());
         }else if(tempX<=0){
            moveLeft = false;
            ready = true;
            movementTimer = 1;
         }
      }else if(moveRight){
         if(tempX>0){
            x++;
            //Instances.camera.setX(Instances.camera.getX()+1);
            tempX--;
            ready = false;
         }else if(tempX<=0){
            moveRight = false;
            ready = true;
            movementTimer = 1;
         }
      }else if(moveUp){
         if(tempY>0){
            y--;
            //Instances.camera.setY(Instances.camera.getY()-1);
            tempY--;
            ready = false;
         }else if(tempY<=0){
            moveUp = false;
            ready = true;
            movementTimer = 1;
         }
      }else if(moveDown){
         if(tempY>0){
            y++;
            //Instances.camera.setY(Instances.camera.getY()+1);
            tempY--;
            ready = false;
         }else if(tempY<=0){
            moveDown = false;
            ready = true;
            movementTimer = 1;
         }
      }        
   }

private void movementUpdate(){

      if(ready){
         if(left){
            moveLeft = true;
            tempX = 15;
         }else if(right){
            moveRight = true;
            tempX = 15;
         }else if(up){
            moveUp = true;
            tempY = 15;
         }else if(down){
            moveDown = true;
            tempY = 15;
         }
      }
      movementTimer++;
      if(movementTimer>1){
         tileMovement();
      }

   }
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2012-03-31 11:49:55 »

Well what have you tried? Have you at least added some println()s so you can see what's being called and what isn't? Is your keyPressed method getting called? Can you receive key events? Where does movementUpdate get called from? Is it being called? Is the 'ready' flag ever set?

Have you tried looking at the player x and y positions? Do they change but the position on screen doesn't change, or does the player x and y never change? Why is movementTimer an integer that behaves like a boolean? What is the magic '15' that appears everywhere?

If you answer those questions you'll probably find the bug easily.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Crisiszone

Senior Newbie





« Reply #7 - Posted 2012-03-31 11:57:23 »

I threw some print statements in tileMovement() to confirm that the players position is changing. It does infact change, so the key events are being handled correctly. Movement update is being called in the players update() method, which is called in the game loop.

At the moment, the players x and y get changed, but nothing occurs on the screen. Before I had changed how the map was being loaded, the movement was great, perfect for what I was going for actually. The only thing that changed was the map rendering.

The magic 15 is how far the player moves. It's half the length of one of my tiles.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2012-03-31 12:11:12 »

At the moment, the players x and y get changed, but nothing occurs on the screen. Before I had changed how the map was being loaded, the movement was great, perfect for what I was going for actually. The only thing that changed was the map rendering.

The magic 15 is how far the player moves. It's half the length of one of my tiles.

Ok, so check when your draw your player that the player.getX()/getY() actually returns the new position, and not the old one.

Have you check that your render() methods are actually being called repeatedly? I can't see anywhere that would trigger that.

Also, consider swapping your magic 15s with something like TILE_SIZE / 2.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Crisiszone

Senior Newbie





« Reply #9 - Posted 2012-03-31 12:19:46 »

Interesting! For some reason subtracting the Cameras x and y was keeping the player from moving, when all it did before was keep the screen centered on the player and allowed the camera to "follow" it. Now I need to find a way to fix the camera again >.< the endless joys of programming Tongue

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
public class Camera {
   private int x, y;
   

   Camera(){
     
   }
   
   public void setPosition(){
      x = Instances.player.getX()-Settings.width/2;
      y = Instances.player.getY()-Settings.heigth/2;
   }
   
   public void setX(int x){
      this.x = x;
   }
   
   public void setY(int y){
      this.y = y;
   }
   
   public int getX(){
      return x;
   }
   
   public int getY(){
      return y;
   }
}


and I always thought this was one of the easier things to do since it worked perfectly before >.< setPosition() is also called in the game loop along with the players update().
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (11 views)
2014-11-28 16:25:35

PocketCrafter7 (5 views)
2014-11-28 16:25:09

PocketCrafter7 (5 views)
2014-11-28 16:24:29

toopeicgaming1999 (73 views)
2014-11-26 15:22:04

toopeicgaming1999 (63 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (27 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!