VIrtueeL
|
 |
«
Posted
2014-03-11 11:10:53 » |
|
thay are really good to know if do alot of things like items/animals/AI
i have always created 100 moveMethods for animals like
public void moveCow(){ } public void movePig(){ } public void moveZombie(){ }
now thay all have just one called
public void move(){ } so mutch easyier
|
|
|
|
Opiop
|
 |
«
Reply #1 - Posted
2014-03-11 11:11:52 » |
|
Yeah...
|
|
|
|
Roquen
|
 |
«
Reply #2 - Posted
2014-03-11 12:34:34 » |
|
Humm...you don't need an interface for this just method overloading. And really you don't even need that.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
VIrtueeL
|
 |
«
Reply #3 - Posted
2014-03-11 12:49:08 » |
|
what xD?
|
|
|
|
Opiop
|
 |
«
Reply #4 - Posted
2014-03-11 12:50:33 » |
|
You could use a structure where you have a superclass and a child class that extends that superclass and inherits the parent functions. An interface here isn't the best choice.
|
|
|
|
Varkas
|
 |
«
Reply #5 - Posted
2014-03-11 12:52:49 » |
|
... and if you want to be really obscure you can do all method calls "by name" through reflection.
|
|
|
|
Opiop
|
 |
«
Reply #6 - Posted
2014-03-11 12:56:34 » |
|
How was my answer obscure?
|
|
|
|
SHC
|
 |
«
Reply #7 - Posted
2014-03-11 12:59:34 » |
|
Interfaces are particularly useful if your code involves a common type which has many implementations. The best example here would be the Java Collections API. There is an interface called and there are a lot of implementations of it like , etc., As @opiop65 pointed out, interfaces are not the best option here. You could instead go with a superclass with subclasses since you are only seeking method overloading.
|
|
|
|
Varkas
|
 |
«
Reply #8 - Posted
2014-03-11 13:00:52 » |
|
How was my answer obscure?
It's not been about your answer. It was a (inserious) suggestion to the original poster. Smalltalk programmers won't think it's obscure though, since in smalltalk you just need the method name and no interface or super class.
|
|
|
|
Opiop
|
 |
«
Reply #9 - Posted
2014-03-11 13:03:54 » |
|
Ah, I'm sorry! Thought that was directed at me and I was confused.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
VIrtueeL
|
 |
«
Reply #10 - Posted
2014-03-11 18:48:01 » |
|
it works greate so far lols
|
|
|
|
Opiop
|
 |
«
Reply #11 - Posted
2014-03-11 19:08:58 » |
|
So you're going to ignore everyone that posted here who all have more experience than you? That's wise, let me know how that turns out.
|
|
|
|
VIrtueeL
|
 |
«
Reply #12 - Posted
2014-03-11 19:11:58 » |
|
am not recoding this 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
| public class Player implements Entity{
int w = 64; int h = 64; int damage = 7; int health = 100; int lastHealth = health - 1; boolean isDead = false; boolean isDamaged; static boolean isMoving = false; boolean MovingRight = false; boolean MovingLeft= false; boolean MovingUp= false; boolean MovingDown= false; public static int MouseDetection = 150; public String Username = ""; public static Vector2F pos = new Vector2F(0,0); public static Vector2F moveto = new Vector2F(0,0); public Player(int x, int y) { this.pos.x = (int) x; this.pos.y = (int) y; } BufferedImage image; @Override public void SetEntityImage(BufferedImage image) { this.image = image; } @Override public void SetEntityImageWidthAndHeight(int w, int h) { this.w = w; this.h = h; } @Override public void RenderEntity(Graphics2D g2d) { if(image != null){ g2d.drawImage(image, (int)Player.pos.getWorldLocation().x,(int) Player.pos.getWorldLocation().y, w , h , null); g2d.drawRect((int)Player.moveto.getWorldLocation().x - 64 + 32 , (int)Player.moveto.getWorldLocation().y - 64 + 32, 128, 128); g2d.drawRect((int)Player.pos.getWorldLocation().x ,(int) Player.pos.getWorldLocation().y, w, h); Sys.sendSytemMessage(isMoving +"Rendering"); MoveToLocation(); }
}
private void MoveToLocation() { if(Player.pos.getWorldLocation().x < Player.moveto.getWorldLocation().x) { if(Math.abs(Player.moveto.x - Player.pos.x) > 1) { Player.pos.x-=0.2; isMoving = true; }else{ isMoving = false; } } else { if(Math.abs(Player.moveto.x - Player.pos.x) > 1) { Player.pos.x+=0.2; isMoving = true; }else{ isMoving = false; } } if(Player.pos.getWorldLocation().y < Player.moveto.getWorldLocation().y) { if(Math.abs(Player.moveto.y - Player.pos.y) > 1) { Player.pos.y-=0.2; isMoving = true; }else{ isMoving = false; } } else { if(Math.abs(Player.moveto.y - Player.pos.y) > 1) { Player.pos.y+=0.2; isMoving = true; }else{ isMoving = false; } } }
@Override public void move(float x,float y) { this.pos.x += (int) x; this.pos.y += (int) y; }
@Override public void damage(int amount) { this.damage = amount; }
@Override public void hurt(int amount) { health -= amount; }
@Override public void isDead(boolean isDead) { this.isDead = isDead; }
@Override public void isDamaged() { if(health < lastHealth){ isDamaged = true; }else{ isDamaged = false; } }
@Override public void isColliding() {}
@Override public void isMoving() { if(isMoving = true){
} }
@Override public Rectangle getBounds() { return new Rectangle((int)pos.x, (int)pos.y, w, h); }
@Override public Rectangle getWorldBounds() { return new Rectangle((int)pos.getWorldLocation().x, (int)pos.getWorldLocation().y, w, h); }
@Override public void moveWorldLocation(float x, float y) { this.pos.getWorldLocation().x += x; this.pos.getWorldLocation().y += y; }
@Override public void mouseDragged(MouseEvent e) {}
@Override public void mouseMoved(MouseEvent e) { int y = e.getY(); int x = e.getX(); }
@Override public void mouseClicked(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
@Override public void mousePressed(MouseEvent e) { if (SwingUtilities.isRightMouseButton(e)) { Vector2F pos = new Vector2F(e.getX(),e.getY()); Player.moveto.x = pos.getWorldLocation().x; Player.moveto.y = pos.getWorldLocation().y; } }
@Override public void mouseReleased(MouseEvent e) {}
} |
|
|
|
|
Longarmx
|
 |
«
Reply #13 - Posted
2014-03-12 00:08:08 » |
|
Thats actually not a lot of code. Plus, better to do it now than later.
|
|
|
|
BurntPizza
|
 |
«
Reply #14 - Posted
2014-03-12 00:15:06 » |
|
Thats actually not a lot of code. Plus, better to do it now than later.
Less than 200 lines, and looks like it would be half that when properly refactored.
|
|
|
|
Brynn
|
 |
«
Reply #15 - Posted
2014-03-12 03:12:34 » |
|
I never learned anything about interfaces besides that I use them like classes, because I use runnable right? and thats an interface. But can someone clarify for me what an interface is and what it is ussually used for, and how is it different than a class.
|
Welcome to a new kind of tension All across the alienation Where everything isn't meant to be okay
|
|
|
The Lion King
|
 |
«
Reply #16 - Posted
2014-03-12 03:29:14 » |
|
I never learned anything about interfaces besides that I use them like classes, because I use runnable right? and thats an interface. But can someone clarify for me what an interface is and what it is ussually used for, and how is it different than a class.
Google is your friend. This is a very basic and very broad question. I'll give you some basics about it though. An interface is pretty much a partial template for the class. It forces a class that "implements" an interface to have all the functions that the interface states and forces you to define what they do. If you know what an abstract/virtual method is. A interface is a class that is made up of only abstract/virtual methods. for example, you mentioned the runnable interface. The whole interface simply looks like this. 1 2 3 4
| public interface Runnable { public void run(); } |
so anything that implements Runnable must have the run() function, but every class defines the run function themselves. This guarantees that any Runnable object has the method run(). Ill give you a simple example. 1 2 3 4
| public interface Drawable { public void draw(); } |
Example class that uses drawable: 1 2 3 4 5 6 7 8
| public class Car implements Drawable { public void draw() { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class Game { Drawable[] drawThese; public Game() { drawThese = new Drawable[2]; drawThese[0] = new Car(); drawThese[1] = new otherDrawableThing(); } public void gameLoop() { for(int i = 0; i < drawThese.length; i++) drawThese[i].draw(); } } |
edit: there is a lot more to learn. Google. Also note the useful characteristic that you can implement as many interfaces as you want and you can only extend from one class in Java.
|
"You have to want it more than you want to breath, then you will be successful"
|
|
|
trollwarrior1
|
 |
«
Reply #17 - Posted
2014-03-12 05:48:38 » |
|
I never learned anything about interfaces besides that I use them like classes, because I use runnable right? and thats an interface. But can someone clarify for me what an interface is and what it is ussually used for, and how is it different than a class.
Interface is not a class. When you do: 1 2 3 4 5 6 7 8 9 10 11
| public interface Runnable { public void run(); }
{ Thread thread = new Thread(new Runnable(){ public void run(){ System.out.println("overriding runnable meethod.."); } }).start(); } |
new Runnable means that you're creating a new class, which implements runnable.
|
|
|
|
Roquen
|
 |
«
Reply #18 - Posted
2014-03-12 07:48:46 » |
|
The reason interfaces exist is for cross-cuts. Draw a picture of a tree. That represent a class hierarchy. Now if you need common functionality down the tree in nodes whose common parent are far toward the root then you need an interface to make them type compatible for that functionality. Or similarly to make type compatible classes that creating a common base class is impossible/impractical because they are out of your control.
|
|
|
|
Grunnt
|
 |
«
Reply #19 - Posted
2014-03-12 09:21:44 » |
|
Really, guys and gals, this is a question which is extremely simple to find many answers to. Here's the top 3 google results for 'java interface': - What Is an Interface? (The Java™ Tutorials) - Interfaces (The Java™ Tutorials) - Interface (Java) on Wikipedia
Each of which are very clear and useable explanations of what an Interface in Java is.
|
|
|
|
Roquen
|
 |
«
Reply #20 - Posted
2014-03-12 09:28:39 » |
|
I'd note that "design by interface" has been fashionable for some number of years and is a pretty poor model.
|
|
|
|
jacobgood1
Senior Newbie 
|
 |
«
Reply #21 - Posted
2014-03-15 04:34:14 » |
|
Interfaces are also good for decoupling data and functions. It can pave the way for making objects that are more like data where one can specify actions independent of a hierarchy. However, to really understand what I am saying(typing), you might need to get some functional language exposure(not necessary, but it speeds up the process).
|
|
|
|
Roquen
|
 |
«
Reply #22 - Posted
2014-03-15 11:23:29 » |
|
The same can be said for design by composition.
|
|
|
|
|