Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  SOLVED - Enemy class  (Read 1766 times)
0 Members and 1 Guest are viewing this topic.
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Posted 2011-12-14 22:34:48 »

I am trying to use the same enemy class to create two enemies. How can i do this? If I try to just call it twice, it makes two different ones, but they follow the exact same patter.

Here is the class -


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  
package main;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Enemy1 {
   int x, y;
   int dx, dy;
   Image enemy;
   boolean starting;
   boolean newDirection = false;
   
   public Enemy1()
   {
      ImageIcon i = new ImageIcon(getClass().getResource("/textures/enemy.png"));
      enemy = i.getImage();
      x = 100 + (int) (Math.random() * ((200 - 100) + 1));
      y = 100 + (int) (Math.random() * ((200 - 100) + 1));
      starting = true;
   }
   
   public void move()
   {
      if (starting)
      {
         int movement = 1 + (int) (Math.random() * ((2 - 1) + 1));
         
         if (movement == 1) dx = 1;
         if (movement == 2) dx = -1;
         
         movement = 3 + (int) (Math.random() * ((4 - 3) + 1));
         
         if (movement == 3) dy = 1;
         if (movement == 4) dy = -1;
         
         dx = 1;
         dy = 1;
         
         starting = false;
      }
     
      if (x == 750)
      {
         x = 749;
         newDirection = true;
      }
     
      if (x == 10)
      {
         x = 11;
         newDirection = true;
      }
     
      if (y == 480)
      {
         y = 479;
         newDirection = true;
      }
     
      if (y == 10)
      {
         y = 11;
         newDirection = true;
      }
     
      if (newDirection)
      {        
         if ((dx == 1) && (dy == 1))
         {
            if (y == 479) dy = -1;
            if (x == 749) dx = -1;
         }
         
         else if ((dx == 1) && (dy == -1))
         {
            if (y == 11) dy = 1;
            if (x == 749) dx = -1;
         }
         
         else if ((dx == -1) && (dy == 1))
         {
            if (y == 479) dy = -1;
            if (x == 11) dx = 1;
         }
         
         else if ((dx == -1) && (dy == -1))
         {
            if (y == 11) dy = 1;
            if (x == 11) dx = 1;
         }
         
         newDirection = false;
      }
     
      x = x + dx;
      y = y + dy;
   }
   
   public int getX() {return x;}
   public int getY() {return y;}
   public Image getImage() {return enemy;}
}


Here's the Board class where the enemy is drawn -

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  
public class Board extends JPanel implements ActionListener
{
   private static final long serialVersionUID = -6045447081410695806L;
   
   Player p;
   Enemy1 en, en2;
   Image background;
   Timer time;
   Player playerGetX;
   
   public Board()
   {
      p = new Player();
      en = new Enemy1();
      en2 = new Enemy1();
      addKeyListener(new AL());
      setFocusable(true);
      ImageIcon i = new ImageIcon(getClass().getResource("/textures/Moldy_Background.png"));
      background = i.getImage();
      time = new Timer(5, this);
      time.start();
   }
   
   public void actionPerformed(ActionEvent e)
   {
      p.move();
      en.move();
      en2.move();
      checkCollision();
      repaint();
   }
   
   public void paint(Graphics g)
   {
      super.paint(g);
      Graphics2D g2d = (Graphics2D) g;
     
      g2d.drawImage(background, 0, 0, null);
      g2d.drawImage(p.getImage(), p.getX(), p.getY(), null);
      g2d.drawImage(en.getImage(), en.getX(), en.getY(), null);
      g2d.drawImage(en2.getImage(), en2.getX(), en2.getY(), null);
   }
   
   private class AL extends KeyAdapter
   {
      public void keyReleased(KeyEvent e)
      {
         p.keyReleased(e);
      }
     
      public void keyPressed(KeyEvent e)
      {
         p.keyPressed(e);
      }
   }
   
   public void checkCollision()
   {
     
   }
}


Thanks.

Check out my website!
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2011-12-14 23:07:21 »

Have you tried stepping through it in a debugger? My guess is that the (int) rounding is actually cancelling out whatever effect Math.random() is having, so they both follow the same non-random path. What happens if you do all your movement as floats and just cast the position back to ints to draw?

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

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #2 - Posted 2011-12-14 23:38:14 »

Thanks. It works now.

Check out my website!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2011-12-15 00:19:04 »

sry for asking,
but what were you thinking when you wrote this code?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
if (starting)
      {
         int movement = 1 + (int) (Math.random() * ((2 - 1) + 1));
         
         if (movement == 1) dx = 1;
         if (movement == 2) dx = -1;
         
         movement = 3 + (int) (Math.random() * ((4 - 3) + 1));


         
         if (movement == 3) dy = 1;
         if (movement == 4) dy = -1;
         
         dx = 1;
         dy = 1;
         
         starting = false;
      }


first of all, why is this not in the constructor Huh
and second what is it with this movment field?

1  
2  
dx = Math.random() < 0.5 ? -1 : 1;
dy = Math.random() < 0.5 ? -1 : 1;
Offline StonePickaxes

JGO Coder


Medals: 4
Projects: 2


Nathan Kramber


« Reply #4 - Posted 2011-12-15 00:25:54 »


1  
2  
dx = Math.random() < 0.5 ? -1 : 1;
dy = Math.random() < 0.5 ? -1 : 1;


Because I am new and have NO idea what that *points up* even means.

Check out my website!
Offline Z-Man
« Reply #5 - Posted 2011-12-15 00:46:14 »

It's a ternary operator, or a simplified if-else. For example, the below code sets x to 5 if 0 is less than 1 and -5 if 0 is not less than 1.
1  
int x = 0 < 1 ? 5 : -5;

Offline theagentd

« JGO Bitwise Duke »


Medals: 366
Projects: 2
Exp: 8 years



« Reply #6 - Posted 2011-12-15 09:41:58 »

1  
int x = booleanExpression ? 5 : -5;

is equal to
1  
2  
3  
4  
5  
6  
int x;
if(booleanExpression){
    x = 5;
}else{
    x = -5;
}

It's just a convenience method.

Myomyomyo.
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.

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (55 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (91 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (92 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03

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

toopeicgaming1999 (164 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

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
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!