Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Painting problem & Creating more than 1 "enemy" -Fixed/Answered-  (Read 1682 times)
0 Members and 1 Guest are viewing this topic.
Offline RyanMB97
« Posted 2012-03-23 21:07:32 »

Hello! First off, I got this problem. I am currently using a drawLine to create a "laser". I have an "anchor-point" set up in the middle of my character (at the moment a box). Whenever I click, it shoots and when I release it goes back to the middle. However, if I move, it still points to my previous location from when I released the button. Here's the code:

This was intended as a stop-gap to see if I could fix it.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void Shooting() {
      while (!SHOT) {
         xLaserDest = xLaserAnchor;
         yLaserDest = yLaserAnchor;
      }
      while (SHOT) {
         xLaserDest = xCoordinate;
         yLaserDest = yCoordinate;
      }
   }

My MouseListener stuff:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
class Mouse extends MouseAdapter {
      @Override
      public void mousePressed(MouseEvent e) {
         int xCoordinate = e.getX();
         int yCoordinate = e.getY();
         xMouse = xCoordinate;
         yMouse = yCoordinate;

         xLaserDest = xMouse;
         yLaserDest = yMouse;

         SHOT = true;
      }

      @Override
      public void mouseReleased(MouseEvent e) {
         xLaserDest = xChar + 16;
         yLaserDest = yChar + 16;

         SHOT = false;

      }
   }


My "run" method:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public void run() {
      try {
         while (true) {
            FPS.tick();
            move();
            zom.FindCharacter();
            Thread.sleep(10);
         }
      } catch (Exception e) {
         System.out.println("Error");
      }

   }

and finally my "Paint" and "PaintComponent" 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  
public void paint(Graphics g) {
      dbImage = createImage(getWidth(), getHeight());
      dbg = dbImage.getGraphics();
      paintComponent(dbg);
      g.drawImage(dbImage, 0, 0, this);

   }

   public void paintComponent(Graphics g) {

      xLaserAnchor = xChar + 16;
      yLaserAnchor = yChar + 16;

      g.drawImage(backImage, 0, 0, this);
      g.drawImage(charImage, xChar, yChar, this);
      g.drawImage(zombieImage, zom.xZombie, zom.yZombie, this);

      g.setColor(Color.YELLOW);
      g.setFont(font2);
      g.drawString(Controls, 90, 40);

      g.drawString("MoveSpeed: " + moveSpeed, 480, 40);

      g.setColor(Color.YELLOW);
      g.setFont(font);
      g.drawString("FPS: " + FPS.FPS, 5, 40);

      g.setColor(Color.YELLOW);
      g.drawLine(xLaserAnchor, yLaserAnchor, xLaserDest, yLaserDest);

      repaint();
   }


Another thing I want to do is create more than 1 "Zombie". I do not know how to do this at my current level, and Google hasn't helped much on this issue. If you could help me out here, I'd be eternally grateful Cheesy. Please don't hesitate to ask for my entire source-code, I really want to get this thing working Tongue

-Java Newbie,
Ryan
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-23 22:18:09 »

Nothing looks wrong, x and y LaserDest seem to be assigned just fine when you press the mouse.

Try making them volatile? You make them volatile by specifying "volatile" right their declaration.

The entire source could be useful Smiley

As for creating more zombies, it is simply a matter of using OO to your advantage. Create a Zombie class and then have an ArrayList of Zombies then you loop through to update and draw Smiley


Offline RyanMB97
« Reply #2 - Posted 2012-03-23 22:28:04 »

Okay! I just want to say first that I don't know how to go about creating the ZombiesArray. Is there any chance you have an example for me to study? Also, here's my code in Pastebin: http://pastebin.com/7cnJ4Xf7. Thanks in advance for help Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel


Medals: 350
Projects: 3
Exp: 5 years


I'm the King!


« Reply #3 - Posted 2012-03-23 22:51:00 »

I ran your code and it runs fine, except for when I move with the arrows keys, it draws a line from the old location to the new location. This is because you don't update x and y LaserDest when then anchor points move. Using the mouse shoots fine every time.

BTW- A JFrame isn't meant to be extended, you should be extending JComponent and adding that to a JFrame. Then you don't have to create a new image every time, just simply draw in that JComponent's paintComponent(Graphics)

Offline RyanMB97
« Reply #4 - Posted 2012-03-23 22:54:47 »

Thanks for your help! I got most of the stuff fixed. Now I just have to look up a few bits and pieces. Thanks again Tongue
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.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (43 views)
2014-10-16 15:22:06

Norakomi (33 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (67 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (86 views)
2014-10-11 22:30:10
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!